-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgaedetectFRC.py
143 lines (117 loc) · 5.37 KB
/
algaedetectFRC.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
# For usage with robot external webcam
import cv2
import numpy as np
import depthai as dai
import time
def robot():
# Create pipeline
pipeline = dai.Pipeline()
cam = pipeline.create(dai.node.ColorCamera)
cam.setColorOrder(dai.ColorCameraProperties.ColorOrder.BGR)
cam.setPreviewSize(1280,720)
xout = pipeline.create(dai.node.XLinkOut)
xout.setStreamName("rgb")
cam.preview.link(xout.input)
# Connect to device and start pipeline
with dai.Device(pipeline) as device:
qRgb = device.getOutputQueue(name="rgb", maxSize=4, blocking=False)
#cam = cv2.VideoCapture(0)
while True:
start = time.perf_counter()
frame = qRgb.get().getFrame()
#ret, frame = cam.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Threshold of blue in HSV space
lower_blue = np.array([60, 88, 18])
upper_blue = np.array([93, 255, 255])
# preparing the mask to overlay
mask = cv2.inRange(hsv, lower_blue, upper_blue)
blurry_mask = cv2.GaussianBlur(mask, (21,21), 0)
# Apply Hough transform on the blurred image.
detected_circles = cv2.HoughCircles(blurry_mask,
cv2.HOUGH_GRADIENT, 1, 70, param1 = 260,
param2 = 30, minRadius = 50, maxRadius = 280)
# Draw circles that are detected and the frame rate.
detections = {}
if detected_circles is not None:
# Convert the circle parameters a, b and r to integers.
detected_circles = np.uint16(np.around(detected_circles))
count = 0
for pt in detected_circles[0, :]:
cx, cy, r = pt[0], pt[1], pt[2]
#detections.update()
algae = {}
algae["cx"] = int(cx)
algae["cy"] = int(cy)
algae["r"] = int(r)
detections[f"algae{count}"] = algae
count += 1
# Draw the circumference of the circle.
cv2.circle(frame, (cx, cy), r, (0, 255, 0), 2)
cv2.circle(blurry_mask, (cx, cy), r, (0, 255, 255), 2)
# Draw a small circle (of radius 1) to show the center.
cv2.circle(frame, (cx, cy), 1, (0, 255, 0), 3)
cv2.circle(blurry_mask, (cx, cy), 1, (0, 0, 255), 3)
#Draw the frame rate onto the frame
end = time.perf_counter()
fps = 1/(end-start)
cv2.putText(blurry_mask, f"Frame Rate: {int(fps)}",(7,70), cv2.FONT_HERSHEY_SIMPLEX , 3, (255,255,255), 2, cv2.LINE_AA)
cv2.imshow("Full Color Detection", frame)
cv2.imshow("Masked Detection", blurry_mask)
time.sleep(0.002)
print(detections)
if cv2.waitKey(1) == ord("q"):
break
cv2.destroyAllWindows()
def webcam():
# For usage with computer webcam
import cv2
import numpy as np
import time
cam = cv2.VideoCapture(0)
while True:
start = time.perf_counter()
ret, frame = cam.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Threshold of blue in HSV space
lower_blue = np.array([60, 88, 18])
upper_blue = np.array([93, 255, 255])
# preparing the mask to overlay
mask = cv2.inRange(hsv, lower_blue, upper_blue)
blurry_mask = cv2.GaussianBlur(mask, (21,21), 0)
# Apply Hough transform on the blurred image.
detected_circles = cv2.HoughCircles(blurry_mask,
cv2.HOUGH_GRADIENT, 1, 70, param1 = 260,
param2 = 30, minRadius = 50, maxRadius = 280)
# Draw circles that are detected and the frame rate.
detections = {}
if detected_circles is not None:
# Convert the circle parameters a, b and r to integers.
detected_circles = np.uint16(np.around(detected_circles))
count = 0
for pt in detected_circles[0, :]:
cx, cy, r = pt[0], pt[1], pt[2]
#detections.update()
algae = {}
algae["cx"] = int(cx)
algae["cy"] = int(cy)
algae["r"] = int(r)
detections[f"algae{count}"] = algae
count += 1
# Draw the circumference of the circle.
cv2.circle(frame, (cx, cy), r, (0, 255, 0), 2)
cv2.circle(blurry_mask, (cx, cy), r, (0, 255, 255), 2)
# Draw a small circle (of radius 1) to show the center.
cv2.circle(frame, (cx, cy), 1, (0, 255, 0), 3)
cv2.circle(blurry_mask, (cx, cy), 1, (0, 0, 255), 3)
#Draw the frame rate onto the frame
end = time.perf_counter()
fps = 1/(end-start)
cv2.putText(blurry_mask, f"Frame Rate: {int(fps)}",(7,70), cv2.FONT_HERSHEY_SIMPLEX , 3, (255,255,255), 2, cv2.LINE_AA)
cv2.imshow("Full Color Detection", frame)
cv2.imshow("Masked Detection", blurry_mask)
time.sleep(0.002)
print(detections)
if cv2.waitKey(1) == ord("q"):
break
cv2.destroyAllWindows()