Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from fastapi.responses import FileResponse
|
| 3 |
+
import cv2
|
| 4 |
+
import subprocess
|
| 5 |
+
import uuid
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
def detect_product_in_frame(frame):
|
| 11 |
+
return (100, 50, 400, 400)
|
| 12 |
+
|
| 13 |
+
@app.post("/process_video/")
|
| 14 |
+
async def process_video(file: UploadFile = File(...)):
|
| 15 |
+
input_filename = f"input_{uuid.uuid4()}.mp4"
|
| 16 |
+
with open(input_filename, "wb") as f:
|
| 17 |
+
f.write(await file.read())
|
| 18 |
+
|
| 19 |
+
cap = cv2.VideoCapture(input_filename)
|
| 20 |
+
success, frame = cap.read()
|
| 21 |
+
if not success:
|
| 22 |
+
return {"error": "Não foi possível ler o vídeo."}
|
| 23 |
+
|
| 24 |
+
roi_box = detect_product_in_frame(frame)
|
| 25 |
+
cap.release()
|
| 26 |
+
|
| 27 |
+
output_filename = f"output_{uuid.uuid4()}.mp4"
|
| 28 |
+
|
| 29 |
+
if roi_box:
|
| 30 |
+
x, y, w, h = roi_box
|
| 31 |
+
crop_cmd = [
|
| 32 |
+
'ffmpeg',
|
| 33 |
+
'-i', input_filename,
|
| 34 |
+
'-vf', f"crop={w}:{h}:{x}:{y}",
|
| 35 |
+
'-c:a', 'copy',
|
| 36 |
+
output_filename
|
| 37 |
+
]
|
| 38 |
+
subprocess.run(crop_cmd)
|
| 39 |
+
|
| 40 |
+
os.remove(input_filename)
|
| 41 |
+
return FileResponse(output_filename, media_type="video/mp4", filename="cropped_video.mp4")
|
| 42 |
+
else:
|
| 43 |
+
return {"error": "Não foi possível detectar o produto para realizar o crop."}
|