-
Notifications
You must be signed in to change notification settings - Fork 0
/
05_aug.py
124 lines (94 loc) · 3.5 KB
/
05_aug.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
# Import openCV library and imutils library
import cv2
import imutils
def search_ball():
pass
# Need to be modified according to pi cam
cam = cv2.VideoCapture(0)
# Assuming ball radius to be 6...To be modified
ball_radius_in_cm = 6
app_radius_in_pix = 150
distance_in_cm = 15
focal_len = (app_radius_in_pix * distance_in_cm) / ball_radius_in_cm
# The lower and upper hsv limits for the masking
greenLower = (50, 60, 20)
greenUpper = (75, 255, 255)
x1 = -1
y1 = -1
radius1 = -1
x2 = 0
y2 = 0
radius2 = 1
# This while loop is the starter of the code and runs immediately as pi is switched on.
# The while loop is getting all the contours captured in the pi cam and then selects the contour having maximum area
# So it is necessary that the ball is kept close to the pi cam,and possess the maximum area
while x1 == -1:
abc, frame = cam.read()
areacont = [0] * 200
dist = [100000] * 200
frame = imutils.resize(frame, width=600)
blurr = cv2.GaussianBlur(frame, (11, 11), 0)
hsv = cv2.cvtColor(blurr, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, greenLower, greenUpper)
mask = cv2.erode(mask, None, iterations=1)
mask = cv2.dilate(mask, None, iterations=1)
mask1 = mask
contour = cv2.findContours(mask1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contour = imutils.grab_contours(contour)
radius = 0
lent = len(contour)
if lent > 0:
for i in range(lent):
cont_ind = contour[i]
areacont[i] = cv2.contourArea(cont_ind)
cont1_pos = areacont.index(max(areacont))
cont_final = contour[cont1_pos]
((x1, y1), radius1) = cv2.minEnclosingCircle(cont_final)
# print((x1,y1))
elif lent is 0:
search_ball()
if radius1 > 10:
cv2.circle(frame, (int(x1), int(y1)), int(radius1), (0, 255, 255), 2)
print(str(x1) + " " + str(y1))
# After the ball is tracked once, this while loop tracks the ball as the contour
# that is nearest to the contour in the previous frame,
# if the ball looses the track of ball that is has no contour on scree it calls search ball function to execute
while True:
dist = [100000] * 200
abc, frame = cam.read()
frame = imutils.resize(frame, width=600)
blurr = cv2.GaussianBlur(frame, (11, 11), 0)
hsv = cv2.cvtColor(blurr, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, greenLower, greenUpper)
mask = cv2.erode(mask, None, iterations=1)
mask = cv2.dilate(mask, None, iterations=1)
mask1 = mask
contour = cv2.findContours(mask1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contour = imutils.grab_contours(contour)
radius = 0
lent = len(contour)
if lent > 0:
for i in range(lent):
cont_ind = contour[i]
((x2, y2), radius2) = cv2.minEnclosingCircle(cont_ind)
dist[i] = abs(x2 - x1)
cont2_pos = dist.index(min(dist))
# print(lent," ",cont2_pos)
cont2 = contour[cont2_pos]
((x2, y2), radius2) = cv2.minEnclosingCircle(cont2)
elif lent is 0:
search_ball()
if radius2 > 10:
cv2.circle(frame, (int(x2), int(y2)), int(radius2), (0, 255, 255), 2)
distance = (focal_len * ball_radius_in_cm) / (2 * radius1)
#print(distance)
x1 = x2
y1 = y2
radius1 = radius2
cv2.imshow("Image1", mask)
cv2.imshow("Image", frame)
h = cv2.waitKey(10)
if not h == -1:
break;
cam.release()
cv2.destroyAllWindows()