def app(video_link, video_name, show, record, flip_hor, flip_ver):
    # initialize Face Detection net
    face_detector = FaceDetector()

    # initialize Emotioner net
    emotioner = Emotioner()

    # initialize Video Capturer
    cap = cv.VideoCapture(video_link)
    (W, H), FPS = imgproc.cameraCalibrate(cap)
    LOG.info('Camera Info: ({}, {}) - {:.3f}'.format(W, H, FPS))

    if record:
        time_str = time.strftime(cfg.TIME_FM)
        writer = cv.VideoWriter(video_name+time_str+'.avi',
                cv.VideoWriter_fourcc(*'XVID'), FPS, (W, H))

    while cap.isOpened():
        _, frm = cap.read()
        if not _:
            LOG.info('Reached the end of Video source')
            break

        if flip_ver: frm = cv.flip(frm, 0)
        if flip_hor: frm = cv.flip(frm, 1)

        _start_t = time.time()
        scores, bboxes = face_detector.getFaces(frm, def_score=0.5)
        emos = []
        pbboxes = []
        for i, bbox in enumerate(bboxes):
            x1, y1, x2, y2 = getPadding(frm, bbox)
            face_img = frm[y1:y2, x1:x2]
            emo_idx = emotioner.getEmotion(face_img)
            emos.append(EMOTION[emo_idx])
            pbboxes.append((x1, y1, x2, y2))
        _prx_t = time.time() - _start_t


        if len(bboxes):
            frm = vis.plotBBoxes(frm, pbboxes, emos) 
        frm = vis.plotInfo(frm, 'Raspberry Pi - FPS: {:.3f}'.format(1/_prx_t))
        frm = cv.cvtColor(np.asarray(frm), cv.COLOR_BGR2RGB)

        if record:
            writer.write(frm)
       
        if show:
            cv.imshow(video_name, frm)
            key = cv.waitKey(1)
            if key in [27, ord('q')]:
                LOG.info('Interrupted by Users')
                break

    if record:
        writer.release()
    cap.release()
    cv.destroyAllWindows()
def app(video_link, video_name, show, flip_hor, flip_ver):
    # initialize Face Detection net
    face_detector = FaceDetector()
    LOG.info('Face Detector initialization done')

    # initialize Face Landmark net
    face_landmarker = FaceLandmarker()
    LOG.info('Face Landmarker initialization done')

    # initialize Video Capturer
    cap = cv.VideoCapture(video_link)
    (W, H), FPS = imgproc.cameraCalibrate(cap, size=720, by_height=True)
    LOG.info('Camera Info: ({}, {}) - {:.3f}'.format(W, H, FPS))

    while cap.isOpened():
        _, frm = cap.read()
        if not _:
            LOG.info('Reached the end of Video source')
            break

        if flip_ver: frm = cv.flip(frm, 0)
        if flip_hor: frm = cv.flip(frm, 1)

        frm = imgproc.resizeByHeight(frm, 720)

        _start_t = time.time()
        scores, bboxes = face_detector.getFaces(frm, def_score=0.5)

        landmarks = []
        for i, bbox in enumerate(bboxes):
            x1, y1, x2, y2 = bbox
            face_img = frm[y1:y2, x1:x2]
            landmark = face_landmarker.getLandmark(face_img)
            landmark[:, :] += np.array([x1, y1])
            landmarks.append(landmark)
        _prx_t = time.time() - _start_t

        if len(bboxes):
            for i, landmark in enumerate(landmarks):
                for j, point in enumerate(landmark):
                    cv.circle(frm, tuple(point), 3, (0, 255, 0), -1)

            frm = vis.plotBBoxes(frm, bboxes, len(bboxes) * ['face'], scores)

        frm = vis.plotInfo(frm,
                           'Raspberry Pi - FPS: {:.3f}'.format(1 / _prx_t))
        frm = cv.cvtColor(np.asarray(frm), cv.COLOR_BGR2RGB)

        if show:
            cv.imshow(video_name, frm)
            key = cv.waitKey(1)
            if key in [27, ord('q')]:
                LOG.info('Interrupted by Users')
                break

    cap.release()
    cv.destroyAllWindows()
def app(video_link, video_name, show=False, record=False):
    # initialize TFLite model
    labels = load_labels(cfg.LABEL_PATH)
    
    interpreter = Interpreter(cfg.DETECT_MODEL_PATH)
    interpreter.allocate_tensors()
    _, input_H, input_W, _ = interpreter.get_input_details()[0]['shape']

    # initialize video capturer
    cap = cv.VideoCapture(video_link)
    (W, H), FPS = imgproc.cameraCalibrate(cap)
    FRM_MOD = int(FPS / cfg.pFPS + 0.5)
    LOG.info('Camera Info: ({}, {}) - {:.3f}'.format(W, H, FPS))

    if record:
        writer = imgproc.getVideoRecorder(video_name, (W, H), FPS)

    frm_cnt = 0
    while cap.isOpened():
        _, frm = cap.read()
        if not _:
            LOG.info('Reached the end of Video source')
            break

        frm_cnt += 1
        if frm_cnt % FRM_MOD: continue

        _start_t = time.time()
        inp_image = cv.cvtColor(frm, cv.COLOR_BGR2RGB)
        inp_image = cv.resize(inp_image, (input_W, input_H))
        scores, class_ids, bboxes = detect_objects(interpreter, 
                inp_image, frm.shape[:2], thresh=0.6)
        _prx_t = time.time() - _start_t
        LOG.info('FPS: {:.3f}'.format(1/_prx_t))

        frm = vis.plotBBoxes(frm, bboxes.astype('int'), 
                classes=[labels[idx] for idx in class_ids],
                scores=scores) 
        frm = vis.plotInfo(frm, 'Raspberry Pi - FPS: {:.2f}'.format(1/_prx_t))
        frm = cv.cvtColor(np.asarray(frm), cv.COLOR_BGR2RGB)

        if record:
            writer.write(frm)

        if show:
            cv.imshow(video_name, frm)
            key = cv.waitKey(1)
            if key in [27, ord('q')]:
                LOG.info('Interrupted by Users')
                break

    if record:
        writer.release()
    cap.release()
    cv.destroyAllWindows()
示例#4
0
def app(video_link, video_name, show, record, flip_hor, flip_ver):
    # initialize Face Detection net
    object_detector = ObjectDetector()

    # initialize Video Capturer
    cap = cv.VideoCapture(video_link)
    (W, H), FPS = imgproc.cameraCalibrate(cap)
    LOG.info('Camera Info: ({}, {}) - {:.3f}'.format(W, H, FPS))

    if record:
        time_str = time.strftime(cfg.TIME_FM)
        writer = cv.VideoWriter(video_name + time_str + '.avi',
                                cv.VideoWriter_fourcc(*'XVID'), 20,
                                (1280, 720))

    cnt_frm = 0
    while cap.isOpened():
        _, frm = cap.read()
        if not _:
            LOG.info('Reached the end of Video source')
            break
        cnt_frm += 1

        if flip_ver: frm = cv.flip(frm, 0)
        if flip_hor: frm = cv.flip(frm, 1)
        frm = imgproc.resizeByHeight(frm, 720)

        _start_t = time.time()
        scores, bboxes = object_detector.getObjects(frm, def_score=0.5)
        _prx_t = time.time() - _start_t

        if len(bboxes):
            frm = vis.plotBBoxes(frm, bboxes, len(bboxes) * ['person'], scores)
        frm = vis.plotInfo(frm,
                           'Raspberry Pi - FPS: {:.3f}'.format(1 / _prx_t))
        frm = cv.cvtColor(np.asarray(frm), cv.COLOR_BGR2RGB)

        if record:
            writer.write(frm)

        if show:
            cv.imshow(video_name, frm)
            key = cv.waitKey(1)
            if key in [27, ord('q')]:
                LOG.info('Interrupted by Users')
                break

    if record:
        writer.release()
    cap.release()
    cv.destroyAllWindows()
def app(video_link, video_name, show, flip_hor, flip_ver):
    # initialize Face Detection net
    face_detector = FaceDetector()

    # initialize Video Capturer
    cap = cv.VideoCapture(video_link)
    (W, H), FPS = imgproc.cameraCalibrate(cap)
    LOG.info('Camera Info: ({}, {}) - {:.3f}'.format(W, H, FPS))

    while cap.isOpened():
        _, frm = cap.read()
        if not _:
            LOG.info('Reached the end of Video source')
            break

        if flip_ver: frm = cv.flip(frm, 0)
        if flip_hor: frm = cv.flip(frm, 1)

        _start_t = time.time()
        scores, bboxes = face_detector.getFaces(frm, def_score=0.5)
        _prx_t = time.time() - _start_t

        if len(bboxes):
            frm = vis.plotBBoxes(frm, bboxes, len(bboxes) * ['face'], scores)
        frm = vis.plotInfo(frm,
                           'Raspberry Pi - FPS: {:.3f}'.format(1 / _prx_t))
        frm = cv.cvtColor(np.asarray(frm), cv.COLOR_BGR2RGB)

        if show:
            cv.imshow(video_name, frm)
            key = cv.waitKey(1)
            if key in [27, ord('q')]:
                LOG.info('Interrupted by Users')
                break

    cap.release()
    cv.destroyAllWindows()
