Add low precision color detection (#17)
* Add low precision color detection * Add blank line at end of file * Remove tab * Remove tab * Remove useless comment Co-authored-by: Erick <74315338+erickahmed@users.noreply.github.com> * Update vision-env/src/vision-test.py Co-authored-by: Erick <74315338+erickahmed@users.noreply.github.com> * Update vision-env/src/vision-test.py Co-authored-by: Erick <74315338+erickahmed@users.noreply.github.com> * Update vision-env/src/vision-test.py Co-authored-by: Erick <74315338+erickahmed@users.noreply.github.com> Co-authored-by: Erick <74315338+erickahmed@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,69 @@
|
|||||||
|
from scipy.spatial import KDTree
|
||||||
|
from webcolors import (
|
||||||
|
hex_to_rgb,
|
||||||
|
)
|
||||||
|
|
||||||
|
COLORS = {
|
||||||
|
"aqua": ["#00ffff", (0,255,255)],
|
||||||
|
"black": ["#000000", (0,0,0)],
|
||||||
|
"blue": ["#0000ff", (0,0,255)],
|
||||||
|
"fuchsia": ["#ff00ff", (255,0,255)],
|
||||||
|
"green": ["#008000", (0,128,0)],
|
||||||
|
"gray": ["#808080", (128,128,128)],
|
||||||
|
"lime": ["#00ff00", (0,255,0)],
|
||||||
|
"olive": ["#808000", (128,128,0)],
|
||||||
|
"purple": ["#800080", (128,0,128)],
|
||||||
|
"red": ["#ff0000", (255,0,0)],
|
||||||
|
"silver": ["#c0c0c0", (192,192,192)],
|
||||||
|
"teal": ["#008080", (0,128,128)],
|
||||||
|
"white": ["#ffffff", (255,255,255)],
|
||||||
|
"yellow": ["#ffff00", (255,255,0)],
|
||||||
|
"beige": ["#f5f5dc", (245,245,220)],
|
||||||
|
"brown": ["#a52a2a", (165,42,42)],
|
||||||
|
"gold": ["#ffd700", (255,215,0)],
|
||||||
|
"pink": ["#ffc0cb", (255,192,203)],
|
||||||
|
"lavender": ["#e6e6fa", (230,230,250)],
|
||||||
|
"turquoise": ["#40e0d0", (64,224,208)],
|
||||||
|
"violet": ["#ee82ee", (238,130,238)],
|
||||||
|
"orange": ["#ffa500", (255,165,0)],
|
||||||
|
"lightblue": ["#add8e6", (173,216,230)]
|
||||||
|
}
|
||||||
|
|
||||||
|
COLORS_HEX = {
|
||||||
|
"aqua": "#00ffff",
|
||||||
|
"black": "#000000",
|
||||||
|
"blue": "#0000ff",
|
||||||
|
"fuchsia": "#ff00ff",
|
||||||
|
"green": "#008000",
|
||||||
|
"gray": "#808080",
|
||||||
|
"lime": "#00ff00",
|
||||||
|
"olive": "#808000",
|
||||||
|
"purple": "#800080",
|
||||||
|
"red": "#ff0000",
|
||||||
|
"silver": "#c0c0c0",
|
||||||
|
"teal": "#008080",
|
||||||
|
"white": "#ffffff",
|
||||||
|
"yellow": "#ffff00",
|
||||||
|
"beige": "#f5f5dc",
|
||||||
|
"brown": "#a52a2a",
|
||||||
|
"gold": "#ffd700",
|
||||||
|
"pink": "#ffc0cb",
|
||||||
|
"lavender": "#e6e6fa",
|
||||||
|
"turquoise": "#40e0d0",
|
||||||
|
"violet": "#ee82ee",
|
||||||
|
"orange": "#ffa500",
|
||||||
|
"lightblue": "#add8e6",
|
||||||
|
}
|
||||||
|
|
||||||
|
def convert_rgb_to_names(rgb_tuple):
|
||||||
|
# A dictionary of all the color names and their respective hex in color_db
|
||||||
|
color_db = COLORS_HEX
|
||||||
|
names = []
|
||||||
|
rgb_values = []
|
||||||
|
for color_name, color_hex in color_db.items():
|
||||||
|
names.append(color_name)
|
||||||
|
rgb_values.append(hex_to_rgb(color_hex))
|
||||||
|
|
||||||
|
kdt_db = KDTree(rgb_values)
|
||||||
|
distance, index = kdt_db.query(rgb_tuple)
|
||||||
|
return str(names[index])
|
||||||
@@ -1,16 +1,22 @@
|
|||||||
from genericpath import isfile
|
|
||||||
import io
|
import io
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import cv2
|
import cv2
|
||||||
|
import math
|
||||||
|
import colors_detection
|
||||||
import json
|
import json
|
||||||
|
from genericpath import isfile
|
||||||
from pyautogui import size
|
from pyautogui import size
|
||||||
from google.cloud import vision
|
from google.cloud import vision
|
||||||
|
|
||||||
# Minimum score required for labels detection
|
# Minimum score required for labels detection
|
||||||
MIN_SCORE_REQUIRED = 0.80
|
MIN_SCORE_REQUIRED = 0.80
|
||||||
|
|
||||||
|
# Minimum pixel_fraction required for color detection.
|
||||||
|
# Actually unused but useful for filtering colors.
|
||||||
|
MIN_PFRACTION_REQUIRED = 0.02
|
||||||
|
|
||||||
def shape_selection(event, x, y, flags, param):
|
def shape_selection(event, x, y, flags, param):
|
||||||
# grab references to the global variables
|
# grab references to the global variables
|
||||||
global ref_point, crop
|
global ref_point, crop
|
||||||
@@ -138,15 +144,45 @@ client = vision.ImageAnnotatorClient()
|
|||||||
image = vision.Image(content=content)
|
image = vision.Image(content=content)
|
||||||
|
|
||||||
# Performs label detection on the image file
|
# Performs label detection on the image file
|
||||||
response = client.label_detection(image=image)
|
label_response = client.label_detection(image=image)
|
||||||
labels = response.label_annotations
|
labels = label_response.label_annotations
|
||||||
|
|
||||||
|
# Performs image_properties detection on the image file
|
||||||
|
colors_response = client.image_properties(image=image)
|
||||||
|
colors = colors_response.image_properties_annotation.dominant_colors.colors
|
||||||
|
|
||||||
# Labels sorted by attribute score
|
# Labels sorted by attribute score
|
||||||
sorted_labels = sorted(labels, key=lambda x:x.score, reverse=True)
|
sorted_labels = sorted(labels, key=lambda x:x.score, reverse=True)
|
||||||
|
|
||||||
# Labels filtered by attribute score
|
# Labels filtered by attribute score
|
||||||
filtered_labels = filter(lambda x:x.score > MIN_SCORE_REQUIRED, sorted_labels)
|
filtered_labels = filter(lambda x:x.score > MIN_SCORE_REQUIRED, sorted_labels)
|
||||||
|
|
||||||
print('Labels:')
|
# Labels sorted by attribute score
|
||||||
|
sorted_colors = sorted(colors, key=lambda x:x.pixel_fraction, reverse=True)
|
||||||
|
# Colors filtered by attribute pixel_fraction
|
||||||
|
#filtered_colors = filter(lambda x:x.pixel_fraction > MIN_PFRACTION_REQUIRED, sorted_colors)
|
||||||
|
# Actually no filtering policy
|
||||||
|
filtered_colors = sorted_colors
|
||||||
|
|
||||||
|
print('\nLabels:')
|
||||||
for label in filtered_labels:
|
for label in filtered_labels:
|
||||||
print(label.description + ' ---> ' + str(label.score))
|
print('--- Label: ' + label.description + ' ---> ' + str(math.trunc(label.score*100)) + '%' +
|
||||||
|
'\n')
|
||||||
|
|
||||||
|
print('\nColors:')
|
||||||
|
for color_info in filtered_colors:
|
||||||
|
color = color_info.color
|
||||||
|
|
||||||
|
red = math.trunc(color.red)
|
||||||
|
green = math.trunc(color.green)
|
||||||
|
blue = math.trunc(color.blue)
|
||||||
|
|
||||||
|
rgb_triplet = (red, green, blue)
|
||||||
|
rgb_triplet_str = (str(red) + '%, ', str(green) + '%, ', str(blue) + '%')
|
||||||
|
color_name = colors_detection.convert_rgb_to_names(rgb_triplet)
|
||||||
|
|
||||||
|
# uncomment for verbose output
|
||||||
|
#print(str(color_info.pixel_fraction) + ' == ' + str(math.trunc(color_info.pixel_fraction*100)) + '%' + ' score: ' + str(color_info.score) + '---> ' + color_name)
|
||||||
|
print('--- Pixel Fraction: ' + str(round(color_info.pixel_fraction*100, 3)) + '%' +
|
||||||
|
'\tScore: ' + str(round(color_info.score, 5)) +
|
||||||
|
'\tColor Name: ' + color_name +
|
||||||
|
'\n')
|
||||||
|
|||||||
Reference in New Issue
Block a user