Пример #1
0
def webcam_main():
    print("Camera sensor warming up...")
    vs = cv2.VideoCapture(0)
    time.sleep(2.0)

    mark_detector = MarkDetector()
    
    # loop over the frames from the video stream
    while True:
        _, frame = vs.read()
        start = cv2.getTickCount()

        frame = imutils.resize(frame, width=750, height=750)
        frame = cv2.flip(frame, 1)
        faceboxes = mark_detector.extract_cnn_facebox(frame)

        if faceboxes is not None:
            for facebox in faceboxes:
                # Detect landmarks from image of 64X64 with grayscale.
                face_img = frame[facebox[1]: facebox[3],
                                    facebox[0]: facebox[2]]
                # cv2.rectangle(frame, (facebox[0], facebox[1]), (facebox[2], facebox[3]), (0, 255, 0), 2)
                face_img = cv2.resize(face_img, (CNN_INPUT_SIZE, CNN_INPUT_SIZE))
                face_img = cv2.cvtColor(face_img, cv2.COLOR_BGR2GRAY)
                face_img0 = face_img.reshape(1, CNN_INPUT_SIZE, CNN_INPUT_SIZE, 1)

                land_start_time = time.time()
                marks = mark_detector.detect_marks_keras(face_img0)
                # marks *= 255
                marks *= facebox[2] - facebox[0]
                marks[:, 0] += facebox[0]
                marks[:, 1] += facebox[1]
                # Draw Predicted Landmarks
                mark_detector.draw_marks(frame, marks, color=(255, 255, 255), thick=2)

        fps_time = (cv2.getTickCount() - start)/cv2.getTickFrequency()
        cv2.putText(frame, '%.1ffps'%(1/fps_time) , (frame.shape[1]-65,15), cv2.FONT_HERSHEY_DUPLEX, 0.5, (0,255,0))
        # show the frame
        cv2.imshow("Frame", frame)
        # writer.write(frame)
        key = cv2.waitKey(1) & 0xFF

        # if the `q` key was pressed, break from the loop
        if key == ord("q"):
            break

    # do a bit of cleanup
    cv2.destroyAllWindows()
    vs.stop()
Пример #2
0
def webcam_main():
    print("Camera sensor warming up...")
    cv2.namedWindow('face landmarks', cv2.WINDOW_NORMAL)
    frame = cv2.imread(IMAGE_PATH)
    time.sleep(2.0)

    mark_detector = MarkDetector(current_model, CNN_INPUT_SIZE)
    if current_model.split(".")[-1] == "pb":
        run_model = 0
    elif current_model.split(".")[-1] == "hdf5" or current_model.split(
            ".")[-1] == "h5":
        run_model = 1
    else:
        print("input model format error !")
        return

    faceboxes = mark_detector.extract_cnn_facebox(frame)
    print(faceboxes)

    if faceboxes is not None:
        facebox = faceboxes[0]

        # Detect landmarks from image of 64X64 with grayscale.
        face_img = frame[facebox[1]:facebox[3], facebox[0]:facebox[2]]
        cv2.rectangle(frame, (facebox[0], facebox[1]),
                      (facebox[2], facebox[3]), (0, 255, 0), 2)
        face_img = cv2.resize(face_img, (CNN_INPUT_SIZE, CNN_INPUT_SIZE))
        face_img = cv2.cvtColor(face_img, cv2.COLOR_BGR2GRAY)
        face_img0 = face_img.reshape(1, CNN_INPUT_SIZE, CNN_INPUT_SIZE, 1)

        if run_model == 1:
            marks = mark_detector.detect_marks_keras(face_img0)
        else:
            marks = mark_detector.detect_marks_tensor(face_img0, 'input_2:0',
                                                      'output/BiasAdd:0')
        # marks *= 255
        marks *= facebox[2] - facebox[0]
        marks[:, 0] += facebox[0]
        marks[:, 1] += facebox[1]
        # Draw Predicted Landmarks
        mark_detector.draw_marks(frame, marks, color=(255, 255, 255), thick=2)

    # loop over the frames from the video stream
    while True:
        start = cv2.getTickCount()

        # frame = cv2.resize(frame, (750,750))
        # frame = cv2.flip(frame, 1)

        # fps_time = (cv2.getTickCount() - start)/cv2.getTickFrequency()
        # cv2.putText(frame, '%.1ffps'%(1/fps_time), (frame.shape[1]-65,15), cv2.FONT_HERSHEY_DUPLEX, 0.5, (0,255,0))
        # show the frame
        cv2.imshow("face landmarks", frame)
        # writer.write(frame)
        key = cv2.waitKey(1)

        # if the `q` key was pressed, break from the loop
        if key == ord("q") or key == 0xFF:
            break

    # do a bit of cleanup
    cv2.destroyAllWindows()
Пример #3
0
def single_main():
    """MAIN"""
    cam = cv2.VideoCapture(VIDEO_PATH)
    _, sample_frame = cam.read()

    # Load Landmark detector
    mark_detector = MarkDetector()

    # Setup process and queues for multiprocessing
    img_queue = Queue()
    box_queue = Queue()
    img_queue.put(sample_frame)
    box_process = Process(target=get_face,
                          args=(
                              mark_detector,
                              img_queue,
                              box_queue,
                          ))
    box_process.start()

    while True:
        frame_got, frame = cam.read()
        if frame_got is False:
            break
        img_queue.put(frame)
        start = cv2.getTickCount()

        # Get face from box queue.
        faceboxes = box_queue.get()
        if faceboxes is not None:
            for facebox in faceboxes:
                # if facebox is not None:
                # Detect landmarks from image of 64x64.
                face_img = frame[facebox[1]:facebox[3], facebox[0]:facebox[2]]
                # cv2.rectangle(frame, (facebox[0], facebox[1]), (facebox[2], facebox[3]), (0, 255, 0), 2)
                face_img = cv2.resize(face_img,
                                      (CNN_INPUT_SIZE, CNN_INPUT_SIZE))
                face_img = cv2.cvtColor(face_img, cv2.COLOR_BGR2GRAY)
                face_img0 = face_img.reshape(1, CNN_INPUT_SIZE, CNN_INPUT_SIZE,
                                             1)
                marks = mark_detector.detect_marks_keras(
                    face_img0)  # landmark predictor
                marks *= facebox[2] - facebox[0]
                marks[:, 0] += facebox[0]
                marks[:, 1] += facebox[1]

                # Draw Predicted Landmarks
                mark_detector.draw_marks(frame, marks, color=(255, 255, 255))

        fps_time = (cv2.getTickCount() - start) / cv2.getTickFrequency()
        cv2.putText(frame, '%.1ffps' % (1 / fps_time),
                    (frame.shape[1] - 65, 15), cv2.FONT_HERSHEY_DUPLEX, 0.5,
                    (127, 127, 127))
        # Show preview.
        cv2.imshow("Preview", frame)
        if cv2.waitKey(10) == ord('q'):
            break

    # Clean up the multiprocessing process.
    box_process.terminate()
    box_process.join()

    # out.release()
    cam.release()
    cv2.destroyAllWindows()