-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj_detection_video.py
48 lines (36 loc) · 1.28 KB
/
obj_detection_video.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
import cv2
import numpy as np
def nothing(x):
pass
cap = cv2.VideoCapture(0)
cv2.namedWindow('Tracking')
cv2.createTrackbar('LowerHue', 'Tracking', 0, 255, nothing)
cv2.createTrackbar('LowerSat', 'Tracking', 0, 255, nothing)
cv2.createTrackbar('LowerVal', 'Tracking', 0, 255, nothing)
cv2.createTrackbar('UpperHue', 'Tracking', 0, 255, nothing)
cv2.createTrackbar('UpperSat', 'Tracking', 0, 255, nothing)
cv2.createTrackbar('UpperVal', 'Tracking', 0, 255, nothing)
while True:
_, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
l_h = cv2.getTrackbarPos("LowerHue", "Tracking")
l_s = cv2.getTrackbarPos("LowerSat", "Tracking")
l_v = cv2.getTrackbarPos("LowerVal", "Tracking")
u_h = cv2.getTrackbarPos("UpperHue", "Tracking")
u_s = cv2.getTrackbarPos("UpperSat", "Tracking")
u_v = cv2.getTrackbarPos("UpperVal", "Tracking")
#blue
#l_b = np.array([110, 50, 50])
#u_b = np.array([130, 255, 255])
l_b = np.array([l_h, l_s, l_v])
u_b = np.array([u_h, u_s, u_v])
mask = cv2.inRange(hsv, l_b, u_b)
res = cv2.bitwise_and(frame, frame, mask=mask)
cv2.imshow('frame', frame)
cv2.imshow('mask', mask)
cv2.imshow('result', res)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()