Index>Robot Kit>DarkPaw Spider Robot Kit for RPi>Lesson 3 Introducing Color Detection Function

Lesson 3 Introducing Color Detection Function

2373

Lesson 3 Introducing Color Detection Function

In this tutorial we'll learn how to detect colors.  

3.1 Color Detection & Color Space

Color Space

Color space refers to a method of organizing colors. With the help of color space and tests of physical devices, we can get a certain analog and digital representation of colors. Color space can be defined by random colors. For example, Pantone series uses a group of specific colors as sample, and define each color with a name and code. Or, define it mathematically like Adobe RGB, sRGB, etc.

RGB Color Space

RGB uses additive colors as it describes a specific proportion by which various kinds of "light" produces colors. Starting from black, light keeps mixing to generate different colors. RGB means the value of red, green, and blue; RGBA produces a transparent color based on RGB with the alpha channel.

Common color spaces based on the RGB mode include sRGB, Adobe RGB and Adobe Wide Gamut RGB. 

HSV Color Space

HSV, or hue, saturation, and value, also referred to as HSB (B for brightness), is commonly used by artists because it's more understandable to describe color by hue and saturation compared with terms of additive or subtractive color mixing. HSV is a variant of RGB color space and closely related in content and color standards as it derives from RGB.

HSV color space is used for color detection with OpenCV since it's less effected by ambient light and brings more accurate detection results. Besides, it's easy to define color range with HSV. During color detection, the code is to recognize some range of colors instead of some color. Therefore, the HSV color space is used for color detection since it is similar to the human perception of color.

3.2 Running the Color Detection Function

The web terminal and GUI are not connected. Therefore, to control the robot via GUI, you need to manually run server.py on Raspberry Pi. (similar to manually running webserver.py, the only difference is the server.py program)

1. Power on the robot. It may take 30-50s.

2. After it's booted, remotely log into the Raspberry pi. Terminate the auto-run program webServer.py, and run the server.py program.

Terminate the auto-run program webServer.py:

sudo killall python3

wps315.jpg 

Run the server.py program:

sudo python3 adeept_darkpaw/server/server.py

wps316.jpg 

 

3. After the server.py run successfully on the Raspberry Pi, run GUI.py on Windows.  

Run GUI.py in cmd. If you right click on the GUI.py file in the folder, there may be an error message.

Access the folder of the robot program:

wps317.jpg 

Run the GUI.py program (run it after accessing the client folder, or there may be an error).

Python GUI.py

wps318.jpg 

The GUI interface will appear after run successfully.

wps319.jpg 

4. Enter the IP address of the Raspberry Pi in the GUI on PC, click Connect, and you can control the robot now. For instance, enter 192.168.3.242.

wps320.jpg 

5. Click the FindColor button. The DarkPaw robot then will detect objects in yellow and mark them accordingly.

6. Click FindColor again to stop detecting.

7. To change the color to be detected, you can change the code of Line 162 and 163 in FPV.py.    

        self.colorUpper = (44, 255, 255)

        self.colorLower = (24, 100, 100)

(44, 255, 255) and (24, 100, 100) represent the maximum and minimum values of HSV for yellow in (H, S, V). You can change them for your desired color. self.colorUpper and self.colorLower are the upper and lower range values respectively.

For example, to detect green objects, you can change it to:

        self.colorUpper = (77, 255, 255)

        self.colorLower = (35, 43, 36)

 

3.3 HSV Color Component Range in OpenCV

Generally, the H (hue) value of HSV color space ranges from 0-360, yet 0-180 in OpenCV.

HSV\Color

Black

gray

white

red

orange

yellow

green

cyan

blue

purple

H_min

0

0

0

0

156

11

26

35

78

100

125

H_max

180

180

180

10

180

25

34

77

99

124

155

S_min

0

0

0

43

43

43

43

43

43

43

S_max

255

43

30

255

255

255

255

255

255

255

V_min

0

46

221

46

46

46

46

46

46

46

V_max

46

220

255

255

255

255

255

255

255

255