Fix indentation in run_batch
Browse files204 unmodified lines20520620720820921018 unmodified lines22923023123223323423563 unmodified lines299300301302303304305306307308309310311312313314315316317318319320321322323324204 unmodified lines@api_app.post("/infer")async def infer(video: UploadFile = File(...),frame_rate: int = 5,max_frames: int = 20,) -> dict:18 unmodified linesraise HTTPException(status_code=400, detail="Could not open video.")total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))idxs = sample_indices(total_frames, max_frames=max_frames)idx_set = set(idxs)all_frames: List[str] = []63 unmodified lineslogits = clf_model(batch)probs = torch.softmax(logits, dim=1).cpu().numpy()for idx, (orig_frame, (crop, has_face)) in enumerate(zip(frames, crops_info)):p_fake = float(probs[idx, 0])p_real = float(probs[idx, 1])label = "fake" if p_fake >= p_real else "real"score = p_fake if label == "fake" else p_realimg_for_display = crop if has_face and crop is not None else orig_frameframe_b64 = b64_encode_image_bgr(img_for_display)out_frames.append(frame_b64)out_preds.append({"frame_index": int(indices[idx]),"label": label,"score": float(score),"prob_real": p_real,"prob_fake": p_fake,"has_face": bool(has_face),})# -------------------- Root app: mount API and frontend ---------------------204 unmodified lines20520620720820921021118 unmodified lines23023123223323423523623723863 unmodified lines302303304305306307308309310311312313314315316317318319320321322323324325326327204 unmodified lines@api_app.post("/infer")async def infer(video: UploadFile = File(...),# Keep params for compatibility but default to 20 frames like preprocessingframe_rate: int = 5,max_frames: int = 20,) -> dict:18 unmodified linesraise HTTPException(status_code=400, detail="Could not open video.")total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))# Mirror preprocessing: uniformly sample exactly `max_frames` indices# across the whole video (equivalent to np.linspace in processing_detect_n_crop).idxs = np.linspace(0, max(0, total_frames - 1), max_frames).astype(int).tolist()idx_set = set(idxs)all_frames: List[str] = []63 unmodified lineslogits = clf_model(batch)probs = torch.softmax(logits, dim=1).cpu().numpy()for idx, (orig_frame, (crop, has_face)) in enumerate(zip(frames, crops_info)):p_fake = float(probs[idx, 0])p_real = float(probs[idx, 1])label = "fake" if p_fake >= p_real else "real"score = p_fake if label == "fake" else p_realimg_for_display = crop if has_face and crop is not None else orig_frameframe_b64 = b64_encode_image_bgr(img_for_display)out_frames.append(frame_b64)out_preds.append({"frame_index": int(indices[idx]),"label": label,"score": float(score),"prob_real": p_real,"prob_fake": p_fake,"has_face": bool(has_face),})# -------------------- Root app: mount API and frontend ---------------------
app.py+22-19