Malina Claude Opus 4.8 commited on
Commit
7342244
·
1 Parent(s): 862aba3

NINA: fix diarization (torchaudio AudioMetaData shim) + OCR score parsing

Browse files

- diarize.py: pyannote 3.3.2 references torchaudio.AudioMetaData, removed in
torchaudio 2.11. We feed pyannote an in-memory tensor (no file I/O), so shim the
symbol before import to let it load. Fixes "module 'torchaudio' has no attribute
'AudioMetaData'".
- ocr.py: RapidOCR 1.2.3 may return the confidence as a string → '>=' str/float
crash. Parse score defensively; also pass BGR (cv2 convention) for the bottom strip.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

Files changed (2) hide show
  1. diarize.py +27 -0
  2. ocr.py +17 -4
diarize.py CHANGED
@@ -26,6 +26,33 @@ def _get_pipeline():
26
  if _pipeline is None:
27
  try:
28
  import torch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  from pyannote.audio import Pipeline
30
  # huggingface-hub renamed use_auth_token → token; support both.
31
  try:
 
26
  if _pipeline is None:
27
  try:
28
  import torch
29
+ import torchaudio
30
+ # pyannote 3.3.2 references torchaudio.AudioMetaData, which newer torchaudio
31
+ # (2.11) removed from the top level. We pass pyannote an in-memory waveform
32
+ # tensor (not a file), so its file-I/O paths aren't exercised — shimming the
33
+ # symbol is enough to let the import succeed.
34
+ if not hasattr(torchaudio, "AudioMetaData"):
35
+ _AMD = None
36
+ for _mp in ("torchaudio.backend.common", "torchaudio._backend.common"):
37
+ try:
38
+ _AMD = getattr(__import__(_mp, fromlist=["AudioMetaData"]),
39
+ "AudioMetaData", None)
40
+ if _AMD:
41
+ break
42
+ except Exception:
43
+ pass
44
+ if _AMD is None:
45
+ from dataclasses import dataclass
46
+
47
+ @dataclass
48
+ class _AMD: # minimal stand-in
49
+ sample_rate: float = 0.0
50
+ num_frames: int = 0
51
+ num_channels: int = 0
52
+ bits_per_sample: int = 0
53
+ encoding: str = "PCM_S"
54
+ torchaudio.AudioMetaData = _AMD
55
+
56
  from pyannote.audio import Pipeline
57
  # huggingface-hub renamed use_auth_token → token; support both.
58
  try:
ocr.py CHANGED
@@ -63,15 +63,28 @@ def _sample_frames(video_path: str, duration: float):
63
  def _read_frame(engine, img: np.ndarray) -> str:
64
  """OCR the bottom strip of one frame → a single cleaned line of caption text."""
65
  h = img.shape[0]
66
- strip = img[int(h * (1.0 - OCR_BOTTOM_FRACTION)):, :, :]
 
67
  try:
68
- result, _ = engine(strip)
 
69
  except Exception:
70
  return ""
71
  if not result:
72
  return ""
73
- # result: list of [box, text, score]; keep confident lines, join in reading order.
74
- parts = [r[1].strip() for r in result if len(r) >= 3 and r[2] and r[2] >= 0.5 and r[1].strip()]
 
 
 
 
 
 
 
 
 
 
 
75
  return " ".join(parts).strip()
76
 
77
 
 
63
  def _read_frame(engine, img: np.ndarray) -> str:
64
  """OCR the bottom strip of one frame → a single cleaned line of caption text."""
65
  h = img.shape[0]
66
+ strip = img[int(h * (1.0 - OCR_BOTTOM_FRACTION)):, :, ::-1] # bottom strip, RGB→BGR for cv2
67
+ strip = np.ascontiguousarray(strip)
68
  try:
69
+ out = engine(strip)
70
+ result = out[0] if isinstance(out, tuple) else out
71
  except Exception:
72
  return ""
73
  if not result:
74
  return ""
75
+ # result: list of [box, text, score]; score format varies by version, so parse it
76
+ # defensively (it may arrive as a string).
77
+ parts = []
78
+ for r in result:
79
+ if not isinstance(r, (list, tuple)) or len(r) < 2:
80
+ continue
81
+ text = str(r[1]).strip()
82
+ try:
83
+ score = float(r[2]) if len(r) >= 3 else 1.0
84
+ except (TypeError, ValueError):
85
+ score = 1.0
86
+ if text and score >= 0.5:
87
+ parts.append(text)
88
  return " ".join(parts).strip()
89
 
90