-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
180 lines (117 loc) · 5.45 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from sys import maxsize
import cv2
import uuid
import time
from centerRedCircle import centerRedCircle
import numpy
from typing import List
import json
from websocket import create_connection
import math
vid = cv2.VideoCapture("rtsp://admin:P@[email protected]/1")
cv2.namedWindow('Cerling')
cascade_stone = cv2.CascadeClassifier("./cascade.xml")
STEP_STONE = 25
STEP_STONE_FRAME = 2
POINT = 0
stone_first_color = None
with open("app.conf", "r") as f:
CONFIG = json.loads(f.read())
RED_ROUND = CONFIG["red_round"]
BLUE_ROUND = CONFIG["blue_round"]
BLUE_CENTER_TOP = int(BLUE_ROUND[0] + (BLUE_ROUND[2] / 2))
CENTER_RED_ROUND = CONFIG["center_circle"]
GAME_FIELD = CONFIG["game_field"]
RADIUS = CENTER_RED_ROUND[1] - BLUE_ROUND[1]
IS_STARTING_CALIBRATE = False
INDEX_STARTING_CALIBRATE = 100
STONE_LIST = []
NORMILIZE_STACK = []
INDEX_MAX_START_NORMILIZE = 30
INDEX_START_NORMILIZE = INDEX_MAX_START_NORMILIZE
old_count_stone = 0
count_stone_normalaze = 0
ret, frame = vid.read()
def range_color(change: List[int], range: List[List[int]]):
return numpy.sum(change > range[0]) == 3 and numpy.sum(change < range[1]) == 3
while(True):
ret, frame = vid.read()
if not isinstance(frame, numpy.ndarray):
vid = cv2.VideoCapture("rtsp://admin:P@[email protected]/1")
continue
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rectangles = cascade_stone.detectMultiScale(gray, scaleFactor=1.01, minSize=(155, 155), maxSize=(180,180))
count_stone = 0
for (x, y, w, h) in rectangles:
x = math.floor(x / STEP_STONE_FRAME) * STEP_STONE_FRAME
y = math.floor(y / STEP_STONE_FRAME) * STEP_STONE_FRAME
w = math.floor(w / STEP_STONE_FRAME) * STEP_STONE_FRAME
h = math.floor(h / STEP_STONE_FRAME) * STEP_STONE_FRAME
rect_stone = frame[y + STEP_STONE:y+h - STEP_STONE, x + STEP_STONE:x+w - STEP_STONE]
avg_color_stone = numpy.mean(numpy.mean(rect_stone, axis=1), axis=0)
if numpy.sum(numpy.isnan(avg_color_stone)) == 3:
continue
yellow_stone = range_color(avg_color_stone, [ numpy.array([70,160,190]), numpy.array([170,255,255]) ])
green_stone = range_color(avg_color_stone, [ numpy.array([70,115,0]), numpy.array([170,255,75]) ])
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 3)
name_color = None
if yellow_stone:
name_color = "yellow"
if green_stone:
name_color = "green"
if not yellow_stone and not green_stone:
continue
count_stone += 1
center_stone = ( x + int(w / 2), y + int(h / 2) )
cv2.circle(frame, ( x + int(w / 2), y + int(h / 2) ), 5, (255, 0, 0), 2)
right_bottom_rect = (CENTER_RED_ROUND[0], center_stone[1])
bottom_distance = abs(right_bottom_rect[0] - center_stone[0])
right_distance = abs(right_bottom_rect[1] - CENTER_RED_ROUND[1])
distance = int(((bottom_distance ** 2) + (right_distance ** 2)) ** 0.5)
if distance - ((int(w / 2) + int(h / 2)) / 3 ) > RADIUS:
continue
cv2.putText(frame, str(distance), (center_stone[0], center_stone[1] + 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 1, cv2.LINE_AA)
STONE_LIST.append({
"color": name_color,
"distance": distance,
})
NORMILIZE_STACK.append(count_stone)
INDEX_START_NORMILIZE -= 1
if INDEX_START_NORMILIZE == 0:
INDEX_START_NORMILIZE = INDEX_MAX_START_NORMILIZE
count_stone_normalaze = sum(NORMILIZE_STACK) / INDEX_MAX_START_NORMILIZE
NORMILIZE_STACK.clear()
STONE_LIST = sorted(STONE_LIST, key=lambda x: x['distance'])
POINT = 0
stone_first_color = None
for index, stone in enumerate(STONE_LIST):
if index == 0:
stone_first_color = stone['color']
POINT += 1
elif stone_first_color != stone['color']:
break
else:
POINT += 1
if abs(count_stone_normalaze - old_count_stone) > 0.80:
old_count_stone = round(count_stone_normalaze)
if IS_STARTING_CALIBRATE:
pass
if INDEX_STARTING_CALIBRATE >= 0 and IS_STARTING_CALIBRATE == False:
INDEX_STARTING_CALIBRATE -= 1
else:
IS_STARTING_CALIBRATE = True
cv2.putText(frame, str(POINT), (50, 250), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,0,0), 3, cv2.LINE_AA)
cv2.putText(frame, str(stone_first_color), (50, 300), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,0,0), 3, cv2.LINE_AA)
cv2.putText(frame, str(RADIUS), (50, 350), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,0,0), 3, cv2.LINE_AA)
STONE_LIST.clear()
print("stone", count_stone)
print("old", old_count_stone)
print("normalize", count_stone_normalaze)
cv2.rectangle(frame, (RED_ROUND[0], RED_ROUND[1]), (RED_ROUND[0] + RED_ROUND[2], RED_ROUND[1] + RED_ROUND[3]), (255, 0, 0), 3)
cv2.rectangle(frame, (BLUE_ROUND[0], BLUE_ROUND[1]), (BLUE_ROUND[0] + BLUE_ROUND[2], BLUE_ROUND[1] + BLUE_ROUND[3]), (255, 0, 0), 3)
cv2.circle(frame, ( BLUE_CENTER_TOP, BLUE_ROUND[1] ), 5, (255, 0, 0), 2)
frame = cv2.resize(frame, (1280, 800))
cv2.imshow('Cerling', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()