-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathtcam_webserver.py
More file actions
79 lines (61 loc) · 2.15 KB
/
tcam_webserver.py
File metadata and controls
79 lines (61 loc) · 2.15 KB
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
import logging
import time
import sys
# pip3 install flask
from flask import Flask, Response, abort
sys.path.append("../python-common")
from TIS import TIS, SinkFormats
app = Flask(__name__)
DEVICE_FRAMERATE = "15/1"
OUTPUT_FRAMERATE = "10/1"
# Create the camera object and start the stream
camera = TIS()
camera.open_device(None, 640, 480, f"{DEVICE_FRAMERATE}", SinkFormats.BGRA, False,
conversion=f"videorate ! video/x-raw,framerate={OUTPUT_FRAMERATE} ! videoconvert ! jpegenc quality=60")
camera.start_pipeline()
# reference point in time to calculate current FPS
t_fps_start = None
fps_frame_count = 0
current_fps = 0.0
def imagegenerator():
global t_fps_start
global fps_frame_count
global current_fps
try:
while True:
frame = camera.snap_image(1000)
if frame is not None:
fps_frame_count += 1
if t_fps_start is None:
t_fps_start = time.time()
dt = time.time() - t_fps_start
if dt >= 10.0:
current_fps = fps_frame_count / dt
logging.info("Current FPS: %.1f", current_fps)
fps_frame_count = 0
t_fps_start = time.time()
# Create the boundary between the sent jpegs
yield (b'--imagingsource\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
else:
logging.info("No frame received.")
finally:
logging.info("Stream closed")
@app.route('/mjpeg_stream')
def imagestream():
logging.info("Starting mjpeg stream")
return Response(imagegenerator(), mimetype='multipart/x-mixed-replace; boundary=imagingsource')
@app.route('/', methods=['GET', 'POST'])
def index():
try:
with open("index.html", "r", encoding="UTF-8") as f:
return Response(f.read(), mimetype="text/html")
except FileNotFoundError as e:
abort(404, str(e))
return None
def main():
logging.basicConfig(level=logging.INFO)
logging.info("Logging enabled")
app.run(host='0.0.0.0', threaded=True)
if __name__ == "__main__":
main()