Skip to content

Commit 24ee1a4

Browse files
committed
color modified
1 parent 7b16220 commit 24ee1a4

6 files changed

+119
-98
lines changed

knowledge_graph.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ def create_knowledge_graph(frame):
3737
:return: knowledge dict and plotted image
3838
"""
3939

40-
knowledge = {"scene": "", "classes": {}}
41-
# add scene
42-
knowledge["scene"] = SceneClassifier.predict(frame)
40+
knowledge = {"scene": SceneClassifier.predict(frame), "classes": {}}
4341

4442
boxes, classes = ObjectDetector.predict(frame)
4543
class_count = Counter(classes)
@@ -54,7 +52,7 @@ def create_knowledge_graph(frame):
5452
l, t, r, b = list(map(int, box))
5553
crop = frame[t:b, l:r]
5654
text = TextDetector.detect(crop)
57-
color = ColorDetector.predict(crop)
55+
color = ColorDetector.find_color(crop)
5856
index = len(knowledge["classes"][classes[i]].get("objects", {}))
5957
if index == 0:
6058
knowledge["classes"][classes[i]]["objects"] = {}

modules/answer_generator.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33

44

55
class AnswerGenerator:
6-
def __init__(self):
6+
def __init__(self, verbose=False):
77
"""
88
create a model from pre-trained weights
99
"""
10-
print("START")
10+
if verbose:
11+
print("Loading model. This might take a while...")
1112
with nostderrout():
1213
self.model = build_model(configs.squad.squad)
13-
print("END")
14+
if verbose:
15+
print("Loaded model.")
1416

1517
def predict(self, comprehension, question):
1618
"""

modules/color_detection.py

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,75 @@
1-
from sklearn.cluster import KMeans
1+
import cv2
22
import numpy as np
3-
import webcolors
43

54

65
class ColorDetector:
76

7+
colors = {"black": [0, 0, 0],
8+
"white": [255, 255, 255],
9+
"gray": [102, 102, 102],
10+
"maroon": [140, 0, 0],
11+
"red": [255, 0, 0],
12+
"orange": [255, 85, 0],
13+
"yellow": [255, 170, 0],
14+
"green": [0, 255, 0],
15+
"aqua": [0, 255, 242],
16+
"blue": [0, 85, 255],
17+
"purple": [102, 0, 255],
18+
"pink": [255, 0, 255]}
19+
820
@staticmethod
9-
def find_closest_color(requested_colour):
21+
def nearest_color(color):
1022
"""
11-
finds the closest color in the sixteen HTML4 color
12-
:param requested_colour: RGB value of color
13-
:return: RGB value of color closest among HTML4 colors
23+
find the nearest color
24+
:param color: (R, G, B)
25+
:return: nearest (R', G', B')
1426
"""
15-
min_colours = {}
16-
for key, name in webcolors.css3_hex_to_names.items():
17-
r_c, g_c, b_c = webcolors.hex_to_rgb(key)
18-
rd = (r_c - requested_colour[0]) ** 2
19-
gd = (g_c - requested_colour[1]) ** 2
20-
bd = (b_c - requested_colour[2]) ** 2
21-
min_colours[(rd + gd + bd)] = name
22-
return min_colours[min(min_colours.keys())]
27+
wrapper = np.uint8([[list(color)]])
28+
hsv_wrapper = cv2.cvtColor(wrapper, cv2.COLOR_RGB2HSV)
29+
hsv = hsv_wrapper[0, 0] + [10, 0, 0]
30+
h, s, v = hsv
31+
if v < 30:
32+
return ColorDetector.colors["black"]
33+
elif v < 80:
34+
return ColorDetector.colors["gray"]
35+
elif v > 190 and s < 27:
36+
return ColorDetector.colors["white"]
37+
elif s < 54 and v < 185:
38+
return ColorDetector.colors["gray"]
39+
elif h < 18:
40+
if v < 150:
41+
return ColorDetector.colors["maroon"]
42+
else:
43+
return ColorDetector.colors["red"]
44+
elif h < 26:
45+
return ColorDetector.colors["orange"]
46+
elif h < 34:
47+
return ColorDetector.colors["yellow"]
48+
elif h < 73:
49+
return ColorDetector.colors["green"]
50+
elif h < 102:
51+
return ColorDetector.colors["aqua"]
52+
elif h < 127:
53+
return ColorDetector.colors["blue"]
54+
elif h < 149:
55+
return ColorDetector.colors["purple"]
56+
elif h < 175:
57+
return ColorDetector.colors["pink"]
58+
else:
59+
return ColorDetector.colors["red"]
2360

2461
@staticmethod
25-
def get_colour_name(requested_colour):
26-
"""
27-
get color name of passed RGB value
28-
:param requested_colour: RGB value of a color
29-
:return: named color of the RGB value
30-
"""
31-
try:
32-
closest_name = webcolors.rgb_to_name(requested_colour)
33-
except ValueError:
34-
closest_name = ColorDetector.find_closest_color(requested_colour)
35-
return closest_name
62+
def approximate_image(image):
63+
return np.asarray([[ColorDetector.nearest_color(pixel) for pixel in row] for row in image])
3664

