-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
161 lines (118 loc) · 5.95 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import cv2
import numpy as np
from hsv_color_range import color_range, check_color_range
from utilities import key_x, key_y
count = 0
small_cube_color_maps = {0: [], 1: [], 2: [], 3: [], 4: [], 5: []}
def draw_small_cube(count, frame, mean_clr):
global small_cube_color_maps
if count < 0 or count > 5:
return frame
xy_left_top = mapping[count][0]
p1 = (xy_left_top[0], xy_left_top[1])
p2 = (xy_left_top[0] + 80, xy_left_top[1])
p3 = (xy_left_top[0] + 160, xy_left_top[1])
p4 = (xy_left_top[0] + 240, xy_left_top[1])
p5 = (xy_left_top[0], xy_left_top[1] + 80)
p6 = (xy_left_top[0] + 240, xy_left_top[1] + 80)
p7 = (xy_left_top[0], xy_left_top[1] + 160)
p8 = (xy_left_top[0] + 240, xy_left_top[1] + 160)
p9 = (xy_left_top[0], xy_left_top[1] + 240)
p10 = (xy_left_top[0] + 80, xy_left_top[1] + 240)
p11 = (xy_left_top[0] + 160, xy_left_top[1] + 240)
p12 = (xy_left_top[0] + 240, xy_left_top[1] + 240)
if len(small_cube_color_maps[count]) == 0:
small_cube_color_maps[count] = mean_clr
# If atleast one side of the cube is captured
if len(small_cube_color_maps[0]) != 0:
for cube_index in range(0, (count+1)):
try:
cv2.rectangle(frame, (p1[0], p1[1]) , (p5[0] + 80, p2[1] + 80) ,small_cube_color_maps[cube_index][0],-1)
cv2.rectangle(frame, (p2[0], p2[1]) , (p5[0] + 160, p3[1] + 80) ,small_cube_color_maps[cube_index][1],-1)
cv2.rectangle(frame, (p3[0], p3[1]) , (p6[0] , p6[1]) ,small_cube_color_maps[cube_index][2],-1)
cv2.rectangle(frame, (p5[0], p5[1]) , (p7[0] + 80, p7[1]) ,small_cube_color_maps[cube_index][3],-1)
cv2.rectangle(frame, (p5[0] + 80, p5[1]) , (p7[0] + 160, p7[1]) ,small_cube_color_maps[cube_index][4],-1)
cv2.rectangle(frame, (p5[0] + 160, p5[1]) , (p8[0], p8[1]) ,small_cube_color_maps[cube_index][5-i],-1)
cv2.rectangle(frame, (p7[0], p7[1]) , (p10[0], p10[1]) ,small_cube_color_maps[cube_index][6-i],-1)
cv2.rectangle(frame, (p7[0] + 80, p7[1]) , (p11[0], p11[1]) ,small_cube_color_maps[cube_index][7-i],-1)
cv2.rectangle(frame, (p7[0] + 160, p7[1]) , (p12[0], p12[1]) ,small_cube_color_maps[cube_index][8-i],-1)
except Exception as e:
print("Cube index: ",cube_index)
cv2.line(frame, p5 , p6, (0,0,0), 5)
cv2.line(frame, p7 , p8, (0,0,0), 5)
cv2.line(frame, p2 , p10, (0,0,0), 5)
cv2.line(frame, p3 , p11, (0,0,0), 5)
cv2.rectangle(frame,mapping[count][0], mapping[count][1],(0,0,0),5)
return frame
mapping = {0: ((30,30),(270,270)), 1: ((290,30),(530,270)), 2: ((550,30),(790,270)),
3: ((810,30),(1050,270)), 4: ((1070,30),(1310,270)), 5: ((1330,30),(1570,270))}
cap = cv2.VideoCapture(0)
mean_clr = [] # initialised here for usage in line 91
while 1:
color = None
ret, frame = cap.read()
annotless_frame = frame.copy()
for i in range(count):
if i > 5:
break
frame = draw_small_cube(i, frame, mean_clr)
if count < 6:
cv2.rectangle(frame,mapping[count][0], mapping[count][1],(0,0,255),10) # When a cube is filled, next empty cube is drawn
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray_blur = cv2.GaussianBlur(gray, (5,5), 4)
edges = cv2.Canny(gray_blur, 30,55)
kernel1 = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
dilation = cv2.dilate(edges,kernel1,iterations = 3)
erosion = cv2.erode(dilation,kernel2,iterations = 5)
contours, hierarchy = cv2.findContours(erosion, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
mask = np.zeros(frame.shape[:2], dtype='uint8')
RR = []
for i in contours:
approx = cv2.approxPolyDP(i, 0.1*cv2.arcLength(i, True), True)
if (len(approx) != 4):
continue
rec = cv2.minAreaRect(i) # center, (width, height), angle of rotation
# Small squares
if (rec[1][0] * rec[1][1] > 8000) and (rec[1][0] * rec[1][1] < 18000):
ratio = float(rec[1][0])/rec[1][1] # width / height
if ratio >= 1.15 or ratio <= 0.88:
continue
print(rec[1][0] * rec[1][1])
RR.append(rec)
mean_clr = []
if len(RR) < 9:
subarray = [RR]
for REC in subarray:
if count > 5:
break
for rec in REC:
box = cv2.boxPoints(rec)
box = np.int0(box)
cv2.circle(frame,(int(rec[0][0]), int(rec[0][1])), int(rec[1][0]/2), (0,255,255), 10) # img, center, radius, color, thickness
cv2.circle(mask,(int(rec[0][0]), int(rec[0][1])), int(rec[1][0]/2), (255,255,255), -1)
mask2 = np.zeros(frame.shape[:2], dtype='uint8')
cv2.circle(mask2,(int(rec[0][0]), int(rec[0][1])), int(rec[1][0]/2), (255,255,255), -1)
mean_val_hsv = np.array(cv2.mean(cv2.cvtColor(annotless_frame.copy(), cv2.COLOR_BGR2HSV), mask=mask2)[:-1])
color = check_color_range(mean_val_hsv)
if len(RR) >= 9:
mean_clr.append(color)
if color == None:
color = (0,0,0)
print("----------None----------")
else:
...
shift = np.ceil(3.5 * int(rec[1][0]) + 0.5 * int(rec[1][0]))
org = (int(shift + rec[0][0]), int(rec[0][1]))
cv2.putText(frame, "C", org ,cv2.FONT_HERSHEY_SIMPLEX, 1.3, color, thickness=10)
frame3 = cv2.bitwise_and(annotless_frame, annotless_frame, mask=mask)
concat1 = np.concatenate((edges, dilation))
cv2.imshow("Main frame", frame)
#cv2.imshow("Mask", frame3)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if cv2.waitKey(60) & 0xFF == ord('e'):
count += 1
if count > 6:
small_cube_color_maps = {0: [], 1: [], 2: [], 3: [], 4: [], 5: []}
count = 0