-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathopen_multiWebcam.py
49 lines (34 loc) · 1.11 KB
/
open_multiWebcam.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
# an example to open 2 usb webcam using opencv python module
# caveat: since each thread will each produce 1 imshow window, user needs to manually close the window one at a time using "Esc" key
# author: yptheangel
import cv2
import threading
keyPressed = -1
print("OpenCV version: "+cv2.__version__)
def cameraThread1():
#0 is the first camera
cap = cv2.VideoCapture(0)
while cap.isOpened():
_, frame = cap.read()
cv2.imshow('Camera 0',frame)
keyPressed = cv2.waitKey(1)
#the program will exit if 'Esc' key is pressed
if keyPressed == 27:
break
cap.release()
def cameraThread2():
#1 is the second webcam
cap1 = cv2.VideoCapture(1)
while cap1.isOpened():
_, frame1 = cap1.read()
cv2.imshow('Camera 1',frame1)
keyPressed = cv2.waitKey(1)
if keyPressed == 27:
break
cap1.release()
t1 = threading.Thread(name='Thread1', target=cameraThread1)
t2 = threading.Thread(name='Thread2', target=cameraThread2)
t1.start()
t2.start()
#Destroy all windows
cv2.destroyAllWindows()