Esempio n. 1
0
def getCapture(cap):  # 반복적으로 화면 캡쳐를 얻는 함수
    # 로컬에 화면 캡쳐 이미지를 저장함
    camera = CSI_Camera()
    camera.create_gstreamer_pipeline(sensor_id=0,
                                     sensor_mode=2,
                                     framerate=30,
                                     flip_method=0,
                                     display_height=720,
                                     display_width=1280)
    camera.open(camera.gstreamer_pipeline)
    camera.start()
    cv2.namedWindow("Sticker Solution", cv2.WINDOW_AUTOSIZE)

    if not camera.video_capture.isOpened():
        print("Unable to open camera")
        SystemExit(0)

    try:
        camera.start_counting_fps()
        while cv2.getWindowProperty("Sticker Solution", 0) >= 0:
            _, img = camera.read()
            cv2.imwrite("images/" + str(cap) + ".jpg", img)
            cv2.imshow("Sticker Solution", img)
            camera.frames_displayed += 1
            cap = cap + 1
            if (cv2.waitKey(5) & 0xFF) == 27: break
    finally:
        camera.stop()
        camera.release()
        cv2.destroyAllWindows()
Esempio n. 2
0
def face_detect():
    face_cascade = cv2.CascadeClassifier(
        "/usr/share/opencv4/haarcascades/haarcascade_frontalface_default.xml")
    eye_cascade = cv2.CascadeClassifier(
        "/usr/share/opencv4/haarcascades/haarcascade_eye.xml")
    left_camera = CSI_Camera()
    left_camera.create_gstreamer_pipeline(
        sensor_id=0,
        sensor_mode=SENSOR_MODE_720,
        framerate=30,
        flip_method=0,
        display_height=DISPLAY_HEIGHT,
        display_width=DISPLAY_WIDTH,
    )
    left_camera.open(left_camera.gstreamer_pipeline)
    left_camera.start()
    cv2.namedWindow("Face Detect", cv2.WINDOW_AUTOSIZE)

    if (not left_camera.video_capture.isOpened()):
        # Cameras did not open, or no camera attached

        print("Unable to open any cameras")
        # TODO: Proper Cleanup
        SystemExit(0)
    try:
        # Start counting the number of frames read and displayed
        left_camera.start_counting_fps()
        while cv2.getWindowProperty("Face Detect", 0) >= 0:
            img = read_camera(left_camera, False)
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            faces = face_cascade.detectMultiScale(gray, 1.3, 5)

            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)
            if show_fps:
                draw_label(
                    img, "Frames Displayed (PS): " +
                    str(left_camera.last_frames_displayed), (10, 20))
                draw_label(
                    img,
                    "Frames Read (PS): " + str(left_camera.last_frames_read),
                    (10, 40))
            cv2.imshow("Face Detect", img)
            left_camera.frames_displayed += 1
            keyCode = cv2.waitKey(5) & 0xFF
            # Stop the program on the ESC key
            if keyCode == 27:
                break
    finally:
        left_camera.stop()
        left_camera.release()
        cv2.destroyAllWindows()
Esempio n. 3
0
def start_cameras():
    left_camera = CSI_Camera()
    left_camera.create_gstreamer_pipeline(
        sensor_id=0,
        sensor_mode=SENSOR_MODE_720,
        framerate=30,
        flip_method=0,
        display_height=DISPLAY_HEIGHT,
        display_width=DISPLAY_WIDTH,
    )
    left_camera.open(left_camera.gstreamer_pipeline)
    left_camera.start()

    right_camera = CSI_Camera()
    right_camera.create_gstreamer_pipeline(
        sensor_id=1,
        sensor_mode=SENSOR_MODE_720,
        framerate=30,
        flip_method=0,
        display_height=DISPLAY_HEIGHT,
        display_width=DISPLAY_WIDTH,
    )
    right_camera.open(right_camera.gstreamer_pipeline)
    right_camera.start()

    cv2.namedWindow("CSI Cameras", cv2.WINDOW_AUTOSIZE)

    if (not left_camera.video_capture.isOpened()
            or not right_camera.video_capture.isOpened()):
        # Cameras did not open, or no camera attached

        print("Unable to open any cameras")
        # TODO: Proper Cleanup
        SystemExit(0)
    try:
        # Start counting the number of frames read and displayed
        left_camera.start_counting_fps()
        right_camera.start_counting_fps()
        while cv2.getWindowProperty("CSI Cameras", 0) >= 0:
            left_image = read_camera(left_camera, show_fps)
            right_image = read_camera(right_camera, show_fps)
            # 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)
            left_camera.frames_displayed += 1
            right_camera.frames_displayed += 1
            # This also acts as a frame limiter
            # Stop the program on the ESC key
            if (cv2.waitKey(20) & 0xFF) == 27:
                break

    finally:
        left_camera.stop()
        left_camera.release()
        right_camera.stop()
        right_camera.release()
    cv2.destroyAllWindows()
