Example #1
0
        def gen(fr):
            detector = object_detector.ObjectDetector(
                'ssd_mobilenet_v1_coco_2017_11_17')
            #detector = ObjectDetector('mask_rcnn_inception_v2_coco_2018_01_28')
            #detector = ObjectDetector('pet', label_file='data/pet_label_map.pbtxt')

            #cam = camera.VideoCamera()
            retry_cnt = 0
            while True:
                try:
                    frame, face_result_list = fr.get_frame_live()
                    #frame = cam.get_frame()
                    frame, obj_detect_dict = detector.detect_objects_live(
                        frame)
                    self.buffer_handle(face_result_list, obj_detect_dict)

                    ret, jpg = cv2.imencode('.jpg', frame)
                    jpg_bytes = jpg.tobytes()

                    yield (b'--frame\r\n'
                           b'Content-Type: image/jpeg\r\n\r\n' + jpg_bytes +
                           b'\r\n\r\n')
                except:
                    try:
                        time.sleep(1)
                        if retry_cnt > 5:
                            break
                        fr = face_recog.FaceRecog()
                    except:
                        retry_cnt += 1
Example #2
0
def video_feed():
    return Response(gen(face_recog.FaceRecog()),
                    mimetype='multipart/x-mixed-replace; boundary=frame')
Example #3
0
def video_feed():

    time.sleep(3)
    print('3초후 시작')
    return Response(gen(face_recog.FaceRecog()),
                    mimetype='multipart/x-mixed-replace; boundary=frame')
Example #4
0
        def func(face_recog_m, detector):
            retry_cnt = 0

            insert_flag = False
            insert_priv_time = time.time()

            imwrite_flag = False
            img_write_priv_time = time.time()

            while True:
                now_date = datetime.datetime.now()
                self.now_date = now_date
                now_str = now_date.strftime("%Y%m%d%H%M%S")

                curr_time = time.time()
                # db 입력 간격 측정
                if curr_time - insert_priv_time >= self.db_insert_term:
                    insert_priv_time = curr_time
                    insert_flag = True
                else:
                    insert_flag = False

                # 이미지 저장 간격 측정
                if curr_time - img_write_priv_time >= self.img_write_gap:
                    img_write_priv_time = curr_time
                    imwrite_flag = True
                else:
                    imwrite_flag = False

                try:
                    frame, face_result_list = face_recog_m.get_frame_live()
                    frame, obj_detection_dict = detector.detect_objects_live(
                        frame)
                    if imwrite_flag:
                        # 10 분마다 이미지파일 쓰기
                        img_write_file = self.imwrite_path + now_str + ".jpg"
                        cv2.imwrite(img_write_file, frame)
                        self.img_file_queue.put(img_write_file)
                        print("write img: ", img_write_file)

                        # 이미지 파일 개수가 1000개 이상일때 삭제
                        if self.img_file_queue.qsize() > self.max_img_file_cnt:
                            del_img_file = self.img_file_queue.get()
                            os.remove(del_img_file)
                            print("remove img: ", del_img_file)

                        # DB에서 2시간 이전의 데이터 삭제
                        self.delete_outdated_data(now_date)

                    for face_result in face_result_list:
                        face_corr = face_result[0]
                        face_name = face_result[1]
                        self.current_buffer.append((now_date, face_name))
                        if insert_flag:
                            val = (now_str, face_name, str(face_corr))
                            self.insert_db(val)
                            print("insert ", val)
                    for item in obj_detection_dict:
                        try:
                            obj_corr = item
                            class_str = obj_detection_dict[item][0]
                            class_list = class_str.split()
                            obj_class = class_list[0][:-1]
                            obj_score = int(class_list[1][:-1])
                            self.current_buffer.append((now_date, obj_class))
                            if insert_flag:
                                val = (now_str, obj_class, str(obj_corr))
                                self.insert_db(val)
                                print("insert ", val)
                        except:
                            pass
                    origin_len = len(self.current_buffer)
                    for _ in range(origin_len):
                        if self.check_current_max(now_date):
                            del self.current_buffer[0]
                    self.frame = frame
                    self.frame_cnt += 1
                except Exception as err:
                    print(err)
                    try:
                        face_recog_m = face_recog.FaceRecog()
                    except:
                        retry_cnt += 1
                        if retry_cnt > 5:
                            break
