Bladeren bron

Feature: Add /analyze endpoint for full processing and refactor /detect for simple detection

Dr-Swopt 3 dagen geleden
bovenliggende
commit
5f0bb61282
2 gewijzigde bestanden met toevoegingen van 40 en 6 verwijderingen
  1. 14 2
      README.md
  2. 26 4
      src/api/main.py

+ 14 - 2
README.md

@@ -48,14 +48,26 @@ python train_script.py
 
 ### Running the API Server (DDD Structure)
 
-The new architecture decouples the vision logic from the API entry point.
+The new architecture decouples the vision logic from the API entry point. You can run it either via the root wrapper or directly as a module:
 
 ```powershell
-# Run the FastAPI server from the src directory
+# Option 1: Using the root wrapper
+python main.py
+
+# Option 2: Running as a module
 python -m src.api.main
 ```
 By default, the server runs on `http://localhost:8000`.
 
+### Available Endpoints
+
+| Endpoint | Method | Description |
+| :--- | :--- | :--- |
+| `/detect` | `POST` | Simple YOLO detection (Returns JSON) |
+| `/analyze` | `POST` | Detection + Vertex Vectorization + MongoDB Archival |
+| `/get_confidence` | `GET` | Returns the current model confidence setting |
+| `/set_confidence` | `POST` | Updates the global model confidence setting |
+
 ### Running the Streamlit Dashboard
 
 The Streamlit app still provides the user interface for manual testing.

+ 26 - 4
src/api/main.py

@@ -54,6 +54,30 @@ async def set_confidence(threshold: float = Body(..., embed=True)):
 
 @app.post("/detect")
 async def detect_ripeness(file: UploadFile = File(...)):
+    """Simple YOLO detection only. No archival or vectorization."""
+    image_bytes = await file.read()
+    img = Image.open(io.BytesIO(image_bytes))
+    
+    results = yolo_model(img, conf=current_conf)
+    
+    detections = []
+    for r in results:
+        for box in r.boxes:
+            detections.append({
+                "class": yolo_model.names[int(box.cls)],
+                "confidence": round(float(box.conf), 2),
+                "box": box.xyxy.tolist()[0]
+            })
+            
+    return {
+        "status": "success",
+        "current_threshold": current_conf,
+        "data": detections
+    }
+
+@app.post("/analyze")
+async def analyze_ripeness(file: UploadFile = File(...)):
+    """Full analysis: Detection + Vertex Vectorization + MongoDB Archival."""
     # 1. Save file temporarily for YOLO and Vertex
     temp_path = f"temp_{file.filename}"
     with open(temp_path, "wb") as buffer:
@@ -73,10 +97,8 @@ async def detect_ripeness(file: UploadFile = File(...)):
                     "box": box.xyxy.tolist()[0]
                 })
 
-        # 3. If detections found, analyze the first one (or all) for deeper insights
-        results_summary = []
+        # 3. If detections found, analyze the first one (primary) for deeper insights
         if detections:
-            # For this MVP, we analyze the primary detection
             primary_detection = detections[0]
             record_id = analyze_use_case.execute(temp_path, primary_detection)
             
@@ -84,7 +106,7 @@ async def detect_ripeness(file: UploadFile = File(...)):
                 "status": "success",
                 "record_id": record_id,
                 "detections": detections,
-                "message": "FFB processed and archived successfully"
+                "message": "FFB analyzed, vectorized, and archived successfully"
             }
         
         return {"status": "no_detection", "message": "No palm oil FFB detected"}