jukrapopk commited on
Commit
95b7f7e
·
1 Parent(s): 60929a9

refactor: extract draw_stats function

Browse files
Files changed (1) hide show
  1. app.py +19 -16
app.py CHANGED
@@ -20,6 +20,24 @@ PORT = int(os.environ.get("PORT", 5000))
20
 
21
  sessions: dict[str, Session] = {}
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  def process_frame(session_id: str):
24
  global sessions
25
 
@@ -59,22 +77,7 @@ def process_frame(session_id: str):
59
  continue
60
  else:
61
  if session.show_stats:
62
- font = cv2.FONT_HERSHEY_SIMPLEX
63
- font_scale = 1
64
- thickness = 2
65
- color = (0, 255, 0)
66
- text = f"""
67
- ID: {session_id}
68
- FPS: {session.measured_fps:.0f}
69
- CPU: {session.cpu_usage:02.0f}%
70
- """
71
-
72
- y0, dy = 30, 36
73
- for i, line in enumerate(text.split('\n')):
74
- (text_width, text_height), _ = cv2.getTextSize(line, font, font_scale, thickness)
75
- x = frame.shape[1] - text_width - 10
76
- y = y0 + (i - 1) * dy
77
- cv2.putText(frame, line, (x, y), font, font_scale, color, thickness, cv2.LINE_AA)
78
  session.latest_frame = frame
79
 
80
  next_frame_time += frame_duration
 
20
 
21
  sessions: dict[str, Session] = {}
22
 
23
+ def draw_stats(frame: MatLike, session: Session):
24
+ font = cv2.FONT_HERSHEY_SIMPLEX
25
+ font_scale = 1
26
+ thickness = 2
27
+ color = (0, 255, 0)
28
+ text = f"""
29
+ ID: {session.id}
30
+ FPS: {session.measured_fps:.0f}
31
+ CPU: {session.cpu_usage:02.0f}%
32
+ """
33
+
34
+ y0, dy = 30, 36
35
+ for i, line in enumerate(text.split('\n')):
36
+ (text_width, text_height), _ = cv2.getTextSize(line, font, font_scale, thickness)
37
+ x = frame.shape[1] - text_width - 10
38
+ y = y0 + (i - 1) * dy
39
+ cv2.putText(frame, line, (x, y), font, font_scale, color, thickness, cv2.LINE_AA)
40
+
41
  def process_frame(session_id: str):
42
  global sessions
43
 
 
77
  continue
78
  else:
79
  if session.show_stats:
80
+ draw_stats(frame, session)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  session.latest_frame = frame
82
 
83
  next_frame_time += frame_duration