-
Notifications
You must be signed in to change notification settings - Fork 9
/
camera.py
81 lines (60 loc) · 2.07 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
73
74
75
76
77
78
79
80
81
import cv2
import threading
from pose import decorate
class RecordingThread (threading.Thread):
def __init__(self, name, camera):
threading.Thread.__init__(self)
self.name = name
self.isRunning = True
self.cap = camera
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
self.out = cv2.VideoWriter('output.mp4',-1, 20.0, (640,480))
def run(self):
while self.isRunning:
ret, frame = self.cap.read()
if ret:
self.out.write(frame)
self.out.release()
def stop(self):
self.isRunning = False
def __del__(self):
self.out.release()
class VideoCamera(object):
def __init__(self):
# Open a camera
self.cap = cv2.VideoCapture(0)
# Initialize video recording environment
self.is_record = False
self.out = None
# Thread for recording
self.recordingThread = None
def __del__(self):
self.cap.release()
def get_frame(self):
ret, frame = self.cap.read()
frame = decorate(frame)
if ret:
ret, jpeg = cv2.imencode('.jpg', frame)
# Record video
# if self.is_record:
# if self.out == None:
# fourcc = cv2.VideoWriter_fourcc(*'MJPG')
# self.out = cv2.VideoWriter('./static/video.avi',fourcc, 20.0, (640,480))
# ret, frame = self.cap.read()
# if ret:
# self.out.write(frame)
# else:
# if self.out != None:
# self.out.release()
# self.out = None
return jpeg.tobytes()
else:
return None
def start_record(self):
self.is_record = True
self.recordingThread = RecordingThread("Video Recording Thread", self.cap)
self.recordingThread.start()
def stop_record(self):
self.is_record = False
if self.recordingThread != None:
self.recordingThread.stop()