示例#6
0
def app(video_path, video_name, show=False, record=False):
    detector = FaceDetector()
    plugin = detector.getPlugin()
    poser = HeadPoser(plugin=plugin)

    cap = cv.VideoCapture(video_path)
    (W, H), FPS = imgproc.cameraCalibrate(cap)
    _, frm = cap.read()
    frm = imgproc.resizeByHeight(frm)
    H, W = frm.shape[:2]
    FRM_MOD = int(1. * FPS / cfg.pFPS + 0.5)
    LOG.info('Camera Info: ({}, {}) - {:.3f}'.format(W, H, FPS))

    if record:
        time_str = time.strftime(cfg.TIME_FM)
        writer = cv.VideoWriter(video_name + time_str + '.avi',
                                cv.VideoWriter_fourcc(*'XVID'), FPS, (W, H))

    cnt_frm = 0
    while cap.isOpened():
        _, frm = cap.read()
        if not _:
            LOG.info('Reached the end of Video source')
            break

        cnt_frm += 1
        if cnt_frm % FRM_MOD: continue
        print(cnt_frm)
        frm = imgproc.resizeByHeight(frm)

        _start_t = time.time()
        scores, bboxes = detector.getFaces(frm)

        if len(bboxes):
            for i, bbox in enumerate(bboxes):
                x1, y1, x2, y2 = bbox
                face_image = frm[y1:y2, x1:x2]
                yaw, pitch, roll = poser.estimatePose(face_image)

                cpoint = [(x1 + x2) // 2, (y1 + y2) // 2]
                frm = hvis.draw(frm, cpoint, (yaw, pitch, roll))
        _prx_t = time.time() - _start_t

        frm = vis.plotInfo(frm,
                           'Raspberry Pi - FPS: {:.3f}'.format(1 / _prx_t))
        frm = cv.cvtColor(np.asarray(frm), cv.COLOR_BGR2RGB)

        if record:
            writer.write(frm)

        if show:
            cv.imshow(video_name, frm)
            key = cv.waitKey(1)
            if key in [27, ord('q')]:
                LOG.info('Interrupted by Users')
                break

    if record:
        writer.release()
    cap.release()
    cv.destroyAllWindows()
def app(video_link, video_name, show, record, flip_hor, flip_ver):
    # initialize Face Detection net
    config = read_py_config('config.py')
    object_detector = ObjectDetector()
    reid = PersonEmbedder()

    # initialize Video Capturer
    #cap = MulticamCapture(video_link)
    cap = WebcamVideoStream(src=video_link).start()
    (W, H), FPS = imgproc.cameraCalibrate(cap)
    LOG.info('Camera Info: ({}, {}) - {:.3f}'.format(W, H, FPS))
    tracker = MultiCameraTracker(1, reid, **config)

    if record:
        time_str = time.strftime(cfg.TIME_FM)
        writer = cv.VideoWriter(video_name + time_str + '.avi',
                                cv.VideoWriter_fourcc(*'XVID'), 20,
                                (1280, 720))

    cnt_frm = 0
    counter = 0
    while True:
        frm = cap.read()
        if frm is None:
            continue
        cnt_frm += 1

        # if flip_ver: frm = cv.flip(frm, 0)
        # if flip_hor: frm = cv.flip(frm, 1)
        _start_t = time.time()
        all_detections = []

        frm = imgproc.resizeByHeight(frm, 640)
        _, bboxes = object_detector.getObjects(frm, def_score=0.5)
        all_detections.append(bboxes)

        tracker.process([frm], all_detections, [[]])
        tracked_objects = tracker.get_tracked_objects()
        _prx_t = time.time() - _start_t
        fps = round(1 / _prx_t, 1)
        if len(bboxes):
            frm = visualize_multicam_detections([frm], tracked_objects, fps)
        frm = vis.plotInfo(frm,
                           'Raspberry Pi - FPS: {:.3f}'.format(1 / _prx_t))
        frm = cv.cvtColor(np.asarray(frm), cv.COLOR_BGR2RGB)

        if record:
            writer.write(frm)

        if show:
            cv.imshow(video_name, frm)
            key = cv.waitKey(1)
            if key in [27, ord('q')]:
                LOG.info('Interrupted by Users')
                break
        else:
            if (counter % 10 == 0):
                print(
                    f"IN : {SingleCameraTracker.COUNT_IN}, OUT: {SingleCameraTracker.COUNT_OUT}"
                )
        counter += 1

    if record:
        writer.release()
    cap.release()
    cv.destroyAllWindows()