Hussein El-Hadidy
commited on
Commit
·
4c67faf
1
Parent(s):
9cd3820
Added support for single image CPR
Browse files
app.py
CHANGED
|
@@ -201,4 +201,36 @@ async def process_video(file: UploadFile = File(...)):
|
|
| 201 |
except Exception as e:
|
| 202 |
raise HTTPException(status_code=500, detail=f"YOLO processing error: {str(e)}")
|
| 203 |
|
| 204 |
-
return JSONResponse(content={"message": "Video processed successfully", "result_dir": "runs/detect/testResult"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
except Exception as e:
|
| 202 |
raise HTTPException(status_code=500, detail=f"YOLO processing error: {str(e)}")
|
| 203 |
|
| 204 |
+
return JSONResponse(content={"message": "Video processed successfully", "result_dir": "runs/detect/testResult"})
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
@app.post("/process_image")
|
| 208 |
+
async def process_image(file: UploadFile = File(...)):
|
| 209 |
+
if not file.content_type.startswith("image/"):
|
| 210 |
+
raise HTTPException(status_code=400, detail="File must be an image.")
|
| 211 |
+
|
| 212 |
+
print("File content type:", file.content_type)
|
| 213 |
+
print("File filename:", file.filename)
|
| 214 |
+
|
| 215 |
+
# Save uploaded image
|
| 216 |
+
image_path = os.path.join(UPLOAD_DIR, file.filename)
|
| 217 |
+
with open(image_path, "wb") as buffer:
|
| 218 |
+
shutil.copyfileobj(file.file, buffer)
|
| 219 |
+
|
| 220 |
+
# Load the YOLO model
|
| 221 |
+
model = YOLO("yolo11n-pose_float16.tflite")
|
| 222 |
+
|
| 223 |
+
print("Model loaded successfully")
|
| 224 |
+
|
| 225 |
+
# Run YOLO detection on the image
|
| 226 |
+
try:
|
| 227 |
+
results = model(
|
| 228 |
+
source=image_path,
|
| 229 |
+
show=False, # Set to False for API use
|
| 230 |
+
save=False
|
| 231 |
+
)
|
| 232 |
+
result_json = results[0].tojson()
|
| 233 |
+
except Exception as e:
|
| 234 |
+
raise HTTPException(status_code=500, detail=f"YOLO processing error: {str(e)}")
|
| 235 |
+
|
| 236 |
+
return JSONResponse(content={"message": "Image processed successfully", "results": result_json})
|
main.py
CHANGED
|
@@ -189,7 +189,7 @@ async def process_video(file: UploadFile = File(...)):
|
|
| 189 |
|
| 190 |
print("Model loaded successfully")
|
| 191 |
|
| 192 |
-
# Run YOLO detection
|
| 193 |
try:
|
| 194 |
results = model(
|
| 195 |
source=video_path,
|
|
@@ -201,4 +201,36 @@ async def process_video(file: UploadFile = File(...)):
|
|
| 201 |
except Exception as e:
|
| 202 |
raise HTTPException(status_code=500, detail=f"YOLO processing error: {str(e)}")
|
| 203 |
|
| 204 |
-
return JSONResponse(content={"message": "Video processed successfully", "result_dir": "runs/detect/testResult"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
print("Model loaded successfully")
|
| 191 |
|
| 192 |
+
# Run YOLO detection
|
| 193 |
try:
|
| 194 |
results = model(
|
| 195 |
source=video_path,
|
|
|
|
| 201 |
except Exception as e:
|
| 202 |
raise HTTPException(status_code=500, detail=f"YOLO processing error: {str(e)}")
|
| 203 |
|
| 204 |
+
return JSONResponse(content={"message": "Video processed successfully", "result_dir": "runs/detect/testResult"})
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
@app.post("/process_image")
|
| 208 |
+
async def process_image(file: UploadFile = File(...)):
|
| 209 |
+
if not file.content_type.startswith("image/"):
|
| 210 |
+
raise HTTPException(status_code=400, detail="File must be an image.")
|
| 211 |
+
|
| 212 |
+
print("File content type:", file.content_type)
|
| 213 |
+
print("File filename:", file.filename)
|
| 214 |
+
|
| 215 |
+
# Save uploaded image
|
| 216 |
+
image_path = os.path.join(UPLOAD_DIR, file.filename)
|
| 217 |
+
with open(image_path, "wb") as buffer:
|
| 218 |
+
shutil.copyfileobj(file.file, buffer)
|
| 219 |
+
|
| 220 |
+
# Load the YOLO model
|
| 221 |
+
model = YOLO("yolo11n-pose_float16.tflite")
|
| 222 |
+
|
| 223 |
+
print("Model loaded successfully")
|
| 224 |
+
|
| 225 |
+
# Run YOLO detection on the image
|
| 226 |
+
try:
|
| 227 |
+
results = model(
|
| 228 |
+
source=image_path,
|
| 229 |
+
show=False, # Set to False for API use
|
| 230 |
+
save=False
|
| 231 |
+
)
|
| 232 |
+
result_json = results[0].tojson()
|
| 233 |
+
except Exception as e:
|
| 234 |
+
raise HTTPException(status_code=500, detail=f"YOLO processing error: {str(e)}")
|
| 235 |
+
|
| 236 |
+
return JSONResponse(content={"message": "Image processed successfully", "results": result_json})
|