Welcome to DOFBOT SE repository
5.Color Recognition with Voice Broadcast
Color Recognition Voice Broadcast
1. Function Description
Use the voice recognition and broadcast module to broadcast the color recognized by the program.
2. Startup and Operation
2.1 Startup
Open the terminal and enter the following command to start:
xxxxxxxxxxpython3 ~/dofbot_voice/scripts/voice_color_identify.py
2.2 Operation Steps
After the program runs successfully, the program will call the camera to get the current image. Hold a color block and place the green square in the image within the block, as shown in the figure below:

Then the terminal will print "get color!". Say "Hi, yahboom" to the voice module. The voice module replies "I'm here" indicating successful wake-up. Then say "What color is this?", the voice module will broadcast the current color of the block. Assuming the currently recognized block is blue, the voice module will broadcast "This is blue". Note: If the color cannot be recognized, you may need to modify the HSV values in the program or adjust the ambient light.
3. Core Code Analysis
Source code path: ~/dofbot_voice/scripts/voice_color_identify.py
# Get the color of the block in the current imagedef get_color(img): H = [] color_name={} img = cv2.resize(img, (640, 480), ) # Convert color image to HSV HSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # Draw rectangle cv2.rectangle(img, (280, 180), (360, 260), (0, 255, 0), 2) # Extract H, S, V values of each row and column into the container for i in range(280, 360): for j in range(180, 260): H.append(HSV[j, i][0]) # Calculate the min and max of H, S, V H_min = min(H);H_max = max(H) # print(H_min,H_max) # Determine color if H_min >= 0 and H_max <= 10 or H_min >= 156 and H_max <= 180: color_name['name'] = 'red' elif H_min >= 21 and H_max <= 28: color_name['name'] = 'yellow' elif H_min >= 35 and H_max <= 78: color_name['name'] = 'green' elif H_min >= 100 and H_max <= 124: color_name['name'] = 'blue' return img, color_name# Get voice resultresult = mySpeech.speech_read()# If the recognized voice result is 60, broadcast the audio file reporting the block color result based on the value of color_name['name']if result == 60: if color_name['name'] == 'yellow': mySpeech.void_write(64) elif color_name['name'] == 'red': mySpeech.void_write(61) elif color_name['name'] == 'green': mySpeech.void_write(63) elif color_name['name'] == 'blue': mySpeech.void_write(62)