Example #5
0
    def run_video(self):
        print("run video")
        import camera
        face_recog_m = face_recog.FaceRecog()
        detector = ObjectDetector('ssd_mobilenet_v1_coco_2017_11_17')
        print("known faces: ", face_recog_m.known_face_names)

        # Using OpenCV to capture from device 0. If you have trouble capturing
        # from a webcam, comment the line below out and use a video file
        # instead.

        def func(face_recog_m, detector):
            retry_cnt = 0

            insert_flag = False
            insert_priv_time = time.time()

            imwrite_flag = False
            img_write_priv_time = time.time()

            while True:
                now_date = datetime.datetime.now()
                self.now_date = now_date
                now_str = now_date.strftime("%Y%m%d%H%M%S")

                curr_time = time.time()
                # db 입력 간격 측정
                if curr_time - insert_priv_time >= self.db_insert_term:
                    insert_priv_time = curr_time
                    insert_flag = True
                else:
                    insert_flag = False

                # 이미지 저장 간격 측정
                if curr_time - img_write_priv_time >= self.img_write_gap:
                    img_write_priv_time = curr_time
                    imwrite_flag = True
                else:
                    imwrite_flag = False

                try:
                    frame, face_result_list = face_recog_m.get_frame_live()
                    frame, obj_detection_dict = detector.detect_objects_live(
                        frame)
                    if imwrite_flag:
                        # 10 분마다 이미지파일 쓰기
                        img_write_file = self.imwrite_path + now_str + ".jpg"
                        cv2.imwrite(img_write_file, frame)
                        self.img_file_queue.put(img_write_file)
                        print("write img: ", img_write_file)

                        # 이미지 파일 개수가 1000개 이상일때 삭제
                        if self.img_file_queue.qsize() > self.max_img_file_cnt:
                            del_img_file = self.img_file_queue.get()
                            os.remove(del_img_file)
                            print("remove img: ", del_img_file)

                        # DB에서 2시간 이전의 데이터 삭제
                        self.delete_outdated_data(now_date)

                    for face_result in face_result_list:
                        face_corr = face_result[0]
                        face_name = face_result[1]
                        self.current_buffer.append((now_date, face_name))
                        if insert_flag:
                            val = (now_str, face_name, str(face_corr))
                            self.insert_db(val)
                            print("insert ", val)
                    for item in obj_detection_dict:
                        try:
                            obj_corr = item
                            class_str = obj_detection_dict[item][0]
                            class_list = class_str.split()
                            obj_class = class_list[0][:-1]
                            obj_score = int(class_list[1][:-1])
                            self.current_buffer.append((now_date, obj_class))
                            if insert_flag:
                                val = (now_str, obj_class, str(obj_corr))
                                self.insert_db(val)
                                print("insert ", val)
                        except:
                            pass
                    origin_len = len(self.current_buffer)
                    for _ in range(origin_len):
                        if self.check_current_max(now_date):
                            del self.current_buffer[0]
                    self.frame = frame
                    self.frame_cnt += 1
                except Exception as err:
                    print(err)
                    try:
                        face_recog_m = face_recog.FaceRecog()
                    except:
                        retry_cnt += 1
                        if retry_cnt > 5:
                            break

        th = threading.Thread(target=func, args=(face_recog_m, detector))
        th.daemon = True
        th.start()
        return th
def make_error(status_code, sub_code, message, action):
    response = jsonify({
        'status': status_code,
        'sub_code': sub_code,
        'message': message,
        'action': action
    })
    response.status_code = status_code
    return response


app = CustomFlask(__name__)
cors = CORS(app)

faceObject = face_recog.FaceRecog()


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/hello')
def hello():
    return "hello"


@app.route('/register')
def get_register():
    return render_template('register.html')
Example #7
0
    print("get_data.py execute...")
    #my_face = face_recog.FaceRecog()
    kosta_student = {
        'minji': '박민지',
        'chaeryeong': '이채령',
        'iu': '이지은',
        'Chris': '크리스 햄스워스',
        'Liam': '리암 햄스워스'
    }
    open_message = "출석확인 완료"
    state_message = "입실확인 완료"
    close_messate = "퇴칠확인 완료"

    today = GetTime()
    today_data = GetData()
    face_cam = face_recog.FaceRecog()

    while True:
        get_date = str(today.today_object)
        #print("get_date", get_date, type(get_date)) # get_date 2021-04-28 <class 'str'>
        get_hour = today.get_now().hour
        get_minute = today.get_now().minute
        get_time = str(get_hour) + ":" + str(get_minute)
        #print("get_time", get_time, type(get_time)) #get_time 16:44 <class 'str'>
        #print(get_hour,"시 ", get_minute,"분") # 16 시  44 분

        face_frame = face_cam.get_frame()
        cv2.imshow("Frame", face_frame)
        count = []
        if face_cam.face_names:
            name = face_cam.face_names
Example #8
0
        #print("%0.3f, %0.3f, %0.3f sec" % (time2 - time1, time3 - time2, time4 - time3))

        return frame, rtn_dict

    def get_jpg_bytes(self):
        frame = self.get_frame()
        # We are using Motion JPEG, but OpenCV defaults to capture raw images,
        # so we must encode it into JPEG in order to correctly display the
        # video stream.
        ret, jpg = cv2.imencode('.jpg', frame)
        return jpg.tobytes()


if __name__ == '__main__':
    import camera
    face_recog = face_recog.FaceRecog()
    print(face_recog.known_face_names)

    detector = ObjectDetector('ssd_mobilenet_v1_coco_2017_11_17')
    #detector = ObjectDetector('mask_rcnn_inception_v2_coco_2018_01_28')
    #detector = ObjectDetector('pet', label_file='data/pet_label_map.pbtxt')

    # Using OpenCV to capture from device 0. If you have trouble capturing
    # from a webcam, comment the line below out and use a video file
    # instead.
    cam = camera.VideoCamera()

    print("press `q` to quit")
    while True:
        frame, face_result_list = face_recog.get_frame_live()
        #frame = cv2.resize(frame, (640, 480))
        # Only existed path is allowed
        assert os.path.exists(args.train)
        print('Train Path :: ', args.train)
        train_path = args.train
        assert os.path.exists(args.test)
        print('Test Path :: ', args.test)
        test_path = args.test

    # Set window
    winName = 'Frame'
    cv.namedWindow(winName, cv.WINDOW_NORMAL)

    # --image option process
    if args.image:
        helmet = helmet.helmet_detection()
        face_recog = face_recog.FaceRecog(train_path)
        for img in os.listdir(test_path):
            # Extract unknown image list from directory
            img_path = test_path + '/' + img
            image_file = cv.imread(img_path)
            small_frame = cv.resize(image_file, (0, 0), fx=0.25, fy=0.25)

            face_recog_result = face_recog.get_frame(small_frame)
            helmet_result = helmet.get_detection(frame=small_frame,
                                                 copy_frame=face_recog_result)

            # show the frame
            cv.imshow(winName, helmet_result)
            key = cv.waitKey(2000) & 0xFF

            # if the `q` key was pressed, break from the loop