jukrapopk commited on
Commit
a679fba
·
1 Parent(s): 848ba92

refactor: check ret before write fps + show filesize

Browse files
Files changed (1) hide show
  1. src/app.py +6 -4
src/app.py CHANGED
@@ -27,11 +27,10 @@ def video_reader():
27
  while True:
28
  ret, frame = cap.read()
29
 
30
- cv2.putText(frame, f"{measured_fps: .0f}", (frame.shape[1] - 70, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
31
- ret, jpeg = cv2.imencode('.jpg', frame)
32
  if ret:
33
  with lock:
34
- latest_frame = jpeg.tobytes()
 
35
  frame_count += 1
36
 
37
  # measure FPS
@@ -52,7 +51,10 @@ def video_reader():
52
  def generate():
53
  while True:
54
  with lock:
55
- frame_bytes = latest_frame
 
 
 
56
  if frame_bytes is not None:
57
  yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame_bytes + b'\r\n')
58
 
 
27
  while True:
28
  ret, frame = cap.read()
29
 
 
 
30
  if ret:
31
  with lock:
32
+ cv2.putText(frame, f"{measured_fps: .0f}", (frame.shape[1] - 70, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
33
+ latest_frame = frame
34
  frame_count += 1
35
 
36
  # measure FPS
 
51
  def generate():
52
  while True:
53
  with lock:
54
+ encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 50]
55
+ ret, jpeg = cv2.imencode('.jpg', latest_frame, encode_param)
56
+ frame_bytes = jpeg.tobytes() if ret else None
57
+ print(f"frame filesize: {len(frame_bytes) / (1024 * 1024):.4f} MB")
58
  if frame_bytes is not None:
59
  yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame_bytes + b'\r\n')
60