Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, UploadFile, File | |
| from fastapi.responses import FileResponse | |
| import cv2 | |
| import subprocess | |
| import uuid | |
| import os | |
| app = FastAPI() | |
| os.umask(0o077) | |
| def detect_product_in_frame(frame): | |
| return (100, 50, 400, 400) | |
| async def process_video(file: UploadFile = File(...)): | |
| input_filename = f"/tmp/input_{uuid.uuid4()}.mp4" | |
| output_filename = f"/tmp/output_{uuid.uuid4()}.mp4" | |
| with open(input_filename, "wb") as f: | |
| f.write(await file.read()) | |
| cap = cv2.VideoCapture(input_filename) | |
| success, frame = cap.read() | |
| if not success: | |
| return {"error": "Não foi possível ler o vídeo."} | |
| roi_box = detect_product_in_frame(frame) | |
| cap.release() | |
| if roi_box: | |
| x, y, w, h = roi_box | |
| crop_cmd = [ | |
| 'ffmpeg', | |
| '-i', input_filename, | |
| '-vf', f"crop={w}:{h}:{x}:{y}", | |
| '-c:a', 'copy', | |
| output_filename | |
| ] | |
| subprocess.run(crop_cmd) | |
| os.remove(input_filename) | |
| return FileResponse(output_filename, media_type="video/mp4", filename="cropped_video.mp4") | |
| else: | |
| return {"error": "Não foi possível detectar o produto para realizar o crop."} | |