KRISH09bha commited on
Commit
3bc40d7
·
verified ·
1 Parent(s): a67480e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -12
app.py CHANGED
@@ -75,7 +75,7 @@ async def upload_image(file: UploadFile = File(...)):
75
 
76
  @app.post("/upload-video/")
77
  async def upload_video(file: UploadFile = File(...)):
78
- """Upload a video, process it frame by frame, and return detection results."""
79
  if model is None:
80
  return {"error": "Model not loaded. Please upload '12x.pt' to run detection."}
81
 
@@ -88,30 +88,42 @@ async def upload_video(file: UploadFile = File(...)):
88
  os.remove(temp_video_path)
89
  return {"error": "Could not open video file"}
90
 
91
- frame_results = []
92
- frame_interval = 5 # Process every 5th frame for efficiency
 
 
 
 
 
 
 
93
 
 
94
  frame_index = 0
 
95
  while True:
96
  ret, frame = cap.read()
97
  if not ret:
98
  break
99
 
100
  if frame_index % frame_interval == 0:
101
- predictions, object_count = process_frame(frame)
102
- frame_results.append({
103
- "frame_index": frame_index,
104
- "object_count": object_count,
105
- "detections": predictions
106
- })
 
 
107
 
 
108
  frame_index += 1
109
 
110
  cap.release()
111
- os.remove(temp_video_path) # Clean up temporary file
112
-
113
- return {"video_results": frame_results}
114
 
 
115
 
116
  @app.get("/")
117
  def home():
 
75
 
76
  @app.post("/upload-video/")
77
  async def upload_video(file: UploadFile = File(...)):
78
+ """Upload a video, process it frame by frame, and return the processed video."""
79
  if model is None:
80
  return {"error": "Model not loaded. Please upload '12x.pt' to run detection."}
81
 
 
88
  os.remove(temp_video_path)
89
  return {"error": "Could not open video file"}
90
 
91
+ # Get video properties
92
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
93
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
94
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
95
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
96
+
97
+ # Output video
98
+ output_video_path = temp_video_path.replace(".mp4", "_processed.mp4")
99
+ out = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))
100
 
101
+ frame_interval = 5 # Process every 5th frame for efficiency
102
  frame_index = 0
103
+
104
  while True:
105
  ret, frame = cap.read()
106
  if not ret:
107
  break
108
 
109
  if frame_index % frame_interval == 0:
110
+ predictions, _ = process_frame(frame)
111
+
112
+ # Draw bounding boxes on the frame
113
+ for pred in predictions:
114
+ x1, y1, x2, y2 = map(int, pred["bbox"])
115
+ label = f"{pred['class']} ({pred['confidence']:.2f})"
116
+ cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
117
+ cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
118
 
119
+ out.write(frame)
120
  frame_index += 1
121
 
122
  cap.release()
123
+ out.release()
124
+ os.remove(temp_video_path) # Clean up temp file
 
125
 
126
+ return FileResponse(output_video_path, media_type="video/mp4", filename="processed_video.mp4")
127
 
128
  @app.get("/")
129
  def home():