-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython Code
More file actions
67 lines (59 loc) · 1.4 KB
/
Python Code
File metadata and controls
67 lines (59 loc) · 1.4 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
import serial
import cv2 as cv
import numpy as np
ser = serial.Serial('COM3', 9600,timeout=1)
cap=cv.VideoCapture(0)
cv.namedWindow("scale")
i=0
array=np.empty([10,3])
while i<10:
ret,frame=cap.read()
img=frame.copy()
img=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
img=cv.medianBlur(img,3)
circles=cv.HoughCircles(img,cv.HOUGH_GRADIENT,1,500,param1=10,param2=200,minRadius=0,maxRadius=0)
if circles is not None:
print(circles)
array[i]=circles
i+=1
detected_circles=np.uint32(np.round(circles))
for (x,y,r) in detected_circles[0, :]:
cv.circle(frame,(x,y), r,(0,255,255), 6)
cv.circle(frame, (x, y), 2, 6)
k=cv.waitKey(1)
if k==27:
break
cv.imshow("frame",frame)
cv.imshow("img",img)
print(array) #array formed
cap.release()
cv.destroyAllWindows()
x=0
y=0
r=0
for i in range (10):
x=x+array[i][0]
y=y+array[i][1]
r=r+array[i][2]
x=x/10
y=y/10
r=r/10
print(x ,y , r )
initial_x=0
initial_y=0
initial_z=420
angle_x=int((x-initial_x)/initial_z*57.3)
angle_y=int(((y-initial_x)/initial_z)*57.3)
print(angle_x, angle_y)
angle_x=format(int(angle_x),'03d')
angle_y=format(int(angle_y),'03d')
angle= 'f'+str(angle_x)+ str(angle_y)+'t'
print(angle)
cv.imshow("frame",frame)
while True:
ser.write(angle.encode())
if cv.waitKey()==27:
break
# x=ser.read()
cv.destroyAllWindows()
print(x)