Welcome to RDK X5 Robot repository
3. Color recognition
Color recognition
3.1 Introduction to gameplay
This course mainly uses the IMX219 camera to obtain the camera's image, and analyzes the image through the OPENCV library. It can frame red, green, blue and yellow objects and display the names of the corresponding colors.
3.2 Core content analysis
The HSV_Config file is a library file used to process the color recognition of the incoming image.
Color_HSV defines the HSV value of the color. Due to the color difference and light influence of the object color, if the recognition effect of a certain color is inaccurate, you can adjust the course according to the color HSV value. After adjusting the best effect, record the data and update the HSV value of the corresponding color.
xxxxxxxxxxupdate_hsv = HSV_Config.update_hsv()Color_HSV = {"Red" : ((0, 70, 72), (7, 255, 255)),"Green" : ((54, 109, 78), (77, 255, 255)),"Blue" : ((92, 100, 62), (121, 251, 255)),"Yellow": ((26, 100, 91), (32, 255, 255))}
The program function for processing the camera image, passing the image read from the camera to the update_hsv object for processing and outputting the colors in the current image, boxing out the corresponding colors and displaying the color names, and finally transmitting them through the image control for display.
xxxxxxxxxx m_fps = m_fps + 1 fps = m_fps / (time.time() - t_start) if (time.time() - t_start) >= 2: m_fps = fps t_start = time.time() - 1 cv2.putText(frame, "FPS " + str(int(fps)), (10,20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,255), 1) # The image is transmitted to the display component image_widget.value = bgr8_to_jpeg(frame)# time.sleep(.01)3.3 How to play
Open the jupyterLab client and find the code path:
xxxxxxxxxx/home/sunrise/sunriseRobot/Samples/2_AI/03_color_recogniton/color_recognition.ipynb
Click to run all cells, then pull to the bottom to see the generated controls.

The left side shows the image captured by the camera, and the Close_Camera on the right is used to close the camera and program process. Put a red, green, blue or yellow object in front of the camera, and the image will automatically frame the corresponding color block and display the name of the color.

Finally, click the Close_Camera button to close the camera.