Esempio n. 1
0
def app(image_path):
    image = cv.imread(image_path)
    assert image is not None

    face_detector = FaceDetector()
    face_landmarker = FaceLandmarker()

    scores, bboxes = face_detector.getFaces(image, def_score=0.5)
    landmarks = []
    for i, bbox in enumerate(bboxes):
        x1, y1, x2, y2 = bbox
        face_img = image[y1:y2, x1:x2]
        landmark = face_landmarker.getLandmark(face_img)
        landmark[:, :] += np.array([x1, y1])
        landmarks.append(landmark)

    for i, landmark in enumerate(landmarks):
        aligned_img = align(image, bboxes[i], landmark)
        cv.imshow(str(i), aligned_img)

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

        image = vis.plotBBoxes(image, bboxes)
        image = cv.cvtColor(np.asarray(image), cv.COLOR_BGR2RGB)

    cv.imshow(image_path, image)
    cv.waitKey(0)
Esempio n. 2
0
def app(image_path):

    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']

    for i in range(5):
        image = cv.imread(image_path)
        assert image is not None, 'Invalid image path'

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

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

        cv.imwrite('output.jpg', image)
Esempio n. 3
0
 def __drawBoxes(self):
     if len(self.detectedObjects):
         labels = len(self.detectedObjects) * ['strawberry']
         confidence = len(self.detectedObjects) * [0]
         coordinates = [(x, y, x + w, y + h) for x, y, w, h in self.detectedObjects]
         self.frame = vis.plotBBoxes(self.frame, coordinates, labels, confidence)
     self.frame = vis.plotInfo(self.frame, 'Flint View')
     self.frame = cv.cvtColor(np.asarray(self.frame), cv.COLOR_BGR2RGB)
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()
Esempio n. 7
0
def app(image_path):
    # 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')

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

        image = imgproc.resizeByHeight(image, 720)

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

        landmarks = []
        for i, bbox in enumerate(bboxes):
            x1, y1, x2, y2 = bbox
            face_img = image[y1:y2, x1:x2]
            landmark = face_landmarker.getLandmark(face_img)

            aligned_img = alignFace(face_img, landmark)
            cv.imshow('aligned-faces' + str(i), aligned_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(image, tuple(point), 3, (0, 255, 0), -1)

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

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

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

    cap.release()
    cv.destroyAllWindows()
Esempio n. 8
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(image_path):
    # 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 Face Alignment class
    face_aligner = FaceAligner()
    LOG.info('Face Aligner initialization done')

    # initialize Video Capturer
    image = cv.imread(image_path)
    assert image is not None

    image = imgproc.resizeByHeight(image, 720)

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

    landmarks = []
    for i, bbox in enumerate(bboxes):
        x1, y1, x2, y2 = bbox
        face_img = image[y1:y2, x1:x2]
        landmark = face_landmarker.getLandmark(face_img)

        aligned_img = face_aligner.align(face_img, landmark)
        cv.imshow('aligned-faces' + str(i), aligned_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(image, tuple(point), 3, (0, 255, 0), -1)

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

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

    cv.imshow(image_path, image)
    key = cv.waitKey(0)

    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()
Esempio n. 11
0
def processFrame(frm, face_detector, face_landmarker):
    _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)

    return frm
Esempio n. 12
0
            tracking = 0
            continue

        start_time = time.time()

    elif (len(bboxes) == 0) and (time.time() - start_time) > delay:
        tracking = 0
        stage = SEARCH_LR
        #go home call
        my_robot.goReady()
        print("go home,cant see anything")
        start_time = time.time()

    if len(bboxes):
        frm = vis.plotBBoxes(frm,
                             [(x, y, x + w, y + h) for x, y, w, h in bboxes],
                             len(bboxes) * ['strawberry'],
                             len(bboxes) * [0])
    frm = vis.plotInfo(frm, 'Raspberry Pi - FPS: {:.3f}'.format(1 / _prx_t))
    frm = cv.cvtColor(np.asarray(frm), cv.COLOR_BGR2RGB)

    # show the frame
    cv.imshow("Frame", frm)
    cv.imwrite("../Frontend/frame.jpg", frm)

    key = cv.waitKey(1)
    if key in [27, ord('q')]:
        break
    #time.sleep(2)

pipeline.stop()
Esempio n. 13
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)
    cap = WebcamVideoStream(src=cfg.RTSP_URL).start()
    # (W, H), FPS = imgproc.cameraCalibrate(cap.stream)
    # 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 True:
        frm = cap.read()
        if frm is None:
            continue
        #cnt_frm += 1
        # if(cnt_frm % 5 != 0):
        #     continue
        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()
        images, bboxes = object_detector.getObjects(frm, def_score=0.5)
        if images:
            files = []
            for im in images:
                byte_io = BytesIO()
                im.save(byte_io, 'png')
                byte_io.seek(0)
                files.append(('files', byte_io))
            files.append(
                ('bboxes', (None, json.dumps(bboxes), 'application/json')))
            if (len(bboxes)):
                try:
                    requests.post(url=URL, files=files)
                except:
                    continue

        _prx_t = time.time() - _start_t

        if len(bboxes):
            frm = vis.plotBBoxes(frm, bboxes, len(bboxes) * ['person'])
        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()
Esempio n. 14
0
            print("move FORWARD")
        elif (z == 0.0):
            my_robot.moveFB(-1 * increment)
            print("go home,cant see anything")
        else:
            print("DO NOT MOVE, in the middle Z")

        start_time = time.time()

    elif (len(bboxes) == 0) and (time.time() - start_time) > delay:
        #go home call
        my_robot.goReady()
        print("go home,cant see anything")
        start_time = time.time()

    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)

    # show the frame
    cv.imshow("Frame", frm)
    cv.imwrite("../Frontend/frame.jpg", frm)

    key = cv.waitKey(1)
    if key in [27, ord('q')]:
        break
    #time.sleep(2)

pipeline.stop()