Welcome to Muto S2 Robot repository
12.Camera driver
12.Camera driver
12.Camera driver1.Experimental goals2.Experiment preparation3.Experiment procedure4.Experiment summary
1.Experimental goals
This course mainly uses the motherboard's camera to obtain the camera's picture and display it on the JupyterLab control.
2.Experiment preparation
Check the device number of the USB camera, as shown in the figure below, get the device number /dev/video0
ls /dev/video*

3.Experiment procedure
Open the jupyterLab client and find the code path:
xxxxxxxxxxmuto/Samples/Control/12.camera.ipynb
Import the opencv library and jupyter lab display control.
xxxxxxxxxximport cv2import ipywidgets.widgets as widgets
Open the camera. The default configuration device is /dev/video0, the display resolution is 640*480, and the frame rate is 30 frames. If the device number of the camera in the system is not /dev/video0, please modify the configuration information according to the actual device number.
xxxxxxxxxximage_widget = widgets.Image(format='jpeg', width=640, height=480)image = cv2.VideoCapture(0)image.set(3, 640)image.set(4, 480)image.set(5, 30)
Read a frame of image from the camera, ret=True if the reading is successful, ret=False if the reading fails, frame represents the currently read image, calculate the FPS, and display the image on the control.
xwhile g_camera.isOpened():ret, frame = g_camera.read()if not ret:print("Camera Read Fail")breakm_fps = m_fps + 1fps = m_fps / (time.time() - t_start)if (time.time() - t_start) >= 2:m_fps = fpst_start = time.time() - 1text="FPS:" + str(int(fps))cv2.putText(frame, text, (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255), 1)image_widget.value = bgr8_to_jpeg(frame)

If you do not need to use it, please release the camera to avoid errors caused by other routines accessing the camera at the same time. Select the cell displaying the camera and click the stop button to automatically stop and call the camera release program.

xxxxxxxxxxg_camera.release()
4.Experiment summary
This time, the JupyterLab control is used to display the camera screen. If the default device /dev/video0 device number cannot be driven, please check the device number of the current system, or replug and unplug the camera wiring.