Python cv2.CAP_GSTREAMER Examples

The following are 13 code examples of cv2.CAP_GSTREAMER(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cv2 , or try the search function .
Example #1
Source File: camera.py    From tensorrt_demos with MIT License 7 votes vote down vote up
def open_cam_rtsp(uri, width, height, latency):
    """Open an RTSP URI (IP CAM)."""
    gst_elements = str(subprocess.check_output('gst-inspect-1.0'))
    if 'omxh264dec' in gst_elements:
        # Use hardware H.264 decoder on Jetson platforms
        gst_str = ('rtspsrc location={} latency={} ! '
                   'rtph264depay ! h264parse ! omxh264dec ! '
                   'nvvidconv ! '
                   'video/x-raw, width=(int){}, height=(int){}, '
                   'format=(string)BGRx ! videoconvert ! '
                   'appsink').format(uri, latency, width, height)
    elif 'avdec_h264' in gst_elements:
        # Otherwise try to use the software decoder 'avdec_h264'
        # NOTE: in case resizing images is necessary, try adding
        #       a 'videoscale' into the pipeline
        gst_str = ('rtspsrc location={} latency={} ! '
                   'rtph264depay ! h264parse ! avdec_h264 ! '
                   'videoconvert ! appsink').format(uri, latency)
    else:
        raise RuntimeError('H.264 decoder not found!')
    return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER) 
Example #2
Source File: simple-camera.py    From ai-smarthome with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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:
            ret_val, img = cap.read();
            cv2.imshow('CSI Camera',img)
	    # This also acts as 
            keyCode = cv2.waitKey(30) & 0xff
            # Stop the program on the ESC key
            if keyCode == 27:
               break
        cap.release()
        cv2.destroyAllWindows()
    else:
        print('Unable to open camera') 
Example #3
Source File: camera_jetson.py    From object-detection with MIT License 5 votes vote down vote up
def frames():
        camera = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
        if not camera.isOpened():
            raise RuntimeError('Could not start camera.')

        while True:
            # read current frame
            _, img = camera.read()

            yield img 
Example #4
Source File: camera.py    From tensorrt_demos with MIT License 5 votes vote down vote up
def open_cam_usb(dev, width, height):
    """Open a USB webcam."""
    if USB_GSTREAMER:
        gst_str = ('v4l2src device=/dev/video{} ! '
                   'video/x-raw, width=(int){}, height=(int){} ! '
                   'videoconvert ! appsink').format(dev, width, height)
        return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)
    else:
        return cv2.VideoCapture(dev) 
Example #5
Source File: camera.py    From tensorrt_demos with MIT License 5 votes vote down vote up
def open_cam_onboard(width, height):
    """Open the Jetson onboard camera."""
    gst_elements = str(subprocess.check_output('gst-inspect-1.0'))
    if 'nvcamerasrc' in gst_elements:
        # On versions of L4T prior to 28.1, you might need to add
        # 'flip-method=2' into gst_str below.
        gst_str = ('nvcamerasrc ! '
                   'video/x-raw(memory:NVMM), '
                   'width=(int)2592, height=(int)1458, '
                   'format=(string)I420, framerate=(fraction)30/1 ! '
                   'nvvidconv ! '
                   'video/x-raw, width=(int){}, height=(int){}, '
                   'format=(string)BGRx ! '
                   'videoconvert ! appsink').format(width, height)
    elif 'nvarguscamerasrc' in gst_elements:
        gst_str = ('nvarguscamerasrc ! '
                   'video/x-raw(memory:NVMM), '
                   'width=(int)1920, height=(int)1080, '
                   'format=(string)NV12, framerate=(fraction)30/1 ! '
                   'nvvidconv flip-method=2 ! '
                   'video/x-raw, width=(int){}, height=(int){}, '
                   'format=(string)BGRx ! '
                   'videoconvert ! appsink').format(width, height)
    else:
        raise RuntimeError('onboard camera source not found!')
    return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER) 
Example #6
Source File: camera.py    From homesecurity with MIT License 5 votes vote down vote up
def open_cam_rtsp(uri, width, height, latency):
    """Open an RTSP URI (IP CAM)."""
    gst_str = ('rtspsrc location={} latency={} ! '
               'rtph264depay ! h264parse ! omxh264dec ! '
               'nvvidconv ! '
               'video/x-raw, width=(int){}, height=(int){}, '
               'format=(string)BGRx ! videoconvert ! '
               'appsink').format(uri, latency, width, height)
    return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER) 
Example #7
Source File: camera.py    From homesecurity with MIT License 5 votes vote down vote up
def open_cam_usb(dev, width, height):
    """Open a USB webcam.

    We want to set width and height here, otherwise we could just do:
        return cv2.VideoCapture(dev)
    """
    gst_str = ('v4l2src device=/dev/video{} ! '
               'video/x-raw, width=(int){}, height=(int){} ! '
               'videoconvert ! appsink').format(dev, width, height)
    return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER) 
Example #8
Source File: camera.py    From homesecurity with MIT License 5 votes vote down vote up
def open_cam_onboard(width, height):
    """Open the Jetson onboard camera.

    On versions of L4T prior to 28.1, you might need to add
    'flip-method=2' into gst_str.
    """
    gst_str = ('nvcamerasrc ! '
               'video/x-raw(memory:NVMM), '
               'width=(int)2592, height=(int)1458, '
               'format=(string)I420, framerate=(fraction)30/1 ! '
               'nvvidconv ! '
               'video/x-raw, width=(int){}, height=(int){}, '
               'format=(string)BGRx ! videoconvert ! '
               'appsink').format(width, height)
    return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER) 
