Welcome to DOFBOT-JetsonNANO repository
13.USB camera display
USB Camera Display
1. Camera Reading
capture=cv.VideoCapture(0)
Parameter meaning:
The parameter in VideoCapture() is 0, which means opening the notebook's built-in camera. If the parameter is a video file path, it opens the video, such as cap = cv2.VideoCapture("../test.avi")
2. Display Camera Video
ret,img = frame.read()
Return value meaning:
ret: ret is a boolean value, used to determine whether a correct frame has been read back
img: image data of each frame
3. Code and Actual Effect Display
Start Docker
After entering the main board desktop, open a terminal and run the following command to start the corresponding Dofbot container:
./Docker_Ros.sh
Then enter the following command in the Docker terminal to start jupyter-lab,
xxxxxxxxxxcdjupyter-lab --allow-root
Access Jupyter Lab within Docker:
xxxxxxxxxxIP:9999 // Example: 192.168.1.11:9999
Program path: /root/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 blocks, the image captured by the USB camera can be displayed.
