-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmainpcode.py
More file actions
48 lines (37 loc) · 1.33 KB
/
mainpcode.py
File metadata and controls
48 lines (37 loc) · 1.33 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
import cv2
import numpy as np
def identify_green(frame):
"""Detects green objects in the frame and sends a signal to a solenoid valve (Raspberry Pi specific).
Args:
frame (numpy.ndarray): The frame captured from the video stream.
Returns:
None
"""
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_green = np.array([40, 50, 50])
upper_green = np.array([80, 255, 255])
mask = cv2.inRange(hsv, lower_green, upper_green)
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
green_detected = False
for cnt in contours:
# Calculate area of the contour
area = cv2.contourArea(cnt)
# Adjust minimum area threshold as needed
if area > 1000: # Adjust threshold based on object size and camera distance
green_detected = True
cv2.drawContours(frame, [cnt], 0, (0, 255, 0), 2)
break # Exit loop after finding one green object
# Send signal to solenoid valve (Raspberry Pi specific)
cv2.imshow("Green Detection", frame)
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if ret:
identify_green(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
print("Error: Frame not captured")
break
cap.release()
cv2.destroyAllWindows()