-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_recognizer.py
50 lines (39 loc) · 1.16 KB
/
color_recognizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import cv2
import numpy as np
import pandas as pd
from .file_controller import ROOT_PATH
index = ['color', 'color_name', 'hex', 'R', 'G', 'B']
_COLOR_MAPPER = pd.read_csv(f'{ROOT_PATH}/data/colors.csv', names = index, header = None)
def getColorName(R, G, B):
minimum = 10000
for i in range(len(_COLOR_MAPPER)):
d = abs(R - int(_COLOR_MAPPER.loc[i, 'R'])) + \
abs(G - int(_COLOR_MAPPER.loc[i, 'G'])) + \
abs(B - int(_COLOR_MAPPER.loc[i, 'B']))
if(d <= minimum):
minimum = d
cname = _COLOR_MAPPER.loc[i, 'color_name']
return cname
def color_recognize(frame, y, x):
b, g, r = frame[y, x]
b = int(b)
g = int(g)
r = int(r)
return getColorName(r, g, b), r, g, b
def is_rgb(frame, y, x, target):
b, g, r = frame[y, x]
b = int(b)
g = int(g)
r = int(r)
if target == 'r':
target = r
another = g, b
elif target == 'g':
target = g
another = r, b
elif target == 'b':
target = b
another = r, g
else:
raise TypeError('target must be r, g, or b.')
return target >= another[0] and target >= another[1]