-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoordinates_server.py
More file actions
193 lines (132 loc) · 5.83 KB
/
Copy pathcoordinates_server.py
File metadata and controls
193 lines (132 loc) · 5.83 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
#Basedo nos códigos de: https://towardsdatascience.com/automatic-vision-object-tracking-347af1cc8a3b
#https://stackoverflow.com/questions/56787999/python-opencv-realtime-get-rgb-values-when-mouse-is-clicked
from __future__ import print_function
from imutils.video import VideoStream
import imutils
import time
import cv2
import os
import numpy as np
import socket
######################################################################
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('localhost', 8089))
######################################################################
#Coletos os limites hsv da cor que for clicada com o mouse
def retorna_hsv():
lista_rgb = []
hsv_superior = []
hsv_inferior = []
def mouseRGB(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN: #checks mouse left button down condition
colorsB = frame[y,x,0]
colorsG = frame[y,x,1]
colorsR = frame[y,x,2]
lista_rgb.append(colorsR)
lista_rgb.append(colorsG)
lista_rgb.append(colorsB)
def rgb_2_hsv(lista_rgb):
blue = lista_rgb[0]
green = lista_rgb[1]
red = lista_rgb[2]
color = np.uint8([[[red, green, blue]]])
hsv_color = cv2.cvtColor(color, cv2.COLOR_BGR2HSV)
hue = hsv_color[0][0][0]
return hue
cv2.namedWindow('mouseRGB')
cv2.setMouseCallback('mouseRGB',mouseRGB)
capture = cv2.VideoCapture(1, cv2.CAP_DSHOW)
while(True):
ret, frame = capture.read()
frame = imutils.resize(frame, width=1150)
cv2.imshow('mouseRGB', frame)
if cv2.waitKey(1) == 27:
break
if len(lista_rgb)>2:
hsv_inferior.append(int((rgb_2_hsv(lista_rgb)-15))) #sensibilidade da detecção da cor
hsv_inferior.append((100))
hsv_inferior.append((100))
hsv_superior.append(int((rgb_2_hsv(lista_rgb)+15))) #sensibilidade da detecção da cor
hsv_superior.append(255)
hsv_superior.append(255)
hsv_inferior = tuple(hsv_inferior)
hsv_superior = tuple(hsv_superior)
return hsv_inferior, hsv_superior
#coleta os limites hsv para duas cores
m1Lower, m1Upper = retorna_hsv()
m2Lower, m2Upper = retorna_hsv()
cv2.destroyAllWindows()
# initialize the video stream and allow the camera sensor to warmup
print("Acessando a webcam...")
vs = VideoStream(1).start()
time.sleep(2.0)
frame = vs.read()
#informa as coordenadas x e y, bem como o raio de um circulo de cores com limites hsv informados
def info_marcador(vs, frame, colorLower, colorUpper, raio):
#frame = imutils.rotate(frame, angle=180)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# construct a mask for the object color, then perform a series of dilations and erosions to remove any small blobs left in the mask
mask = cv2.inRange(hsv, colorLower, colorUpper)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
# find contours in the mask and initialize the current (x, y) center of the object
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2]
#cnts = cnts[0] if imutils.is_cv2() else cnts[1]
center = None
# only proceed if at least one contour was found
if len(cnts) > 0:
# find the largest contour in the mask, then use it to compute the minimum enclosing circle and centroid
c = max(cnts, key=cv2.contourArea)
((x, y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
# only proceed if the radius meets a minimum size
if (radius > raio):
return int(x), int(y), int(radius), center
return 0, 0, 0, (0,0)
def desenha_circulos(frame, x, y, radius, center):
cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2)
cv2.circle(frame, center, 5, (0, 0, 255), -1)
def captura_raio():
lista_r = []
ultimos_r = []
#retém a média do raio do primeriro circulo marcado
while True:
frame = vs.read()
frame = imutils.resize(frame, width=1150)
key = cv2.waitKey(1) & 0xFF
xA, yA, rA, cA = info_marcador(vs, frame, m1Lower, m1Upper, 0)
lista_r.append(rA)
desenha_circulos(frame, xA, yA, rA, cA)
cv2.imshow("Frame", frame)
if key == 27:
print(lista_r)
ultimos_r = lista_r[-9:]
raio_marcador = int(np.mean(ultimos_r))
break
return raio_marcador
raio_marcador = captura_raio()
def rastreia_circulo(m1Lower, m1Upper, m2Lower, m2Upper, raio_marcador):
#rastreia dois círculos com cores e raios definidos
while True:
# grab the next frame from the video stream, Invert 180o, resize the frame, and convert it to the HSV color space
frame = vs.read()
frame = imutils.resize(frame, width=1150)
key = cv2.waitKey(1) & 0xFF
xA, yA, rA, cA = info_marcador(vs, frame, m1Lower, m1Upper, (raio_marcador-10))
xZ, yZ, rZ, zA = info_marcador(vs, frame, m2Lower, m2Upper, (raio_marcador-10))
desenha_circulos(frame, xA, yA, rA, cA)
desenha_circulos(frame, xZ, yZ, rZ, zA)
print("y1:", yA, " y2:", yZ)
cv2.imshow("Frame", frame)
return yA, yZ
if key == 27:
break
cv2.destroyAllWindows()
while True:
ya, yz= rastreia_circulo(m1Lower, m1Upper, m2Lower, m2Upper, raio_marcador)
ya = int(ya)
yz = int(yz)
lista_coordenadas =[ya, yz]
coordenadas_texto = str(lista_coordenadas)
clientsocket.send(coordenadas_texto.encode())