-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcamera.py
72 lines (56 loc) · 1.72 KB
/
camera.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
Capture and stream video
"""
from __future__ import print_function
import cv2
import numpy as np
class Video(object):
"""
Class to capture video and sen the frame
"""
def __init__(self, source=0):
"""
if source is 0, captures video from the device camera
if source is path to video file, captures video from source file
"""
self.video = cv2.VideoCapture(source)
self.shown = False
def __del__(self):
self.video.release()
def get_frame(self):
"""
Since opencv captures raw images, we should encode it
into JPEG
Returns image as byte stream.
"""
success, image = self.video.read()
# We are using Motion JPEG, but OpenCV defaults to capture raw images,
# so we must encode it into JPEG in order to correctly display the
# video stream.
if not success:
raise Exception
return self._convet_to_byte_stream(image), image
def _convet_to_byte_stream(self, image):
ret, jpeg = cv2.imencode('.jpg', image)
if not ret:
raise Exception
return jpeg.tobytes()
def save_image(self, image):
"""
Save the frame(byte stream) to a specified location
"""
# CV2
cv2.imwrite("newimage.jpg", image)
def display_video(self):
"""
Display video in opencv destroyAllWindows
"""
while True:
ret, img = self.video.read()
print(ret)
cv2.imshow("input", img)
key = cv2.waitKey(10)
if key == 27:
break
cv2.destroyAllWindows()
self.video.release()