JeVois Tutorials  1.8
JeVois Smart Embedded Machine Vision Tutorials
Share this page:
Tuning the color-based object tracker using a python graphical interface

The color-based object tracker of JeVois is a simple but fast and effective way of tracking objects defined by a specific color. The behavior of the module can be fine-tuned by setting ranges for the hue (color tint), saturation (color fade), and value (color brightness) of the objects to track. While this can be achieved by hand, by using the approach studied in Changing machine vision parameters, quickly one becomes tired of typing many setpar commands while searching for appropriate settings of the 6 color-related parameters (hue min and max, saturation min and max, and value min and max).

Here we write a simple graphical user interface in python to tune the parameters.

Hence, in this tutorial you will learn:

Getting started

Available parameters

Typing help in your serial terminal or inspecting the documentation of Object Tracker reveals several parameters. Here we will focus on the hrange, srange, and vrange parameters, which define the range of hue, saturation, and value that we want to track.

If you are not familiar with the HSV color space, check out this Wikipedia page.

Each of these three parameters is a range, that is, we can change them by specifying both the minimum and maximum values, separated by ..., for example, try:

setpar hrange 10...200

A more user-friendly way

We write a simple python script that creates some sliders that one can drag with the mouse. When the sliders are updated, we send the appropriate setpar command to JeVois. Consider this starter python script, which is located in ~/jevois-tutorials/doc/snip/objectrackertuning.py:

1 #!/usr/bin/python
2 
3 # Needed packages: sudo apt-get install python python-tk python-serial
4 
5 # This tutorial is a simple program that allows one to adjust the hue, saturation, and value ranges of the ObjectTracker
6 # module using sliders
7 
8 serdev = '/dev/ttyACM0' # serial device of JeVois
9 
10 from Tkinter import *
11 import serial
12 import time
13 
14 
15 # default values for Hue, Saturation, and Value ranges:
16 hmin = 95
17 hmax = 110
18 smin = 100
19 smax = 255
20 vmin = 60
21 vmax = 253
22 
23 ####################################################################################################
24 # Send a command to JeVois and show response
25 def send_command(cmd):
26  print "HOST>> " + cmd
27  ser.write(cmd + '\n')
28  out = ''
29  time.sleep(0.1)
30  while ser.inWaiting() > 0:
31  out += ser.read(1)
32  if out != '':
33  print "JEVOIS>> " + out, # the final comma suppresses extra newline, since JeVois already sends one
34 
35 ####################################################################################################
36 def update_hmin(val):
37  global hmin
38  global hmax
39  hmin = val
40  send_command('setpar hrange {0}...{1}'.format(hmin, hmax))
41 
42 ####################################################################################################
43 def update_hmax(val):
44  global hmin
45  global hmax
46  hmax = val
47  send_command('setpar hrange {0}...{1}'.format(hmin, hmax))
48 
49 ####################################################################################################
50 def update_smin(val):
51  global smin
52  global smax
53  smin = val
54  send_command('setpar srange {0}...{1}'.format(smin, smax))
55 
56 ####################################################################################################
57 def update_smax(val):
58  global smin
59  global smax
60  smax = val
61  send_command('setpar srange {0}...{1}'.format(smin, smax))
62 
63 ####################################################################################################
64 def update_vmin(val):
65  global vmin
66  global vmax
67  vmin = val
68  send_command('setpar vrange {0}...{1}'.format(vmin, vmax))
69 
70 ####################################################################################################
71 def update_vmax(val):
72  global vmin
73  global vmax
74  vmax = val
75  send_command('setpar vrange {0}...{1}'.format(vmin, vmax))
76 
77 ####################################################################################################
78 # Main code
79 ser = serial.Serial(serdev, 115200, timeout=1)
80 send_command('ping') # should return ALIVE
81 
82 master = Tk()
83 
84 w1 = Label(master, text = "Hue min")
85 w1.pack()
86 w2 = Scale(master, from_=0, to=255, tickinterval=32, length=600, orient=HORIZONTAL, command=update_hmin)
87 w2.set(hmin)
88 w2.pack()
89 
90 w3 = Label(master, text = "Hue max")
91 w3.pack()
92 w4 = Scale(master, from_=0, to=255, tickinterval=32, length=600, orient=HORIZONTAL, command=update_hmax)
93 w4.set(hmax)
94 w4.pack()
95 
96 w5 = Label(master, text = "Saturation min")
97 w5.pack()
98 w6 = Scale(master, from_=0, to=255, tickinterval=32, length=600, orient=HORIZONTAL, command=update_smin)
99 w6.set(smin)
100 w6.pack()
101 
102 w7 = Label(master, text = "Saturation max")
103 w7.pack()
104 w8 = Scale(master, from_=0, to=255, tickinterval=32, length=600, orient=HORIZONTAL, command=update_smax)
105 w8.set(smax)
106 w8.pack()
107 
108 w9 = Label(master, text = "Value min")
109 w9.pack()
110 w10 = Scale(master, from_=0, to=255, tickinterval=32, length=600, orient=HORIZONTAL, command=update_vmin)
111 w10.set(vmin)
112 w10.pack()
113 
114 w11 = Label(master, text = "Value max")
115 w11.pack()
116 w12 = Scale(master, from_=0, to=255, tickinterval=32, length=600, orient=HORIZONTAL, command=update_vmax)
117 w12.set(vmax)
118 w12.pack()
119 
120 mainloop()
121 

We basically create 6 slider widgets (for the 3 ranges), and we attach a callback to each slider which will update the corresponding global variable and then send the corresponding updated range of values to JeVois.

Video of this tutorial in action

Where to go from here

You can further improve upon this tutorial in many ways. Here are some suggestions: