-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor_detector.py
More file actions
34 lines (27 loc) · 934 Bytes
/
Copy pathcolor_detector.py
File metadata and controls
34 lines (27 loc) · 934 Bytes
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
import cv2
import numpy as np
import pyautogui
cap = cv2.VideoCapture(0)
prev_y = 0
lower_white = np.array([22, 93, 0])
upper_white = np.array([45, 255, 255])
while True:
ret, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower_white, upper_white)
if cv2.waitKey(10) == ord('q'):
break
contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
area = cv2.contourArea(c)
if area > 300:
x, y, w, h = cv2.boundingRect(c)
cv2.rectangle(frame, (x, y), (x+w, y + h), (0, 255, 0), 2)
# cv2.drawContours(frame, c, -1, (0, 255, 0), 2)
if y < prev_y:
pyautogui.press('space')
prev_y = y
cv2.imshow('frame', frame)
# cv2.imshow('mask', mask)
cap.release()
cv2.destroyAllWindows()