Spaces:
Runtime error
Runtime error
Update core/process.py
Browse files- core/process.py +84 -0
core/process.py
CHANGED
|
@@ -40,7 +40,91 @@ DEPTH_MODEL_MAP = {
|
|
| 40 |
"DPT Beit Large 512": "dpt_beit_large_512"
|
| 41 |
}
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
|
|
|
|
|
|
| 44 |
#@timeout_decorator.timeout(35, use_signals=False) # 35 sec limit per image
|
| 45 |
def process_image(
|
| 46 |
image: Image.Image,
|
|
|
|
| 40 |
"DPT Beit Large 512": "dpt_beit_large_512"
|
| 41 |
}
|
| 42 |
|
| 43 |
+
def process_video(
|
| 44 |
+
video_path: str,
|
| 45 |
+
run_det: bool,
|
| 46 |
+
det_model: str,
|
| 47 |
+
det_confidence: float,
|
| 48 |
+
run_seg: bool,
|
| 49 |
+
seg_model: str,
|
| 50 |
+
run_depth: bool,
|
| 51 |
+
depth_model: str,
|
| 52 |
+
blend: float
|
| 53 |
+
):
|
| 54 |
+
"""
|
| 55 |
+
Reads each frame from `video_path`, runs your existing `process_image()` on it,
|
| 56 |
+
and writes out a new MP4 in outputs/processed_<name>.mp4.
|
| 57 |
+
|
| 58 |
+
Returns:
|
| 59 |
+
(None, scene_json: dict, output_video_path: str)
|
| 60 |
+
"""
|
| 61 |
+
logger.info(f"Starting video processing for {video_path}")
|
| 62 |
+
cap = cv2.VideoCapture(video_path)
|
| 63 |
+
if not cap.isOpened():
|
| 64 |
+
raise RuntimeError(f"Cannot open video: {video_path}")
|
| 65 |
+
|
| 66 |
+
fps = cap.get(cv2.CAP_PROP_FPS) or 25.0
|
| 67 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 68 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 69 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 70 |
+
|
| 71 |
+
os.makedirs("outputs", exist_ok=True)
|
| 72 |
+
base = os.path.basename(video_path)
|
| 73 |
+
out_path = os.path.join("outputs", f"processed_{base}")
|
| 74 |
+
writer = cv2.VideoWriter(out_path, fourcc, fps, (width, height))
|
| 75 |
+
if not writer.isOpened():
|
| 76 |
+
cap.release()
|
| 77 |
+
raise RuntimeError(f"Cannot write to: {out_path}")
|
| 78 |
+
|
| 79 |
+
frame_idx = 0
|
| 80 |
+
scene_info = {"video": base, "frames_processed": 0}
|
| 81 |
+
|
| 82 |
+
while True:
|
| 83 |
+
ret, frame_bgr = cap.read()
|
| 84 |
+
if not ret:
|
| 85 |
+
break
|
| 86 |
+
|
| 87 |
+
# BGR→RGB→PIL
|
| 88 |
+
frame_rgb = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
|
| 89 |
+
pil_frame = Image.fromarray(frame_rgb)
|
| 90 |
+
|
| 91 |
+
# Run your existing image pipeline
|
| 92 |
+
try:
|
| 93 |
+
processed_img, _, _ = process_image(
|
| 94 |
+
image=pil_frame,
|
| 95 |
+
run_det=run_det,
|
| 96 |
+
det_model=det_model,
|
| 97 |
+
det_confidence=det_confidence,
|
| 98 |
+
run_seg=run_seg,
|
| 99 |
+
seg_model=seg_model,
|
| 100 |
+
run_depth=run_depth,
|
| 101 |
+
depth_model=depth_model,
|
| 102 |
+
blend=blend
|
| 103 |
+
)
|
| 104 |
+
except Exception as e:
|
| 105 |
+
cap.release()
|
| 106 |
+
writer.release()
|
| 107 |
+
raise RuntimeError(f"Error on frame {frame_idx}: {e}")
|
| 108 |
+
|
| 109 |
+
# PIL→BGR numpy
|
| 110 |
+
out_bgr = cv2.cvtColor(np.array(processed_img), cv2.COLOR_RGB2BGR)
|
| 111 |
+
writer.write(out_bgr)
|
| 112 |
+
|
| 113 |
+
frame_idx += 1
|
| 114 |
+
scene_info["frames_processed"] = frame_idx
|
| 115 |
+
|
| 116 |
+
cap.release()
|
| 117 |
+
writer.release()
|
| 118 |
+
logger.info(f"Finished video. Wrote {frame_idx} frames to {out_path}")
|
| 119 |
+
|
| 120 |
+
# Minimal JSON summary
|
| 121 |
+
scene_json = {
|
| 122 |
+
"video": scene_info["video"],
|
| 123 |
+
"frames_processed": scene_info["frames_processed"]
|
| 124 |
+
}
|
| 125 |
|
| 126 |
+
return None, scene_json, out_path
|
| 127 |
+
|
| 128 |
#@timeout_decorator.timeout(35, use_signals=False) # 35 sec limit per image
|
| 129 |
def process_image(
|
| 130 |
image: Image.Image,
|