Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -77,64 +77,6 @@ async def upload_image(file: UploadFile = File(...)):
|
|
77 |
except Exception as e:
|
78 |
raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
|
79 |
|
80 |
-
|
81 |
-
@app.post("/upload-video/")
|
82 |
-
async def upload_video(file: UploadFile = File(...)):
|
83 |
-
"""Upload a video, process it frame by frame, and return the processed video."""
|
84 |
-
if model is None:
|
85 |
-
raise HTTPException(status_code=500, detail="Model not loaded. Please upload '12x.pt' to run detection.")
|
86 |
-
|
87 |
-
try:
|
88 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video:
|
89 |
-
shutil.copyfileobj(file.file, temp_video)
|
90 |
-
temp_video_path = temp_video.name
|
91 |
-
|
92 |
-
cap = cv2.VideoCapture(temp_video_path)
|
93 |
-
if not cap.isOpened():
|
94 |
-
os.remove(temp_video_path)
|
95 |
-
raise HTTPException(status_code=400, detail="Could not open video file.")
|
96 |
-
|
97 |
-
# Get video properties
|
98 |
-
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
99 |
-
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
100 |
-
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
101 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
102 |
-
|
103 |
-
# Output video
|
104 |
-
output_video_path = temp_video_path.replace(".mp4", "_processed.mp4")
|
105 |
-
out = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))
|
106 |
-
|
107 |
-
frame_interval = 5 # Process every 5th frame for efficiency
|
108 |
-
frame_index = 0
|
109 |
-
|
110 |
-
while True:
|
111 |
-
ret, frame = cap.read()
|
112 |
-
if not ret:
|
113 |
-
break
|
114 |
-
|
115 |
-
if frame_index % frame_interval == 0:
|
116 |
-
predictions, _ = process_frame(frame)
|
117 |
-
|
118 |
-
# Draw bounding boxes on the frame
|
119 |
-
for pred in predictions:
|
120 |
-
x1, y1, x2, y2 = map(int, pred["bbox"])
|
121 |
-
label = f"{pred['class']} ({pred['confidence']:.2f})"
|
122 |
-
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
123 |
-
cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
124 |
-
|
125 |
-
out.write(frame)
|
126 |
-
frame_index += 1
|
127 |
-
|
128 |
-
cap.release()
|
129 |
-
out.release()
|
130 |
-
os.remove(temp_video_path) # Clean up temp file
|
131 |
-
|
132 |
-
return FileResponse(output_video_path, media_type="video/mp4", filename="processed_video.mp4")
|
133 |
-
|
134 |
-
except Exception as e:
|
135 |
-
raise HTTPException(status_code=500, detail=f"Error processing video: {str(e)}")
|
136 |
-
|
137 |
-
|
138 |
@app.get("/")
|
139 |
def home():
|
140 |
return {"message": "🎯 Object Detection API for Images and Videos using 12x.pt"}
|
|
|
77 |
except Exception as e:
|
78 |
raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
|
79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
@app.get("/")
|
81 |
def home():
|
82 |
return {"message": "🎯 Object Detection API for Images and Videos using 12x.pt"}
|