-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
73 lines (63 loc) · 2.23 KB
/
app.py
File metadata and controls
73 lines (63 loc) · 2.23 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
# app.py
from flask import Flask, render_template, Response, jsonify, request
from camera import VideoCamera, STABILITY_SECONDS, CONF_THRESHOLD
import os
import time
app = Flask(__name__)
# Global instance of the video camera class
cam = None
# Ensure the camera is initialized only once
def get_camera():
global cam
if cam is None:
try:
cam = VideoCamera()
except IOError as e:
print(f"Failed to initialize camera: {e}")
return None
return cam
@app.route('/')
def index():
"""Video streaming home page."""
# Pass settings to the frontend for display
return render_template('index.html',
stability_s=STABILITY_SECONDS,
conf_thresh=CONF_THRESHOLD)
def gen(camera):
"""Video streaming generator function."""
while True:
frame = camera.get_frame()
# Yield the frame in Motion JPEG format
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
# Control the frame rate (e.g., 30 FPS)
time.sleep(1/30)
@app.route('/video_feed')
def video_feed():
"""Route to stream the video frame by frame."""
camera = get_camera()
if camera is None:
return Response("Error: Camera not available.", status=500)
return Response(gen(camera),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/get_output')
def get_output():
"""API endpoint to check for new characters and append them."""
camera = get_camera()
if camera is None:
return jsonify(output="", status="error")
output = ""
# Empty the queue, collecting all committed characters
while not camera.output_queue.empty():
try:
output += camera.output_queue.get_nowait()
except queue.Empty:
break
return jsonify(output=output, status="ok")
if __name__ == '__main__':
# Ensure all model paths are valid before starting
if not os.path.exists("project/Model"):
print("ERROR: 'project/Model' directory not found. Please check paths.")
else:
# Start the Flask development server
app.run(host='0.0.0.0', debug=False, threaded=True)