qqwjq1981 commited on
Commit
38d8739
·
verified ·
1 Parent(s): 7add5f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -10
app.py CHANGED
@@ -578,25 +578,22 @@ def find_best_subtitle_region(frame, ocr_model, region_height_ratio=0.35, num_st
578
  # Fallback to center-bottom strip
579
  fallback_y = height - int(height * 0.2)
580
  return frame[fallback_y:, :], (fallback_y, height)
581
-
582
- def ocr_frame_worker(args):
583
  frame_idx, frame_time, frame = args
584
 
585
- init_ocr_model() # Ensure model is loaded once per process
586
-
587
- if frame is None or frame.size == 0:
588
- return {"time": frame_time, "text": ""}
589
 
590
- if not isinstance(frame, np.ndarray):
591
  return {"time": frame_time, "text": ""}
592
 
593
  if frame.dtype != np.uint8:
594
  frame = frame.astype(np.uint8)
595
 
596
  try:
597
- subtitle_crop, _ = find_best_subtitle_region(frame, ocr_model)
598
- result = ocr_model.ocr(subtitle_crop, cls=True)
599
- texts = [line[1][0] for line in result[0]] if result[0] else []
600
  combined_text = " ".join(texts).strip()
601
  return {"time": frame_time, "text": combined_text}
602
  except Exception as e:
 
578
  # Fallback to center-bottom strip
579
  fallback_y = height - int(height * 0.2)
580
  return frame[fallback_y:, :], (fallback_y, height)
581
+
582
+ def ocr_frame_worker(args, min_confidence=0.7):
583
  frame_idx, frame_time, frame = args
584
 
585
+ init_ocr_model() # Load model in thread-safe way
 
 
 
586
 
587
+ if frame is None or frame.size == 0 or not isinstance(frame, np.ndarray):
588
  return {"time": frame_time, "text": ""}
589
 
590
  if frame.dtype != np.uint8:
591
  frame = frame.astype(np.uint8)
592
 
593
  try:
594
+ result = ocr_model.ocr(frame, cls=True)
595
+ lines = result[0] if result else []
596
+ texts = [line[1][0] for line in lines if line[1][1] >= min_confidence]
597
  combined_text = " ".join(texts).strip()
598
  return {"time": frame_time, "text": combined_text}
599
  except Exception as e: