-
Notifications
You must be signed in to change notification settings - Fork 143
/
webcam.py
41 lines (35 loc) · 1.25 KB
/
webcam.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
import cv2
import numpy as np
import time
class Webcam(object):
def __init__(self):
#print ("WebCamEngine init")
self.dirname = "" #for nothing, just to make 2 inputs the same
self.cap = None
def start(self):
print("[INFO] Start webcam")
time.sleep(1) # wait for camera to be ready
self.cap = cv2.VideoCapture(0)
self.valid = False
try:
resp = self.cap.read()
self.shape = resp[1].shape
self.valid = True
except:
self.shape = None
def get_frame(self):
if self.valid:
_,frame = self.cap.read()
frame = cv2.flip(frame,1)
# cv2.putText(frame, str(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
# (65,220), cv2.FONT_HERSHEY_PLAIN, 2, (0,256,256))
else:
frame = np.ones((480,640,3), dtype=np.uint8)
col = (0,256,256)
cv2.putText(frame, "(Error: Camera not accessible)",
(65,220), cv2.FONT_HERSHEY_PLAIN, 2, col)
return frame
def stop(self):
if self.cap is not None:
self.cap.release()
print("[INFO] Stop webcam")