-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.py
91 lines (73 loc) · 3.42 KB
/
camera.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
import cv2 as cv
import numpy as np
class Video (object):
def __init__(self):
self.camera = cv.VideoCapture(0)
def __del__(self):
self.camera.release()
def area(self, green_extrac , radius):
C_x , C_y = (green_extrac.shape[0])/2, (green_extrac.shape[1])/2
area_covered = str( round( ((3.14159265*pow(radius,2)) * 100 )/(C_x * C_y), 2) )
return area_covered
def corner(self,green_extrac,x,y):
C_x , C_y = (green_extrac.shape[0])/2, (green_extrac.shape[1])/2
if(x < C_x and y < C_y):
return("Top Left Corner")
elif(x > C_x and y < C_y):
return("Top Right Corner")
elif (x == C_x and y == C_y):
return ("At the Center")
elif(x < C_x and y > C_y):
return("Bottom Left Corner")
else:
return("Bottom Right Corner")
def main_exec(self):
present = False
area, position = "NULL", "NULL"
green_L_hsv = np.array([39, 140, 50])
green_U_hsv = np.array([80, 255, 255])
while True:
response, frame = self.camera.read()
if frame is None:
break
frame_hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
frame_hsv = cv.medianBlur(frame_hsv, 5)
green_extract = cv.inRange(frame_hsv,green_L_hsv,green_U_hsv)
green_extract = cv.erode(green_extract, None, iterations=2)
green_extract = cv.dilate(green_extract, None, iterations=2)
possible_balls = cv.HoughCircles(green_extract, cv.HOUGH_GRADIENT, 1, 25, param1=2, param2=20,minRadius=0, maxRadius=0)
if possible_balls is not None:
present = True
x,y,radius = map(int,possible_balls[0][0])
area = self.area(green_extract.copy(), radius)
position = self.corner(green_extract.copy(), x, y)
cv.putText(frame, "Green Ball" ,(x,y),cv.FONT_HERSHEY_SIMPLEX,1,(255,255,255),3)
cv.circle(frame, (x,y), radius,(80, 255, 255), 2)
"""
------------Old Code written for SubTask 1-----------------------
#Boundary/Outline for the Green Ball
boundary, hierarchy= cv.findContours(green_extract.copy(), cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)
#Contour Area
if(len(boundary)>0):
max_contour = max(boundary, key=cv.contourArea)
((x, y), radius) = cv.minEnclosingCircle(max_contour )
points = cv.moments(max_contour)
#Finding the center
center = (int(points['m10'] / points['m00']), int(points['m01'] / points['m00']))
if radius > 20 :
cv.circle(frame, (int(x), int(y)), int(radius),(255, 255, 255), 5)
print("FOUND")
else:
print("NOT FOUND")
cv.imshow("The Actual Frame",frame)
cv.imshow("Extracted Green ball", green_extract)
"""
if cv.waitKey(1) & 0xFF == ord('d'):
break
ret, jpeg = cv.imencode('.jpg', frame)
return jpeg.tobytes(), position, area, present
"""
obj = Video()
obj.main_exec()
cv.destroyAllWindows()
"""