Spaces:
Sleeping
Sleeping
remove thread lock
Browse files
app.py
CHANGED
@@ -15,8 +15,6 @@ class Session:
|
|
15 |
app = Flask(__name__)
|
16 |
PORT = int(os.environ.get("PORT", 5000))
|
17 |
|
18 |
-
lock = threading.Lock()
|
19 |
-
|
20 |
sessions: set[Session] = set()
|
21 |
|
22 |
def frame_processor(session_id: str):
|
@@ -44,18 +42,17 @@ def frame_processor(session_id: str):
|
|
44 |
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
|
45 |
continue
|
46 |
else:
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
session.latest_frame = frame
|
59 |
|
60 |
# measure FPS
|
61 |
frame_count += 1
|
@@ -82,11 +79,10 @@ def stream_mjpeg(session_id: str):
|
|
82 |
while True:
|
83 |
if session.latest_frame is None:
|
84 |
continue
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
# print(f"frame filesize: {len(frame_bytes) / (1024 * 1024):.4f} MB")
|
90 |
if frame_bytes is not None:
|
91 |
yield (b"--frame\r\n"b"Content-Type: image/jpeg\r\n\r\n" + frame_bytes + b"\r\n")
|
92 |
|
|
|
15 |
app = Flask(__name__)
|
16 |
PORT = int(os.environ.get("PORT", 5000))
|
17 |
|
|
|
|
|
18 |
sessions: set[Session] = set()
|
19 |
|
20 |
def frame_processor(session_id: str):
|
|
|
42 |
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
|
43 |
continue
|
44 |
else:
|
45 |
+
if session.show_fps:
|
46 |
+
text = f"{session_id}: {session.measured_fps:.0f}"
|
47 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
48 |
+
font_scale = 1
|
49 |
+
thickness = 2
|
50 |
+
color = (0, 255, 0)
|
51 |
+
(text_width, text_height), _ = cv2.getTextSize(text, font, font_scale, thickness)
|
52 |
+
x = frame.shape[1] - text_width - 10
|
53 |
+
y = 30
|
54 |
+
cv2.putText(frame, text, (x, y), font, font_scale, color, thickness, cv2.LINE_AA)
|
55 |
+
session.latest_frame = frame
|
|
|
56 |
|
57 |
# measure FPS
|
58 |
frame_count += 1
|
|
|
79 |
while True:
|
80 |
if session.latest_frame is None:
|
81 |
continue
|
82 |
+
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 50]
|
83 |
+
ret, jpeg = cv2.imencode(".jpg", session.latest_frame, encode_param)
|
84 |
+
frame_bytes = jpeg.tobytes() if ret else None
|
85 |
+
# print(f"frame filesize: {len(frame_bytes) / (1024 * 1024):.4f} MB")
|
|
|
86 |
if frame_bytes is not None:
|
87 |
yield (b"--frame\r\n"b"Content-Type: image/jpeg\r\n\r\n" + frame_bytes + b"\r\n")
|
88 |
|