3765
@staticmethod
38-
def predict(frame):
39-
"""
40-
predicts primary color in the frame
41-
:param frame: input image as numpy array
42-
:return: name of the color
43-
"""
44-
height, width, dim = frame.shape
45-
resized = np.resize(frame, (width * height, dim))
46-
# cluster
47-
k_means = KMeans(16)
48-
labels = k_means.fit_predict(resized)
49-
palette = k_means.cluster_centers_
50-
# create new image
51-
new_image = np.reshape(palette[labels], (width, height, palette.shape[1]))
52-
new_image = new_image.astype(np.uint8)
53-
# find dominant color
54-
unique, counts = np.unique(new_image.reshape(-1, new_image.shape[2]), return_counts=True, axis=0)
55-
sort_ix = np.argsort(counts)
56-
sort_ix = sort_ix[-1]
57-
58-
return ColorDetector.get_colour_name(tuple(unique[sort_ix]))
66+
def find_color(image):
67+
image = ColorDetector.approximate_image(image)
68+
(values, counts) = np.unique(image.reshape(-1, image.shape[2]), return_counts=True, axis=0)
69+
index = np.argmax(counts)
70+
color = values[index]
71+
for (key, value) in ColorDetector.colors.items():
72+
if (value == color).all():
73+
return key
74+
return "black"
75+

modules/paragraph_generator.py

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import inflect
22
from num2words import num2words
33

4+
45
class ParagraphGenerator:
56
def __init__(self):
67
self.ie = inflect.engine()
@@ -35,6 +36,36 @@ def describe_class_count(self, class_, count):
3536
self.ie.plural(class_, count=count)
3637
)
3738

39+
def describe_object_text(self, classname, id, text, single=False):
40+
"""
41+
describe the text on the object
42+
Example:
43+
The first board says hello.
44+
Hello is written on the first stop sign.
45+
:param classname: object to describe
46+
:param id: identifier of the object
47+
:param text: text on the object
48+
:return: sentence describing text
49+
"""
50+
num2word = num2words(id, to='ordinal') + ' '
51+
if single:
52+
num2word = ""
53+
54+
description = ""
55+
56+
description += "The %s%s says %s. " % (
57+
num2word,
58+
classname,
59+
text
60+
)
61+
description += "%s is written on the %s%s. " % (
62+
text,
63+
num2word,
64+
classname
65+
)
66+
67+
return description
68+
3869
def describe_object_color(self, classname, id, color, single=False):
3970
"""
4071
describe the color of the object
@@ -45,6 +76,7 @@ def describe_object_color(self, classname, id, color, single=False):
4576
:param classname: object to describe
4677
:param id: identifier of the object
4778
:param color: color of the object
79+
:param single: if object is occurring once, do not mention index
4880
:return: sentence describing color
4981
"""
5082
num2word = num2words(id, to='ordinal') + ' '
@@ -62,36 +94,6 @@ def describe_object_color(self, classname, id, color, single=False):
6294
color
6395
)
6496

65-
def describe_object_text(self, classname, id, text, single=False):
66-
"""
67-
describe the text on the object
68-
Example:
69-
The first board says hello.
70-
Hello is written on the first stop sign.
71-
:param classname: object to describe
72-
:param id: identifier of the object
73-
:param text: text on the object
74-
:return: sentence describing text
75-
"""
76-
num2word = num2words(id, to='ordinal') + ' '
77-
if single:
78-
num2word = ""
79-
80-
description = ""
81-
82-
description += "The %s%s says %s. " % (
83-
num2word,
84-
classname,
85-
text
86-
)
87-
description += "%s is written on the %s%s. " % (
88-
text,
89-
num2word,
90-
classname
91-
)
92-
93-
return description
94-
9597
def describe_scene(self, scene):
9698
"""
9799
describe the scene

modules/text_detection.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import cv2
2+
import re
23
import numpy as np
34
import pytesseract
45
from imutils.object_detection import non_max_suppression
@@ -86,11 +87,10 @@ def get_stitched_text(detected):
8687
:param detected: array of [text, bounding box]
8788
:return: sentence of words
8889
"""
89-
# TODO: complete this function
9090
sentence = ""
9191
for text, _ in detected:
92-
sentence += text.strip() + " "
93-
return sentence
92+
sentence += " ".join(re.findall("[a-zA-Z]+", text)).strip() + " "
93+
return sentence.strip()
9494

9595
@staticmethod
9696
def detect(frame):

run.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@
1414

1515
knowledge, frame = knowledge_graph.create_knowledge_graph(image)
1616
pprint(knowledge)
17-
18-
paragraph_generator = ParagraphGenerator()
19-
paragraph = paragraph_generator.generate(knowledge)
20-
21-
answer_generator = AnswerGenerator()
22-
print(paragraph)
23-
24-
while True:
25-
question = input().strip()
26-
if question == "":
27-
break
28-
print(answer_generator.predict(paragraph, question))
17+
#
18+
# paragraph_generator = ParagraphGenerator()
19+
# paragraph = paragraph_generator.generate(knowledge)
20+
#
21+
# print(paragraph)
22+
# answer_generator = AnswerGenerator(verbose=True)
23+
#
24+
# while True:
25+
# print("[QUESTION] ", end="")
26+
# question = input().strip()
27+
# if question == "":
28+
# break
29+
# answer = answer_generator.predict(paragraph, question)[0]
30+
# print("[ANSWER] %s" % answer)

0 commit comments

Comments
 (0)