Commit Β·
4742baa
1
Parent(s): ecd75a8
Add detailed error logging to infer-aw-contour endpoint
Browse files- backend/app.py +12 -0
backend/app.py
CHANGED
|
@@ -9,6 +9,7 @@ import os
|
|
| 9 |
from io import BytesIO
|
| 10 |
from PIL import Image
|
| 11 |
import uvicorn
|
|
|
|
| 12 |
from backend.inference import infer_aw_contour, analyze_frame, analyze_video_frame
|
| 13 |
|
| 14 |
app = FastAPI(title="Pathora Colposcopy API", version="1.0.0")
|
|
@@ -52,11 +53,15 @@ async def infer_aw_contour_endpoint(file: UploadFile = File(...), conf_threshold
|
|
| 52 |
try:
|
| 53 |
# Read image file
|
| 54 |
image_data = await file.read()
|
|
|
|
| 55 |
|
| 56 |
# Try to open image - this will work regardless of content type
|
| 57 |
try:
|
| 58 |
image = Image.open(BytesIO(image_data))
|
|
|
|
| 59 |
except Exception as e:
|
|
|
|
|
|
|
| 60 |
raise HTTPException(status_code=400, detail=f"Invalid image file: {str(e)}")
|
| 61 |
|
| 62 |
# Convert to numpy array and BGR format (OpenCV uses BGR)
|
|
@@ -69,9 +74,12 @@ async def infer_aw_contour_endpoint(file: UploadFile = File(...), conf_threshold
|
|
| 69 |
image = image.convert('RGB')
|
| 70 |
|
| 71 |
frame = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
|
|
|
| 72 |
|
| 73 |
# Run inference - returns dict with 'overlay', 'contours', 'detections', etc.
|
|
|
|
| 74 |
result = infer_aw_contour(frame, conf_threshold=conf_threshold)
|
|
|
|
| 75 |
|
| 76 |
# Convert result overlay back to RGB for JSON serialization
|
| 77 |
if result["overlay"] is not None:
|
|
@@ -84,8 +92,10 @@ async def infer_aw_contour_endpoint(file: UploadFile = File(...), conf_threshold
|
|
| 84 |
buffer.seek(0)
|
| 85 |
import base64
|
| 86 |
image_base64 = base64.b64encode(buffer.getvalue()).decode()
|
|
|
|
| 87 |
else:
|
| 88 |
image_base64 = None
|
|
|
|
| 89 |
|
| 90 |
return JSONResponse({
|
| 91 |
"status": "success",
|
|
@@ -97,6 +107,8 @@ async def infer_aw_contour_endpoint(file: UploadFile = File(...), conf_threshold
|
|
| 97 |
})
|
| 98 |
|
| 99 |
except Exception as e:
|
|
|
|
|
|
|
| 100 |
raise HTTPException(status_code=500, detail=f"Error during inference: {str(e)}")
|
| 101 |
|
| 102 |
|
|
|
|
| 9 |
from io import BytesIO
|
| 10 |
from PIL import Image
|
| 11 |
import uvicorn
|
| 12 |
+
import traceback
|
| 13 |
from backend.inference import infer_aw_contour, analyze_frame, analyze_video_frame
|
| 14 |
|
| 15 |
app = FastAPI(title="Pathora Colposcopy API", version="1.0.0")
|
|
|
|
| 53 |
try:
|
| 54 |
# Read image file
|
| 55 |
image_data = await file.read()
|
| 56 |
+
print(f"β
File received, size: {len(image_data)} bytes")
|
| 57 |
|
| 58 |
# Try to open image - this will work regardless of content type
|
| 59 |
try:
|
| 60 |
image = Image.open(BytesIO(image_data))
|
| 61 |
+
print(f"β
Image opened, mode: {image.mode}, size: {image.size}")
|
| 62 |
except Exception as e:
|
| 63 |
+
print(f"β Image open error: {e}")
|
| 64 |
+
traceback.print_exc()
|
| 65 |
raise HTTPException(status_code=400, detail=f"Invalid image file: {str(e)}")
|
| 66 |
|
| 67 |
# Convert to numpy array and BGR format (OpenCV uses BGR)
|
|
|
|
| 74 |
image = image.convert('RGB')
|
| 75 |
|
| 76 |
frame = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 77 |
+
print(f"β
Frame converted, shape: {frame.shape}")
|
| 78 |
|
| 79 |
# Run inference - returns dict with 'overlay', 'contours', 'detections', etc.
|
| 80 |
+
print(f"π Running infer_aw_contour with conf_threshold={conf_threshold}")
|
| 81 |
result = infer_aw_contour(frame, conf_threshold=conf_threshold)
|
| 82 |
+
print(f"β
Inference complete, detections: {result['detections']}")
|
| 83 |
|
| 84 |
# Convert result overlay back to RGB for JSON serialization
|
| 85 |
if result["overlay"] is not None:
|
|
|
|
| 92 |
buffer.seek(0)
|
| 93 |
import base64
|
| 94 |
image_base64 = base64.b64encode(buffer.getvalue()).decode()
|
| 95 |
+
print(f"β
Image encoded to base64, size: {len(image_base64)} chars")
|
| 96 |
else:
|
| 97 |
image_base64 = None
|
| 98 |
+
print("β οΈ No overlay returned from inference")
|
| 99 |
|
| 100 |
return JSONResponse({
|
| 101 |
"status": "success",
|
|
|
|
| 107 |
})
|
| 108 |
|
| 109 |
except Exception as e:
|
| 110 |
+
print(f"β EXCEPTION in infer_aw_contour:")
|
| 111 |
+
traceback.print_exc()
|
| 112 |
raise HTTPException(status_code=500, detail=f"Error during inference: {str(e)}")
|
| 113 |
|
| 114 |
|