Esempio n. 4
0
def lighter_detect():
    camera = CSI_Camera()
    camera.create_gstreamer_pipeline(
        sensor_id       = 0,
        sensor_mode     = SENSOR_MODE,
        framerate       = 30,
        flip_method     = 0,
        display_height  = DISPLAY_HEIGHT,
        display_width   = DISPLAY_WIDTH,
    )
    camera.open(camera.gstreamer_pipeline)
    camera.start()
    cv2.namedWindow("Lighter Test FPS", cv2.WINDOW_AUTOSIZE)

    if not camera.video_capture.isOpened():
        print("Unable to open camera")
        SystemExit(0)
    
    try:
        camera.start_counting_fps()
        while cv2.getWindowProperty("Lighter Test FPS", 0) >= 0:
            img = read_camera(camera)
            blob = cv2.dnn.blobFromImage(img, 0.00392, (446, 446), (0, 0, 0), True, crop=False)
            net.setInput(blob)
            outs = net.forward(output_layers)
            
            confidences = []
            boxes = []

            for out in outs:
                for detection in out:
                    scores = detection[5:]
                    class_id = np.argmax(scores)
                    confidence = scores[class_id]
                    if confidence > 0.5 :
                        center_x = int(detection[0] * DISPLAY_WIDTH)
                        center_y = int(detection[1] * DISPLAY_HEIGHT)
                        w = int(detection[2] * DISPLAY_WIDTH)
                        h = int(detection[3] * DISPLAY_HEIGHT)
                        x = int(center_x - w / 2)
                        y = int(center_y - h / 2)
                        if class_id == 0: cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 2, cv2.LINE_AA)
                        elif class_id == 1: cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2, cv2.LINE_AA)
                        else: cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2, cv2.LINE_AA)
                        cv2.putText(img, classes[class_id], (x, y+30), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
                        boxes.append([x, y, w, h])
                        confidences.append(float(confidence))

            cv2.imshow("Lighter Test FPS", img)
            camera.frames_displayed += 1
            if (cv2.waitKey(5) & 0xFF) == 27: break # ESC key Stops program
    finally:
        camera.stop()
        camera.release()
        cv2.destroyAllWindows()
Esempio n. 5
0
def getCapture():
    camera = CSI_Camera()
    camera.create_gstreamer_pipeline(sensor_id=0,
                                     sensor_mode=0,
                                     framerate=30,
                                     flip_method=0,
                                     display_height=DISPLAY_HEIGHT,
                                     display_width=DISPLAY_WIDTH)
    camera.open(camera.gstreamer_pipeline)
    camera.start()
    cv2.namedWindow("Gas Test Display", cv2.WINDOW_AUTOSIZE)

    if not camera.video_capture.isOpened():
        print("Unable to open camera")
        SystemExit(0)

    try:
        camera.start_counting_fps()
        image_num = 1
        gpu_frame = cv2.cuda_GpuMat()

        while cv2.getWindowProperty("Gas Test Display", 0) >= 0:
            _, original_img = camera.read()
            gpu_frame.upload(original_img)

            # Apply grayscale and sobel filter
            #temp = cv2.cvtColor(original_img, cv2.COLOR_RGB2GRAY)
            #img = np.zeros([DISPLAY_HEIGHT, DISPLAY_WIDTH, 3])
            #for i in range(3) : img[:, :, i] = temp[:, :]
            #img = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5, scale=0.15)
            #img = cv2.convertScaleAbs(img)
            # Apply Sharpnening filter
            #img = getSharpening(img, 2)

            gpu_frame.download(original_img)
            cv2.imshow("Gas Test Display", original_img)
            camera.frames_displayed += 1

            if (cv2.waitKey(25) & 0xFF) == 27:
                break  # Quit if you pressed ESC.
            #if (cv2.waitKey(25) & 0xFF) == 13:          # Capture if you pressed ENTER.
            #   cv2.imwrite('/home/jyjun/test' + str(image_num) + '.png', img)
            #  print("<Image captured successfully!>")
            # image_num = image_num + 1
    finally:
        camera.stop()
        camera.release()
        cv2.destroyAllWindows()
Esempio n. 6
0
def getCapture(cap):  # 실시간으로 화면을 캡쳐 후 로컬저장함
    camera = CSI_Camera()
    camera.create_gstreamer_pipeline(
        sensor_id=0,
        sensor_mode=
        2,  # [TODO] Sensor_mode로 설정하지 말고 해상도로 설정하도록 해야할 것 같은데? 4032x3040 (Ratio: 4:3)
        framerate=30,
        flip_method=0,
        display_height=720,  # [TODO] 이것도 4:3으로 맞춰주는게 가장 best인데...
        display_width=1280)
    camera.open(camera.gstreamer_pipeline)
    camera.start()
    cv2.namedWindow("Gas Solution", cv2.WINDOW_AUTOSIZE)

    if not camera.video_capture.isOpened():
        print("Unable to open camera")
        SystemExit(0)

    try:
        camera.start_counting_fps()
        while cv2.getWindowProperty("Gas Solution", 0) >= 0:
            _, img_ori = camera.read()

            temp = cv2.cvtColor(img_ori, cv2.COLOR_RGB2GRAY)

            img = np.zeros([720, 720, 3])

            for i in range(3):
                img[:, :, i] = temp[:, 280:1000]
            img = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5, scale=0.15)
            img = cv2.convertScaleAbs(img)

            cv2.imwrite(
                "images/" + str(cap) + ".jpg",
                img)  # [중요!] 이미지를 로컬에 저장함! TODO. 이거 저장하지말고 바로 판단하도록 바꿔야함
            #cv2.imshow("Gas Solution", img)                # [TODO]  이걸 굳이 띄울 필요가 있나?
            time.sleep(0.33)  # 의도적으로 3프레임으로 만들려고 0.33초 sleep
            camera.frames_displayed += 1
            cap = cap + 1
            if (cv2.waitKey(5) & 0xFF) == 27: break
    finally:
        camera.stop()
        camera.release()
        cv2.destroyAllWindows()