Example #9
Source File: lambda_function.py    From aws-builders-fair-projects with Apache License 2.0 5 votes vote down vote up
def open_usb_camera():
    gst_str = ("v4l2src device={} ! "
                "video/x-raw, width=(int){}, height=(int){}, framerate=(fraction)30/1 ! "
                "videoconvert !  video/x-raw, , format=(string)BGR ! appsink"
              ).format(
                VIDEO_DEVICE, VIDEO_WIDTH, VIDEO_HEIGHT
              )
    return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER) 
Example #10
Source File: create_train_data.py    From aws-builders-fair-projects with Apache License 2.0 5 votes vote down vote up
def open_cam_usb():
    # We want to set width and height here, otherwise we could just do:
    #     return cv2.VideoCapture(dev)
    gst_str = ("v4l2src device=/dev/video0 ! "
                "video/x-raw, width=(int){}, height=(int){}, framerate=(fraction)30/1 ! "
                "videoconvert !  video/x-raw, , format=(string)BGR ! appsink"
              ).format(WIDTH, HEIGHT)
    return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER) 
Example #11
Source File: jetson-cam.py    From ai-smarthome with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, mqttBroker, camera=None, topic="ha/camera/mqtt"):
        self.show = True
        self.broker = mqttBroker
        self.topic = topic
        self.send_frames = 0
        # To flip the image, modify the flip_method parameter (0 and 2 are the most common)
        print(gstreamer_pipeline(flip_method=0))
        self.cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
        # First frame is average...
        ret, self.avgframe = self.cap.read()
        self.connect("Jetson MQTT Camera") 
Example #12
Source File: camera_jetson.py    From object-detection with MIT License 4 votes vote down vote up
def ObjectTracking(self):
    detector = Detector()
    myiter = glob.iglob(os.path.join(IMAGE_FOLDER, '**', '*.jpg'),
                        recursive=True)
    newdict = reduce(lambda a, b: reduce_tracking(a,b), myiter, dict())
    startID = max(map(int, newdict.keys()), default=0) + 1
    ct = CentroidTracker(startID=startID)
    camera = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
    if not camera.isOpened():
        raise RuntimeError('Could not start camera.')

    try:
        while True:
            _, img = camera.read()
            boxes, confs, clss = detector.prediction(img, conf_th=0.8, conf_class=[1])
            img = detector.draw_boxes(img, boxes, confs, clss)
            previous_object_ID = ct.nextObjectID
            objects = ct.update(boxes)
            if len(boxes) > 0 and 1 in clss and previous_object_ID in list(objects.keys()):
                print("detected {} {} {} {}".format(ct.nextObjectID, confs, objects, boxes))

                # loop over the tracked objects
                for (objectID, centroid) in objects.items():
                    text = "ID {}".format(objectID)
                    cv2.putText(img, text, (centroid[0] - 10, centroid[1] - 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
                    cv2.circle(img, (centroid[0], centroid[1]), 4, (0, 255, 0), -1)

                day = datetime.now().strftime("%Y%m%d")
                directory = os.path.join(IMAGE_FOLDER, 'pi', day)
                if not os.path.exists(directory):
                    os.makedirs(directory)
                ids = "-".join(list([str(i) for i in objects.keys()]))
                hour = datetime.now().strftime("%H%M%S")
                filename_output = os.path.join(
                        directory, "{}_person_{}_.jpg".format(hour, ids)
                        )
                cv2.imwrite(filename_output, img)
            time.sleep(0.100)
    except KeyboardInterrupt:
        print('interrupted!')
        camera.release()
        print(type(objects))
        print(objects)
    except Exception as e:
        print('interrupted! by:')
        print(e)
        camera.release()
        print(type(objects))
        print(objects) 
Example #13
Source File: camera.py    From derplearning with MIT License 4 votes vote down vote up
def __connect(self):
        if self._cap is not None:
            self._cap.release()
            del self._cap
            self._cap = None
            time.sleep(1)

        device = "device=/dev/video%i" % self._config["index"]
        gst = None
        if self._config["mode"] == "video":
            gst = (
                "v4l2src %s"
                " ! video/x-raw,format=YUY2,width=%i,height=%i,framerate=%i/1 "
                " ! videoconvert ! appsink"
                % (device, self._config["width"], self._config["height"], self._config["fps"])
            )
        elif self._config["mode"] == "image":
            gst = (
                "v4l2src %s"
                " ! image/jpeg,width=%i,height=%i,framerate=%i/1"
                " ! jpegparse ! jpegdec ! videoconvert ! appsink"
                % (device, self._config["width"], self._config["height"], self._config["fps"])
            )
        elif self._config["mode"] == "csi":
            gst = (
                "nvarguscamerasrc sensor-id=%i"
                " ! video/x-raw(memory:NVMM),width=%i,height=%i,framerate=(fraction)%i/1,format=(string)NV12"
                " ! nvtee ! nvvidconv flip-method=%i"
                " ! video/x-raw,width=%i,height=%i,format=BGRx"
                " ! videoconvert ! appsink"
                % (
                    self._config["index"],
                    self._config["capture_width"],
                    self._config["capture_height"],
                    self._config["fps"],
                    self._config["flip_method"],
                    self._config["width"],
                    self._config["height"],
                )
            )

        print(gst)
        if gst is not None:
            self._cap = cv2.VideoCapture(gst, cv2.CAP_GSTREAMER)