Spaces:
Sleeping
Sleeping
refactor: simplify Session initialization and update stats display logic
Browse files
app.py
CHANGED
@@ -7,12 +7,12 @@ import os
|
|
7 |
import psutil
|
8 |
|
9 |
class Session:
|
10 |
-
def __init__(self, id: str
|
11 |
self.id = id
|
12 |
-
self.latest_frame: MatLike =
|
13 |
-
self.measured_fps =
|
14 |
-
self.cpu_usage =
|
15 |
-
self.
|
16 |
|
17 |
app = Flask(__name__)
|
18 |
PORT = int(os.environ.get("PORT", 5000))
|
@@ -53,18 +53,22 @@ def frame_processor(session_id: str):
|
|
53 |
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
|
54 |
continue
|
55 |
else:
|
56 |
-
if session.
|
57 |
font = cv2.FONT_HERSHEY_SIMPLEX
|
58 |
font_scale = 1
|
59 |
thickness = 2
|
60 |
color = (0, 255, 0)
|
61 |
-
text =
|
|
|
|
|
|
|
|
|
62 |
|
63 |
y0, dy = 30, 36
|
64 |
for i, line in enumerate(text.split('\n')):
|
65 |
(text_width, text_height), _ = cv2.getTextSize(line, font, font_scale, thickness)
|
66 |
x = frame.shape[1] - text_width - 10
|
67 |
-
y = y0 + i*dy
|
68 |
cv2.putText(frame, line, (x, y), font, font_scale, color, thickness, cv2.LINE_AA)
|
69 |
|
70 |
# control framerate
|
@@ -100,7 +104,7 @@ def video_feed():
|
|
100 |
session_id = request.args.get("id", "default")
|
101 |
|
102 |
if session_id not in [s.id for s in sessions]:
|
103 |
-
session = Session(session_id
|
104 |
sessions.add(session)
|
105 |
threading.Thread(target=frame_processor, args=[session_id], daemon=True).start()
|
106 |
|
|
|
7 |
import psutil
|
8 |
|
9 |
class Session:
|
10 |
+
def __init__(self, id: str):
|
11 |
self.id = id
|
12 |
+
self.latest_frame: MatLike = None
|
13 |
+
self.measured_fps = 0
|
14 |
+
self.cpu_usage = 0
|
15 |
+
self.show_stats = os.environ.get("SHOW_STATS", "0") == "1"
|
16 |
|
17 |
app = Flask(__name__)
|
18 |
PORT = int(os.environ.get("PORT", 5000))
|
|
|
53 |
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
|
54 |
continue
|
55 |
else:
|
56 |
+
if session.show_stats:
|
57 |
font = cv2.FONT_HERSHEY_SIMPLEX
|
58 |
font_scale = 1
|
59 |
thickness = 2
|
60 |
color = (0, 255, 0)
|
61 |
+
text =f"""
|
62 |
+
ID: {session_id}
|
63 |
+
FPS: {session.measured_fps:.0f}
|
64 |
+
CPU: {session.cpu_usage:02.0f}%
|
65 |
+
"""
|
66 |
|
67 |
y0, dy = 30, 36
|
68 |
for i, line in enumerate(text.split('\n')):
|
69 |
(text_width, text_height), _ = cv2.getTextSize(line, font, font_scale, thickness)
|
70 |
x = frame.shape[1] - text_width - 10
|
71 |
+
y = y0 + (i - 1) * dy
|
72 |
cv2.putText(frame, line, (x, y), font, font_scale, color, thickness, cv2.LINE_AA)
|
73 |
|
74 |
# control framerate
|
|
|
104 |
session_id = request.args.get("id", "default")
|
105 |
|
106 |
if session_id not in [s.id for s in sessions]:
|
107 |
+
session = Session(session_id)
|
108 |
sessions.add(session)
|
109 |
threading.Thread(target=frame_processor, args=[session_id], daemon=True).start()
|
110 |
|