-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcameraFeed.py
43 lines (33 loc) · 1.19 KB
/
cameraFeed.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
"""
Simply display the contents of the webcam with optional mirroring using OpenCV
via the new Pythonic cv2 interface. Press <esc> to quit.
"""
import cv2
class Camera:
def __init__(self, webcam=0, mirror=True, resize=False, width=224, height=224):
self.mirror = mirror
self.resize = resize
self.width = width
self.height = height
self.source = cv2.VideoCapture(webcam)
def getImage(self):
if not self.source:
self.source = cv2.VideoCapture(0)
while True:
ret, img = self.source.read()
if ret and self.mirror:
img = cv2.flip(img, 1)
if ret and self.resize:
img = cv2.resize(img, (self.width, self.height),
interpolation=cv2.INTER_AREA)
yield img
if __name__ == '__main__':
width, height=(224, 224)
camera = Camera(resize=True)
image = camera.getImage()
for img in image:
cv2.imshow('yield', img)
cv2.namedWindow('yield',cv2.WINDOW_NORMAL)
if cv2.waitKey(30) == 27:
break # esc to quit
cv2.destroyAllWindows()