-
Notifications
You must be signed in to change notification settings - Fork 11
/
mjpegsw.py
256 lines (221 loc) · 6.58 KB
/
mjpegsw.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/python
"""
Author: Marco Mescalchin
Mjpg stream Server for Mac Webcam
"""
import argparse
import os
import signal
import threading
from io import BytesIO
from threading import Lock
from time import sleep
import cv2
from PIL import Image
from flask import Flask, Response, redirect, send_file, url_for
app = Flask(__name__)
img_lock = Lock()
class CameraControl:
def __init__(self):
self.capturing = True
self.img = None
self.lock = Lock()
def stop_capturing(self):
with self.lock:
self.capturing = False
def start_capturing(self):
with self.lock:
self.capturing = True
def update_image(self, new_img):
with self.lock:
self.img = new_img
def get_image(self):
with self.lock:
return self.img
def is_capturing(self):
with self.lock:
return self.capturing
camera_control = CameraControl()
def signal_handler_sigint(signal_number, frame):
print("Stopping camera ...")
camera_control.stop_capturing()
sleep(0.5)
raise RuntimeError("SIGINT received")
signal.signal(signal.SIGINT, signal_handler_sigint)
class CamDaemon(threading.Thread):
def __init__(
self,
camera_control,
camera,
capture_width,
capture_height,
capture_api,
rotate_image=False,
delay=0.2,
):
threading.Thread.__init__(self)
self.camera_control = camera_control
self.camera = camera
self.capture_width = capture_width
self.capture_height = capture_height
self.rotate_image = rotate_image
self.capture_api = capture_api
self.delay = delay
def run(self):
while self.camera_control.is_capturing():
self.capture()
sleep(5)
# when cv2 crashes, it does not release the camera, so we need to exit
os._exit(0)
def capture(self):
if self.capture_api and hasattr(cv2, self.capture_api):
capture = cv2.VideoCapture(self.camera, getattr(cv2, self.capture_api))
else:
capture = cv2.VideoCapture(self.camera)
if self.capture_width:
capture.set(cv2.CAP_PROP_FRAME_WIDTH, self.capture_width)
if self.capture_height:
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, self.capture_height)
capture.setExceptionMode(True)
while self.camera_control.is_capturing():
try:
ret, frame = capture.read()
if self.rotate_image:
frame = cv2.rotate(frame, cv2.ROTATE_180)
if ret:
self.camera_control.update_image(frame)
if self.delay > 0:
sleep(self.delay)
except Exception as e:
print("Error: " + str(e))
self.camera_control.stop_capturing()
break
capture.release()
def create_stream_frame(camera_control):
while True:
img = camera_control.get_image()
if img is not None:
try:
_, _buffer = cv2.imencode(".jpg", img)
frame = _buffer.tobytes()
yield (b"--frame\r\nContent-Type: image/jpeg\r\n\r\n" + frame + b"\r\n")
except Exception as e:
print("Failed to encode image: " + str(e))
continue
sleep(0.1)
@app.route("/")
def hello_world():
return redirect(url_for("video"))
@app.route("/cam.mjpg")
def video():
return Response(
create_stream_frame(camera_control),
mimetype="multipart/x-mixed-replace; boundary=frame",
)
@app.route("/snap.jpg")
def snap():
# check if camera is capturing and return an empty buffer if not instead of an error
if not camera_control.is_capturing() or camera_control.img is None:
return send_file(BytesIO(), download_name="snap.jpg", mimetype="image/jpeg")
img_rgb = cv2.cvtColor(camera_control.img, cv2.COLOR_BGR2RGB)
jpeg = Image.fromarray(img_rgb)
buffer_file = BytesIO()
jpeg.save(buffer_file, "JPEG")
buffer_file.seek(0)
return send_file(buffer_file, download_name="snap.jpg", mimetype="image/jpeg")
def handle_args():
parser = argparse.ArgumentParser(
description="Mjpeg streaming server: mjpegsw -p 8080 --camera 2"
)
parser.add_argument(
"-p",
"--port",
help="http listening port, default 5001",
type=int,
default=5001,
)
parser.add_argument(
"-c",
"--camera",
help="opencv camera number, ex. -c 1",
type=int,
default=0,
)
parser.add_argument(
"-i",
"--ipaddress",
help="listening ip address, default all ips",
type=str,
default="127.0.0.1",
)
parser.add_argument(
"-w",
"--width",
help="capture resolution width",
type=int,
required=False,
)
parser.add_argument(
"-x",
"--height",
help="capture resolution height",
type=int,
required=False,
)
parser.add_argument(
"-r",
"--rotate",
help="rotate image 180 degrees",
action="store_true",
)
parser.add_argument(
"-a",
"--capture_api",
help="specific api for capture",
type=str,
required=False,
)
parser.add_argument(
"-d",
"--delay",
help="delay between captures (seconds)",
type=float,
required=False,
default=1,
)
params = vars(parser.parse_args())
return params
def main():
params = handle_args()
if params["height"]:
print("Image height set to: " + str(params["height"]))
if params["width"]:
print("Image width set to: " + str(params["width"]))
if params["rotate"]:
print("Image will be rotated 180 degrees")
if params["capture_api"]:
print("Will be used capture api: " + params["capture_api"])
if params["delay"] > 0:
print(
"Will be used delay between captures: " + str(params["delay"]) + " seconds"
)
# starts camera daemon thread
camera = CamDaemon(
camera_control,
params["camera"],
params["width"],
params["height"],
params["capture_api"],
params["rotate"],
params["delay"],
)
camera.daemon = True
camera.start()
try:
# starts flask server
app.run(host=params["ipaddress"], port=params["port"], debug=False)
except RuntimeError:
print("Stopping mjpeg server ...")
camera.join()
if __name__ == "__main__":
main()