fix: pipe file decoding through ffmpeg (AV1/VP9 webm was the silent 0-frame killer)
Browse files- circle_bibi.py +53 -10
circle_bibi.py
CHANGED
|
@@ -62,6 +62,44 @@ def iou(a, b):
|
|
| 62 |
return inter / ua if ua > 0 else 0.0
|
| 63 |
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
def main():
|
| 66 |
ap = argparse.ArgumentParser()
|
| 67 |
ap.add_argument("input", help="video file, or device index for --camera")
|
|
@@ -70,6 +108,7 @@ def main():
|
|
| 70 |
ap.add_argument("--refs", default="refs", help="dir with reference photos")
|
| 71 |
ap.add_argument("--gpu", action="store_true")
|
| 72 |
ap.add_argument("--camera", action="store_true")
|
|
|
|
| 73 |
ap.add_argument("--no-label", action="store_true", help="circle only, no text")
|
| 74 |
args = ap.parse_args()
|
| 75 |
|
|
@@ -77,23 +116,22 @@ def main():
|
|
| 77 |
det = YOLO(args.model)
|
| 78 |
face_app, ref = load_ref_embeddings(args.refs, args.gpu)
|
| 79 |
|
| 80 |
-
cap
|
| 81 |
-
if not cap.isOpened():
|
| 82 |
-
sys.exit(f"cannot open {args.input}")
|
| 83 |
-
fps = cap.get(cv2.CAP_PROP_FPS) or 30
|
| 84 |
-
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)); h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 85 |
writer = None
|
| 86 |
if not args.camera:
|
| 87 |
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 88 |
writer = cv2.VideoWriter(args.output, fourcc, fps, (w, h))
|
| 89 |
|
| 90 |
prev = None # smoothed (cx, cy, r)
|
| 91 |
-
hits = total = 0
|
| 92 |
while True:
|
| 93 |
-
|
| 94 |
-
if
|
| 95 |
break
|
| 96 |
total += 1
|
|
|
|
|
|
|
|
|
|
| 97 |
r = det.predict(frame, verbose=False, imgsz=640, conf=0.25,
|
| 98 |
device=0 if args.gpu else "cpu")[0]
|
| 99 |
target = None
|
|
@@ -123,12 +161,17 @@ def main():
|
|
| 123 |
prev = None # lost him -> stop ghost-drawing
|
| 124 |
if writer:
|
| 125 |
writer.write(frame)
|
|
|
|
| 126 |
elif cv2.waitKey(1) & 0xFF == ord("q"):
|
| 127 |
break
|
| 128 |
-
cap
|
|
|
|
|
|
|
|
|
|
| 129 |
if writer:
|
| 130 |
writer.release()
|
| 131 |
-
print(f"[done] circled him in {hits}/{
|
|
|
|
| 132 |
else:
|
| 133 |
cv2.destroyAllWindows()
|
| 134 |
|
|
|
|
| 62 |
return inter / ua if ua > 0 else 0.0
|
| 63 |
|
| 64 |
|
| 65 |
+
def open_source(args):
|
| 66 |
+
"""OpenCV's backend can't decode AV1/VP9 webm that yt-dlp loves to serve.
|
| 67 |
+
File inputs stream through ffmpeg as raw BGR; --camera keeps cv2.VideoCapture."""
|
| 68 |
+
if args.camera:
|
| 69 |
+
cap = cv2.VideoCapture(0)
|
| 70 |
+
if not cap.isOpened():
|
| 71 |
+
sys.exit("cannot open camera")
|
| 72 |
+
fps = cap.get(cv2.CAP_PROP_FPS) or 30
|
| 73 |
+
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)); h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 74 |
+
return cap, None, fps, w, h
|
| 75 |
+
import subprocess, re
|
| 76 |
+
probe = subprocess.run(["ffprobe", "-v", "error", "-select_streams", "v:0",
|
| 77 |
+
"-show_entries", "stream=width,height,r_frame_rate",
|
| 78 |
+
"-of", "csv=s=,:p=0", args.input],
|
| 79 |
+
capture_output=True, text=True)
|
| 80 |
+
if probe.returncode:
|
| 81 |
+
sys.exit(f"ffprobe failed: {probe.stderr[:200]}")
|
| 82 |
+
w, h, rate = probe.stdout.strip().split(",")[:3]
|
| 83 |
+
w, h = int(w), int(h)
|
| 84 |
+
num, _, den = rate.partition("/")
|
| 85 |
+
fps = float(num) / float(den or 1) or 30
|
| 86 |
+
proc = subprocess.Popen(
|
| 87 |
+
["ffmpeg", "-v", "error", "-i", args.input, "-vf", f"scale={w}:{h}",
|
| 88 |
+
"-pix_fmt", "bgr24", "-f", "rawvideo", "-"],
|
| 89 |
+
stdout=subprocess.PIPE, bufsize=w * h * 3)
|
| 90 |
+
return None, proc, fps, w, h
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def read_frame(cap, proc, w, h):
|
| 94 |
+
if cap is not None:
|
| 95 |
+
ok, frame = cap.read()
|
| 96 |
+
return frame if ok else None
|
| 97 |
+
raw = proc.stdout.read(w * h * 3)
|
| 98 |
+
if len(raw) < w * h * 3:
|
| 99 |
+
return None
|
| 100 |
+
return np.frombuffer(raw, np.uint8).reshape(h, w, 3).copy()
|
| 101 |
+
|
| 102 |
+
|
| 103 |
def main():
|
| 104 |
ap = argparse.ArgumentParser()
|
| 105 |
ap.add_argument("input", help="video file, or device index for --camera")
|
|
|
|
| 108 |
ap.add_argument("--refs", default="refs", help="dir with reference photos")
|
| 109 |
ap.add_argument("--gpu", action="store_true")
|
| 110 |
ap.add_argument("--camera", action="store_true")
|
| 111 |
+
ap.add_argument("--stride", type=int, default=1, help="process every Nth frame")
|
| 112 |
ap.add_argument("--no-label", action="store_true", help="circle only, no text")
|
| 113 |
args = ap.parse_args()
|
| 114 |
|
|
|
|
| 116 |
det = YOLO(args.model)
|
| 117 |
face_app, ref = load_ref_embeddings(args.refs, args.gpu)
|
| 118 |
|
| 119 |
+
cap, proc, fps, w, h = open_source(args)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
writer = None
|
| 121 |
if not args.camera:
|
| 122 |
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 123 |
writer = cv2.VideoWriter(args.output, fourcc, fps, (w, h))
|
| 124 |
|
| 125 |
prev = None # smoothed (cx, cy, r)
|
| 126 |
+
hits = total = kept = 0
|
| 127 |
while True:
|
| 128 |
+
frame = read_frame(cap, proc, w, h)
|
| 129 |
+
if frame is None:
|
| 130 |
break
|
| 131 |
total += 1
|
| 132 |
+
if total % max(1, args.stride) != 0:
|
| 133 |
+
if writer: writer.write(frame)
|
| 134 |
+
continue
|
| 135 |
r = det.predict(frame, verbose=False, imgsz=640, conf=0.25,
|
| 136 |
device=0 if args.gpu else "cpu")[0]
|
| 137 |
target = None
|
|
|
|
| 161 |
prev = None # lost him -> stop ghost-drawing
|
| 162 |
if writer:
|
| 163 |
writer.write(frame)
|
| 164 |
+
kept += 1
|
| 165 |
elif cv2.waitKey(1) & 0xFF == ord("q"):
|
| 166 |
break
|
| 167 |
+
if cap is not None:
|
| 168 |
+
cap.release()
|
| 169 |
+
if proc is not None:
|
| 170 |
+
proc.stdout.close(); proc.kill()
|
| 171 |
if writer:
|
| 172 |
writer.release()
|
| 173 |
+
print(f"[done] circled him in {hits}/{kept} kept frames "
|
| 174 |
+
f"({total} read) -> {args.output}")
|
| 175 |
else:
|
| 176 |
cv2.destroyAllWindows()
|
| 177 |
|