def show_camera():
    # To flip the image, modify the flip_method parameter (0 and 2 are the most common)
    print(gstreamer_pipeline(flip_method=0))
    cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
    if cap.isOpened():
        window_handle = cv2.namedWindow("CSI Camera", cv2.WINDOW_AUTOSIZE)
        # Window
        while cv2.getWindowProperty("CSI Camera", 0) >= 0:
            with Timer() as context_time:
                ret_val, img = cap.read()
                print(context_time.elapsed)
                cv2.imshow("CSI Camera", img)
                print(context_time.elapsed)

                # This also acts as
                keyCode = cv2.waitKey(20) & 0xFF
            print(context_time.elapsed)
            print("---")
            # Stop the program on the ESC key
            if keyCode == 27:
                break
        cap.release()
        cv2.destroyAllWindows()
    else:
        print("Unable to open camera")
Exemple #2
0
def face_detect():
    global frames_displayed
    global fps_timer
    face_cascade = cv2.CascadeClassifier(
        "/usr/share/opencv4/haarcascades/haarcascade_frontalface_default.xml")
    eye_cascade = cv2.CascadeClassifier(
        "/usr/share/opencv4/haarcascades/haarcascade_eye.xml")
    cap = cv2.VideoCapture(gstreamer_pipeline(), cv2.CAP_GSTREAMER)
    if cap.isOpened():
        try:
            cv2.namedWindow("Face Detect", cv2.WINDOW_AUTOSIZE)
            # Setup our Frames per second counter
            start_counting_fps()
            while cv2.getWindowProperty("Face Detect", 0) >= 0:
                with Timer() as measure:
                    ret, img = cap.read()
                    print("---")
                    print("Read Cam:" + str(measure.elapsed))
                    before = measure.elapsed
                    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
                    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
                    print("detectMultipleScale: " +
                          str(measure.elapsed - before))
                    before = measure.elapsed
                    for (x, y, w, h) in faces:
                        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0),
                                      2)
                        roi_gray = gray[y:y + h, x:x + w]
                        roi_color = img[y:y + h, x:x + w]
                        eyes = eye_cascade.detectMultiScale(roi_gray)
                        for (ex, ey, ew, eh) in eyes:
                            cv2.rectangle(roi_color, (ex, ey),
                                          (ex + ew, ey + eh), (0, 255, 0), 2)
                    print("eyeCascade: " + str(measure.elapsed - before))
                    print(measure.elapsed)
                    cv2.imshow("Face Detect", img)

                print("Elapsed time: " + str(measure.elapsed))
                frames_displayed = frames_displayed + 1
                keyCode = cv2.waitKey(10) & 0xFF
                # Stop the program on the ESC key
                if keyCode == 27:
                    break
        finally:
            fps_timer.cancel()
            fps_timer.join()

        cap.release()
        # Kill the fps timer
        cv2.destroyAllWindows()
    else:
        print("Unable to open camera")
def show_camera():
    # To flip the image, modify the flip_method parameter (0 and 2 are the most common)
    print(gstreamer_pipeline(flip_method=0))
    left_cap = cv2.VideoCapture(
        gstreamer_pipeline(flip_method=0,
                           display_width=960,
                           display_height=540,
                           framerate=30),
        cv2.CAP_GSTREAMER,
    )
    right_cap = cv2.VideoCapture(
        gstreamer_pipeline(
            flip_method=0,
            sensor_id=1,
            display_width=960,
            display_height=540,
            framerate=30,
        ),
        cv2.CAP_GSTREAMER,
    )
    if left_cap.isOpened():
        cv2.namedWindow("CSI Camera", cv2.WINDOW_AUTOSIZE)
        # Window
        while cv2.getWindowProperty("CSI Camera", 0) >= 0:
            with Timer() as context_time:
                ret_val, left_image = left_cap.read()
                ret_val, right_image = right_cap.read()
                # print(context_time.elapsed)
                # We place both images side by side to show in the window
                camera_images = np.hstack((left_image, right_image))
                cv2.imshow("CSI Cameras", camera_images)
                # cv2.imshow("CSI Camera", left_image)
                # print(context_time.elapsed)

                # This also acts as
                keyCode = cv2.waitKey(20) & 0xFF
            # print(context_time.elapsed)
            # print("---")
            # Stop the program on the ESC key
            if keyCode == 27:
                break
        left_cap.release()
        right_cap.release()
        cv2.destroyAllWindows()
    else:
        print("Unable to open camera")