So, it seems like you are attempting something quite a bit out of your skill level. So, I’ll do this. Here’s the code:
import sensor, image, time, math, gc
sensor.set_pixformat(sensor.RGB565) # or RGB565.
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()
list_of_color_names = 
list_of_color_rgb_tuples = 
with open("rgb.txt") as f:
for l in f:
tokens = l.strip().split()
list_of_color_rgb_tuples.append(tuple(int(x) for x in tokens[0:3]))
gc.collect() # prevent heap fragmentation
def closest_color(r, g, b):
closest_color_diff = 1000000
closest_color_name = "Uknown"
for i in range(len(list_of_color_rgb_tuples)):
red_diff = r - list_of_color_rgb_tuples[i]
green_diff = g - list_of_color_rgb_tuples[i]
blue_diff = b - list_of_color_rgb_tuples[i]
diff = math.sqrt((red_diff*red_diff)+(green_diff*green_diff)+(blue_diff*blue_diff))
if diff < closest_color_diff:
closest_color_diff = diff
closest_color_name = list_of_color_names[i]
img = sensor.snapshot()
stats = img.get_statistics()
r, g, b = image.lab_to_rgb((stats.l_mean(), stats.a_mean(), stats.b_mean()))
colorName = closest_color(r, g, b)
And finally, you need to copy the attached rgb.txt file to the OpenMV Cam disk to get the color names.
This meets your initial question. However, moving forwards… please try to understand exactly what is happening in every line in that script and understand what’s going on.
rgb.txt (31 KB)