Esempio n. 7
0
def getCapture():  # 실시간으로 화면을 캡쳐 후 로컬저장함
    global cap
    camera = CSI_Camera()
    camera.create_gstreamer_pipeline(sensor_id=0,
                                     sensor_mode=2,
                                     framerate=15,
                                     flip_method=0,
                                     display_height=DISPLAY_HEIGHT,
                                     display_width=DISPLAY_WIDTH)
    camera.open(camera.gstreamer_pipeline)
    camera.start()
    cv2.namedWindow("Gas Solution", cv2.WINDOW_AUTOSIZE)

    if not camera.video_capture.isOpened():
        print("Unable to open camera")
        SystemExit(0)

    try:
        camera.start_counting_fps()
        while cv2.getWindowProperty("Gas Solution", 0) >= 0:
            _, img_ori = camera.read()

            temp = cv2.cvtColor(img_ori, cv2.COLOR_RGB2GRAY)

            img = np.zeros([DISPLAY_HEIGHT, DISPLAY_HEIGHT, 3])

            diff_half = (DISPLAY_WIDTH - DISPLAY_HEIGHT) // 2
            for i in range(3):
                img[:, :, i] = temp[:, diff_half:DISPLAY_WIDTH - diff_half]
            img = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5, scale=0.15)
            img = cv2.convertScaleAbs(img)
            cv2.imwrite("images/" + str(cap) + ".jpg", img)
            cap = cap + 1
            cv2.imshow("Gas Solution", img)
            camera.frames_displayed += 1
            if (cv2.waitKey(5) & 0xFF) == 27: break
    finally:
        camera.stop()
        camera.release()
        cv2.destroyAllWindows()
    cv2.createTrackbar("colROI", "DISPLAY", 0, 200, onChange)
    cv2.setTrackbarPos("rowROI", "DISPLAY", 0)
    cv2.setTrackbarPos("colROI", "DISPLAY", 100)

    out = cv2.VideoWriter('stickerVideo.mp4', cv2.VideoWriter_fourcc(*'XVID'),
                          10.0, (DISPLAY_WIDTH, DISPLAY_HEIGHT), True)

    if not camera.video_capture.isOpened():
        print("Unable to open camera")
        SystemExit(0)
    try:
        cnt = 0
        setFlag = False
        upper_lines = []
        lower_lines = []
        camera.start_counting_fps()
        gpu_frame = cv2.cuda_GpuMat()

        while cv2.getWindowProperty("DISPLAY", 0) >= 0:
            # Read frame and initialze.
            _, img = camera.read()
            rowpos = cv2.getTrackbarPos("rowROI", "DISPLAY")
            colpos = cv2.getTrackbarPos("colROI", "DISPLAY")
            img[:UPPER_NOT_ROI + rowpos, :] = img[LOWER_NOT_ROI +
                                                  rowpos:, :] = 0
            img[:, :colpos] = img[:, DISPLAY_WIDTH - colpos:] = 0

            cv2.putText(img, "FPS: " + str(camera.last_frames_displayed),
                        (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255),
                        2, cv2.LINE_4)
            out.write(img)
Esempio n. 9
0
def face_detect(sensor_mode=S_MODE_3_1280_720_60,
                dispW=DISP_W_M3_M4_one_half,
                dispH=DISP_H_M3_M4_one_half):

    face_cascade = cv2.CascadeClassifier(
        "/usr/share/opencv4/haarcascades/haarcascade_frontalface_default.xml")
    eye_cascade = cv2.CascadeClassifier(
        "/usr/share/opencv4/haarcascades/haarcascade_eye.xml")
    left_camera = CSI_Camera()

    # WS mod: IMPORTANT use lowest fps = 21 as default, otherwise modes 0 or 1 crash: reboot required
    left_camera.create_gstreamer_pipeline(
        sensor_id=0,
        sensor_mode=sensor_mode,
        framerate=21,
        flip_method=0,
        display_height=dispH,
        display_width=dispW,
    )
    left_camera.open(left_camera.gstreamer_pipeline)
    left_camera.start()
    txt = "Face Detect: Sensor Mode {}, Display {} x {}".format(
        sensor_mode, dispW, dispH)  # WS mod
    cv2.namedWindow(txt, cv2.WINDOW_AUTOSIZE)

    if (not left_camera.video_capture.isOpened()):
        # Cameras did not open, or no camera attached

        print("Unable to open any cameras")
        # TODO: Proper Cleanup
        SystemExit(0)
    try:
        # Start counting the number of frames read and displayed
        left_camera.start_counting_fps()
        while cv2.getWindowProperty(txt, 0) >= 0:
            img = read_camera(left_camera, False)
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            faces = face_cascade.detectMultiScale(gray, 1.3, 5)

            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)
            if show_fps:
                draw_label(
                    img, "Frames Displayed (PS): " +
                    str(left_camera.last_frames_displayed), (10, 20))
                draw_label(
                    img,
                    "Frames Read (PS): " + str(left_camera.last_frames_read),
                    (10, 40))
            cv2.imshow(txt, img)
            left_camera.frames_displayed += 1
            keyCode = cv2.waitKey(5) & 0xFF
            # Stop the program on the ESC key
            if keyCode == 27:
                break
    finally:
        left_camera.stop()
        left_camera.release()
        cv2.destroyAllWindows()