-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_detection.py
77 lines (64 loc) · 2.44 KB
/
color_detection.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import cv2
import numpy as np
from config import *
# Grid
cell_size = [
(corners[1][0]-corners[0][0]) // grid_size[0],
(corners[1][1]-corners[0][1]) // grid_size[1]
]
def bgr_to_hsv(color):
arr = np.uint8([[list(color)]])
r, g, b = cv2.cvtColor(arr, cv2.COLOR_BGR2HSV)[0][0]
return [r, g, b]
def hsv_to_bgr(color):
arr = np.uint8([[list(color)]])
r, g, b = cv2.cvtColor(arr, cv2.COLOR_HSV2BGR)[0][0]
return [r, g, b]
emoji = {
"W":"⚫",
" ":"⚪",
"C":"🟢",
"M":"🟡"
}
last = []
def detect(img, num_guesses):
colors = [["" for _ in range(grid_size[0])] for _ in range(grid_size[1])]
for y in range(grid_size[1]):
for x in range(grid_size[0]):
mx = corners[0][0] + x * cell_size[0]
my = corners[0][1] + y * cell_size[1]
#cv2.rectangle(img, (mx, my), (mx+cell_size[0], my+cell_size[1]), color=(0, 0, 255), thickness=2)
mean_color = np.array(bgr_to_hsv(cv2.mean(img[my:my+cell_size[0], mx:mx+cell_size[0]])[:-1]))
#mean_color = np.array(cv2.mean(img[my:my+cell_size[0], mx:mx+cell_size[0]])[:-1])
#cv2.imshow("cropped", img[my:my+cell_size[0], mx:mx+cell_size[0]])
#print(mean_color)
#cv2.waitKey()
#print(mean_color[0])
diffs = [
np.linalg.norm(mean_color[0] - GREY),
#np.linalg.norm(mean_color - WHITE),
np.linalg.norm(mean_color[0] - GREEN),
np.linalg.norm(mean_color[0] - YELLOW)
]
min_diff = min(diffs)
min_index = diffs.index(min_diff)
best_color = ["W","C","M"][min_index]
colors[y][x] = best_color
# blank out the rest of the grid
for r in range(num_guesses+1, grid_size[1]):
colors[r] = [" " for _ in range(grid_size[0])]
# test for end of game: if the grid does not match the last grid, then the game is over
# because the stats page is displayed, screwing up the grid detection
global last
for i in range(num_guesses):
if colors[i] != last[i]:
colors[num_guesses] = ["C" for _ in range(grid_size[0])] # success
return colors
def printGrid(colors):
print("\n".join(["".join([emoji[i] for i in row]) for row in colors]))
if __name__ == "__main__":
img = cv2.imread('dewarped.png')
colors = detect(img)
printGrid(colors)
cv2.imshow('color grid', img)
cv2.waitKey()