Welcome to DOFBOT SE repository
12.USB camera display
USB Camera Display
12.1 Camera Reading
capture=cv.VideoCapture(0)
Parameter meaning:
The parameter in VideoCapture() is 0, which means to open the laptop's built-in camera. If the parameter is a video file path, it opens the video, such as cap = cv2.VideoCapture("../test.avi")
12.2 Display Camera Video
ret,img = frame.read()
Return value meaning:
ret: ret is a bool value that indicates whether the correct frame was read back.
img: Image data for each frame.
12.3 Code and Actual Effect Display
Code path: /home/yahboom/Dofbot/6.AI_Visual/0.Camera Driver Tutorial.ipynb
Main code:
ximport cv2import ipywidgets.widgets as widgetsimport threadingimport timeimage_widget = widgets.Image(format='jpeg', width=600, height=500) # Set camera display componentdisplay(image_widget) # Display camera componentxxxxxxxxxx#bgr8 to jpeg formatimport enumimport cv2def bgr8_to_jpeg(value, quality=75): return bytes(cv2.imencode('.jpg', value)[1])xxxxxxxxxximage = cv2.VideoCapture(0,cv2.CAP_V4L2) # Open cameraret, frame = image.read()image_widget.value = bgr8_to_jpeg(frame)After running the code block, the USB camera feed will be displayed.
