forked from NVIDIA-AI-IOT/jetcam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb_camera.py
41 lines (31 loc) · 1.43 KB
/
usb_camera.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from .camera import Camera
import atexit
import cv2
import numpy as np
import threading
import traitlets
class USBCamera(Camera):
capture_fps = traitlets.Integer(default_value=30)
capture_width = traitlets.Integer(default_value=640)
capture_height = traitlets.Integer(default_value=480)
capture_device = traitlets.Integer(default_value=0)
def __init__(self, *args, **kwargs):
super(USBCamera, self).__init__(*args, **kwargs)
try:
self.cap = cv2.VideoCapture(self._gst_str(), cv2.CAP_GSTREAMER)
re , image = self.cap.read()
if not re:
raise RuntimeError('Could not read image from camera.')
except:
raise RuntimeError(
'Could not initialize camera. Please see error trace.')
atexit.register(self.cap.release)
def _gst_str(self):
return 'v4l2src device=/dev/video{} ! video/x-raw, width=(int){}, height=(int){}, framerate=(fraction){}/1 ! videoconvert ! video/x-raw, format=(string)BGR ! appsink'.format(self.capture_device, self.capture_width, self.capture_height, self.capture_fps)
def _read(self):
re, image = self.cap.read()
if re:
image_resized = cv2.resize(image,(int(self.width),int(self.height)))
return image_resized
else:
raise RuntimeError('Could not read image from camera')