Spaces:
Sleeping
Sleeping
Add new files
Browse files- .env +1 -0
- Dockerfile +16 -0
- llm.py +83 -0
- main.py +388 -0
- models/yolo11n.pt +3 -0
- package-lock.json +6 -0
- requirements.txt +0 -0
.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
GEMINI_API_KEY="AIzaSyCLeD1ROTLbVb8GUYCjEuF-yQe2HegW1mE"
|
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 2 |
+
# you will also find guides on how best to write your Dockerfile
|
| 3 |
+
|
| 4 |
+
FROM python:3.9
|
| 5 |
+
|
| 6 |
+
RUN useradd -m -u 1000 user
|
| 7 |
+
USER user
|
| 8 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 9 |
+
|
| 10 |
+
WORKDIR /app
|
| 11 |
+
|
| 12 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 13 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 14 |
+
|
| 15 |
+
COPY --chown=user . /app
|
| 16 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
llm.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
from typing import Dict, Any
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 10 |
+
if not GEMINI_API_KEY:
|
| 11 |
+
raise RuntimeError("GEMINI_API_KEY environment variable is not set. Please set it in your environment or .env file.")
|
| 12 |
+
|
| 13 |
+
GEMINI_API_KEY = os.getenv('GEMINI_API_KEY', 'AIzaSyCLeD1ROTLbVb8GUYCjEuF-yQe2HegW1mE')
|
| 14 |
+
|
| 15 |
+
# Get API key from environment variable
|
| 16 |
+
# Check GC LOL -molt
|
| 17 |
+
def build_prompt_from_counts(class_counts: Dict[str, int], sample_type: str = "Blood smear", stain: str = "Giemsa", magnification: str = "1000x") -> str:
|
| 18 |
+
"""
|
| 19 |
+
Build a prompt for AI analysis based on detection results.
|
| 20 |
+
|
| 21 |
+
Args:
|
| 22 |
+
class_counts: Dictionary mapping class names to counts
|
| 23 |
+
sample_type: Type of sample (default: "Blood smear")
|
| 24 |
+
stain: Type of stain used (default: "Giemsa")
|
| 25 |
+
magnification: Microscope magnification (default: "1000x")
|
| 26 |
+
|
| 27 |
+
Returns:
|
| 28 |
+
Formatted prompt string for AI analysis
|
| 29 |
+
"""
|
| 30 |
+
summary = "Detected features in sample:\n"
|
| 31 |
+
for cls, count in class_counts.items():
|
| 32 |
+
summary += f"- {count} {cls.replace('_', ' ')}(s)\n"
|
| 33 |
+
|
| 34 |
+
summary += f"Sample type: {sample_type}\nStain: {stain}\nMagnification: {magnification}\n"
|
| 35 |
+
|
| 36 |
+
return f"""
|
| 37 |
+
You are a medical pathologist. Based on the following detection results from a microscopic image, write a concise diagnostic summary and suggest next steps.
|
| 38 |
+
|
| 39 |
+
{summary}
|
| 40 |
+
|
| 41 |
+
Output:
|
| 42 |
+
- Diagnostic Summary:
|
| 43 |
+
- Recommendation:
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
def analyze_detections(class_counts: Dict[str, int], sample_type: str = "Blood smear", stain: str = "Giemsa", magnification: str = "1000x") -> Dict[str, Any]:
|
| 47 |
+
"""
|
| 48 |
+
Analyze detection results using Gemini AI.
|
| 49 |
+
|
| 50 |
+
Args:
|
| 51 |
+
class_counts: Dictionary mapping class names to counts
|
| 52 |
+
sample_type: Type of sample
|
| 53 |
+
stain: Type of stain used
|
| 54 |
+
magnification: Microscope magnification
|
| 55 |
+
|
| 56 |
+
Returns:
|
| 57 |
+
Dictionary containing analysis results
|
| 58 |
+
"""
|
| 59 |
+
try:
|
| 60 |
+
# Configure the API key
|
| 61 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 62 |
+
|
| 63 |
+
# Initialize the Gemini model
|
| 64 |
+
model = genai.GenerativeModel('gemini-2.0-flash')
|
| 65 |
+
|
| 66 |
+
# Build the prompt
|
| 67 |
+
prompt = build_prompt_from_counts(class_counts, sample_type, stain, magnification)
|
| 68 |
+
|
| 69 |
+
# Generate content from the model
|
| 70 |
+
response = model.generate_content(prompt)
|
| 71 |
+
|
| 72 |
+
return {
|
| 73 |
+
"success": True,
|
| 74 |
+
"analysis": response.text,
|
| 75 |
+
"detection_summary": class_counts
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
except Exception as e:
|
| 79 |
+
return {
|
| 80 |
+
"success": False,
|
| 81 |
+
"error": str(e),
|
| 82 |
+
"detection_summary": class_counts
|
| 83 |
+
}
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, Query
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from fastapi.responses import StreamingResponse, JSONResponse
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import cv2
|
| 7 |
+
from ultralytics import YOLO
|
| 8 |
+
from llm import analyze_detections
|
| 9 |
+
from typing import Dict, List, Any
|
| 10 |
+
from collections import Counter
|
| 11 |
+
import base64
|
| 12 |
+
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
|
| 15 |
+
app.add_middleware(
|
| 16 |
+
CORSMiddleware,
|
| 17 |
+
allow_origins=["*"],
|
| 18 |
+
allow_credentials=True,
|
| 19 |
+
allow_methods=["*"],
|
| 20 |
+
allow_headers=["*"],
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
model = None
|
| 25 |
+
model_cache = {}
|
| 26 |
+
|
| 27 |
+
@app.on_event("startup")
|
| 28 |
+
def load_model():
|
| 29 |
+
global model
|
| 30 |
+
try:
|
| 31 |
+
model = YOLO("models/PixCellv1.pt")
|
| 32 |
+
model_cache["anemia_detection_yolov8"] = model
|
| 33 |
+
print("Default model (anemia_detection_yolov8) successfully loaded")
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"Failed to load default model: {e}")
|
| 36 |
+
|
| 37 |
+
# Map model_name to model file path
|
| 38 |
+
MODEL_PATHS = {
|
| 39 |
+
"generic_detection_yolov8": "models/PixCellv1.pt",
|
| 40 |
+
"parasite_detection_yolov8": "models/parasite_detection_yolov8.onnx",
|
| 41 |
+
"anemia_detection_yolov8": "models/anemia_detection_yolov8.onnx",
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
@app.post("/predict")
|
| 45 |
+
async def predict(file: UploadFile = File(...), model_name: str = Query("anemia_detection_yolov8")):
|
| 46 |
+
# full_model_name = model_name + ".onnx"
|
| 47 |
+
|
| 48 |
+
image_bytes = await file.read()
|
| 49 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 50 |
+
|
| 51 |
+
# model = load_model(full_model_name)
|
| 52 |
+
|
| 53 |
+
# Use proper YOLO predict method
|
| 54 |
+
results = model.predict(image, save=False, save_txt=False)
|
| 55 |
+
|
| 56 |
+
# Extract results from the first result object
|
| 57 |
+
result = results[0]
|
| 58 |
+
class_names = []
|
| 59 |
+
|
| 60 |
+
if result.boxes is not None:
|
| 61 |
+
for box in result.boxes:
|
| 62 |
+
cls = int(box.cls[0])
|
| 63 |
+
conf = float(box.conf[0])
|
| 64 |
+
xyxy = box.xyxy[0].tolist() # bounding box [x1, y1, x2, y2]
|
| 65 |
+
class_name = model.names[cls]
|
| 66 |
+
class_names.append(class_name)
|
| 67 |
+
|
| 68 |
+
print(f"Class: {class_name}, Confidence: {conf:.2f}, BBox: {xyxy}")
|
| 69 |
+
|
| 70 |
+
# Use Counter for accurate counting
|
| 71 |
+
class_counts = Counter(class_names)
|
| 72 |
+
|
| 73 |
+
img_with_boxes = result.plot()
|
| 74 |
+
img_with_boxes = cv2.cvtColor(img_with_boxes, cv2.COLOR_BGR2RGB)
|
| 75 |
+
|
| 76 |
+
img_pil = Image.fromarray(img_with_boxes)
|
| 77 |
+
img_bytes = io.BytesIO()
|
| 78 |
+
img_pil.save(img_bytes, format="JPEG")
|
| 79 |
+
img_bytes.seek(0)
|
| 80 |
+
|
| 81 |
+
# Return the processed image as before
|
| 82 |
+
return StreamingResponse(img_bytes, media_type="image/jpeg")
|
| 83 |
+
|
| 84 |
+
@app.post("/predict-with-data")
|
| 85 |
+
async def predict_with_data(file: UploadFile = File(...), model_name: str = Query("anemia_detection_yolov8")):
|
| 86 |
+
"""
|
| 87 |
+
Predict endpoint that returns both the processed image and detection data.
|
| 88 |
+
"""
|
| 89 |
+
image_bytes = await file.read()
|
| 90 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 91 |
+
|
| 92 |
+
# Use proper YOLO predict method
|
| 93 |
+
results = model.predict(image, save=False, save_txt=False)
|
| 94 |
+
|
| 95 |
+
# Extract results from the first result object
|
| 96 |
+
result = results[0]
|
| 97 |
+
class_names = []
|
| 98 |
+
|
| 99 |
+
if result.boxes is not None:
|
| 100 |
+
for box in result.boxes:
|
| 101 |
+
cls = int(box.cls[0])
|
| 102 |
+
conf = float(box.conf[0])
|
| 103 |
+
xyxy = box.xyxy[0].tolist()
|
| 104 |
+
class_name = model.names[cls]
|
| 105 |
+
class_names.append(class_name)
|
| 106 |
+
|
| 107 |
+
print(f"Class: {class_name}, Confidence: {conf:.2f}, BBox: {xyxy}")
|
| 108 |
+
|
| 109 |
+
# Use Counter for accurate counting
|
| 110 |
+
class_counts = Counter(class_names)
|
| 111 |
+
|
| 112 |
+
return JSONResponse({
|
| 113 |
+
"detections": dict(class_counts),
|
| 114 |
+
"total_detections": sum(class_counts.values()),
|
| 115 |
+
"success": True
|
| 116 |
+
})
|
| 117 |
+
|
| 118 |
+
@app.post("/ai-analysis")
|
| 119 |
+
async def ai_analysis(
|
| 120 |
+
file: UploadFile = File(...),
|
| 121 |
+
sample_type: str = Query("Blood smear"),
|
| 122 |
+
stain: str = Query("Giemsa"),
|
| 123 |
+
magnification: str = Query("1000x")
|
| 124 |
+
):
|
| 125 |
+
"""
|
| 126 |
+
Perform AI analysis on an image using the detection model and LLM.
|
| 127 |
+
"""
|
| 128 |
+
try:
|
| 129 |
+
# First, get detections from the image
|
| 130 |
+
image_bytes = await file.read()
|
| 131 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 132 |
+
|
| 133 |
+
# Use proper YOLO predict method
|
| 134 |
+
results = model.predict(image, save=False, save_txt=False)
|
| 135 |
+
|
| 136 |
+
# Extract results from the first result object
|
| 137 |
+
result = results[0]
|
| 138 |
+
class_names = []
|
| 139 |
+
|
| 140 |
+
if result.boxes is not None:
|
| 141 |
+
for box in result.boxes:
|
| 142 |
+
cls = int(box.cls[0])
|
| 143 |
+
class_name = model.names[cls]
|
| 144 |
+
class_names.append(class_name)
|
| 145 |
+
|
| 146 |
+
# Use Counter for accurate counting
|
| 147 |
+
class_counts = Counter(class_names)
|
| 148 |
+
|
| 149 |
+
# If no detections found, return early
|
| 150 |
+
if not class_counts:
|
| 151 |
+
return JSONResponse({
|
| 152 |
+
"success": False,
|
| 153 |
+
"error": "No detections found in the image",
|
| 154 |
+
"detection_summary": {}
|
| 155 |
+
})
|
| 156 |
+
|
| 157 |
+
# Perform AI analysis
|
| 158 |
+
analysis_result = analyze_detections(dict(class_counts), sample_type, stain, magnification)
|
| 159 |
+
|
| 160 |
+
return JSONResponse(analysis_result)
|
| 161 |
+
|
| 162 |
+
except Exception as e:
|
| 163 |
+
return JSONResponse({
|
| 164 |
+
"success": False,
|
| 165 |
+
"error": f"Analysis failed: {str(e)}",
|
| 166 |
+
"detection_summary": {}
|
| 167 |
+
})
|
| 168 |
+
|
| 169 |
+
# New endpoint that combines detection and AI analysis in one call
|
| 170 |
+
@app.post("/detect-and-analyze")
|
| 171 |
+
async def detect_and_analyze(
|
| 172 |
+
file: UploadFile = File(...),
|
| 173 |
+
model_name: str = Query("anemia_detection_yolov8"),
|
| 174 |
+
sample_type: str = Query("Blood smear"),
|
| 175 |
+
stain: str = Query("Giemsa"),
|
| 176 |
+
magnification: str = Query("1000x")
|
| 177 |
+
):
|
| 178 |
+
"""
|
| 179 |
+
Combined endpoint that runs detection once and returns both detection results and AI analysis.
|
| 180 |
+
"""
|
| 181 |
+
try:
|
| 182 |
+
# Model selection logic
|
| 183 |
+
global model_cache
|
| 184 |
+
if model_name not in MODEL_PATHS:
|
| 185 |
+
return JSONResponse({
|
| 186 |
+
"success": False,
|
| 187 |
+
"error": f"Unknown model_name: {model_name}",
|
| 188 |
+
"detections": {},
|
| 189 |
+
"total_detections": 0,
|
| 190 |
+
"detection_details": [],
|
| 191 |
+
"ai_analysis": None
|
| 192 |
+
}, status_code=400)
|
| 193 |
+
if model_name not in model_cache:
|
| 194 |
+
try:
|
| 195 |
+
model_cache[model_name] = YOLO(MODEL_PATHS[model_name])
|
| 196 |
+
print(f"Model {model_name} loaded from {MODEL_PATHS[model_name]}")
|
| 197 |
+
except Exception as e:
|
| 198 |
+
return JSONResponse({
|
| 199 |
+
"success": False,
|
| 200 |
+
"error": f"Failed to load model {model_name}: {e}",
|
| 201 |
+
"detections": {},
|
| 202 |
+
"total_detections": 0,
|
| 203 |
+
"detection_details": [],
|
| 204 |
+
"ai_analysis": None
|
| 205 |
+
}, status_code=500)
|
| 206 |
+
yolo_model = model_cache[model_name]
|
| 207 |
+
|
| 208 |
+
image_bytes = await file.read()
|
| 209 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 210 |
+
|
| 211 |
+
# Run model prediction only once
|
| 212 |
+
results = yolo_model.predict(image, save=False, save_txt=False)
|
| 213 |
+
result = results[0]
|
| 214 |
+
|
| 215 |
+
class_names = []
|
| 216 |
+
detection_details = []
|
| 217 |
+
|
| 218 |
+
if result.boxes is not None:
|
| 219 |
+
for box in result.boxes:
|
| 220 |
+
cls = int(box.cls[0])
|
| 221 |
+
conf = float(box.conf[0])
|
| 222 |
+
xyxy = box.xyxy[0].tolist()
|
| 223 |
+
class_name = yolo_model.names[cls]
|
| 224 |
+
class_names.append(class_name)
|
| 225 |
+
detection_details.append({
|
| 226 |
+
"class": class_name,
|
| 227 |
+
"confidence": conf,
|
| 228 |
+
"bbox": xyxy
|
| 229 |
+
})
|
| 230 |
+
print(f"Class: {class_name}, Confidence: {conf:.2f}, BBox: {xyxy}")
|
| 231 |
+
|
| 232 |
+
# Use Counter for accurate counting
|
| 233 |
+
class_counts = Counter(class_names)
|
| 234 |
+
|
| 235 |
+
# Generate processed image
|
| 236 |
+
img_with_boxes = result.plot()
|
| 237 |
+
img_with_boxes = cv2.cvtColor(img_with_boxes, cv2.COLOR_BGR2RGB)
|
| 238 |
+
img_pil = Image.fromarray(img_with_boxes)
|
| 239 |
+
img_bytes = io.BytesIO()
|
| 240 |
+
img_pil.save(img_bytes, format="JPEG")
|
| 241 |
+
img_bytes.seek(0)
|
| 242 |
+
|
| 243 |
+
# Encode processed image as base64
|
| 244 |
+
processed_image_base64 = base64.b64encode(img_bytes.getvalue()).decode("utf-8")
|
| 245 |
+
|
| 246 |
+
# Perform AI analysis if detections found
|
| 247 |
+
ai_analysis_result = None
|
| 248 |
+
if class_counts:
|
| 249 |
+
ai_analysis_result = analyze_detections(dict(class_counts), sample_type, stain, magnification)
|
| 250 |
+
else:
|
| 251 |
+
ai_analysis_result = {
|
| 252 |
+
"success": False,
|
| 253 |
+
"error": "No detections found in the image",
|
| 254 |
+
"detection_summary": {}
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
return JSONResponse({
|
| 258 |
+
"detections": dict(class_counts),
|
| 259 |
+
"total_detections": sum(class_counts.values()),
|
| 260 |
+
"detection_details": detection_details,
|
| 261 |
+
"ai_analysis": ai_analysis_result,
|
| 262 |
+
"processed_image_base64": processed_image_base64,
|
| 263 |
+
"success": True
|
| 264 |
+
})
|
| 265 |
+
|
| 266 |
+
except Exception as e:
|
| 267 |
+
return JSONResponse({
|
| 268 |
+
"success": False,
|
| 269 |
+
"error": f"Detection and analysis failed: {str(e)}",
|
| 270 |
+
"detections": {},
|
| 271 |
+
"total_detections": 0,
|
| 272 |
+
"detection_details": [],
|
| 273 |
+
"ai_analysis": None,
|
| 274 |
+
"processed_image_base64": None
|
| 275 |
+
})
|
| 276 |
+
|
| 277 |
+
|
| 278 |
+
@app.post("/detect-and-analyze-batch")
|
| 279 |
+
async def detect_and_analyze_batch(
|
| 280 |
+
files: list[UploadFile] = File(...),
|
| 281 |
+
model_name: str = Query("anemia_detection_yolov8"),
|
| 282 |
+
sample_type: str = Query("Blood smear"),
|
| 283 |
+
stain: str = Query("Giemsa"),
|
| 284 |
+
magnification: str = Query("1000x")
|
| 285 |
+
):
|
| 286 |
+
"""
|
| 287 |
+
Batch endpoint that runs detection and analysis on multiple images.
|
| 288 |
+
Returns total counts, per-image results, and batch AI analysis.
|
| 289 |
+
"""
|
| 290 |
+
try:
|
| 291 |
+
global model_cache
|
| 292 |
+
if model_name not in MODEL_PATHS:
|
| 293 |
+
return JSONResponse({
|
| 294 |
+
"success": False,
|
| 295 |
+
"error": f"Unknown model_name: {model_name}",
|
| 296 |
+
"total_counts": {},
|
| 297 |
+
"total_detections": 0,
|
| 298 |
+
"results": [],
|
| 299 |
+
"ai_analysis": None
|
| 300 |
+
}, status_code=400)
|
| 301 |
+
if model_name not in model_cache:
|
| 302 |
+
try:
|
| 303 |
+
model_cache[model_name] = YOLO(MODEL_PATHS[model_name])
|
| 304 |
+
print(f"Model {model_name} loaded from {MODEL_PATHS[model_name]}")
|
| 305 |
+
except Exception as e:
|
| 306 |
+
return JSONResponse({
|
| 307 |
+
"success": False,
|
| 308 |
+
"error": f"Failed to load model {model_name}: {e}",
|
| 309 |
+
"total_counts": {},
|
| 310 |
+
"total_detections": 0,
|
| 311 |
+
"results": [],
|
| 312 |
+
"ai_analysis": None
|
| 313 |
+
}, status_code=500)
|
| 314 |
+
yolo_model = model_cache[model_name]
|
| 315 |
+
|
| 316 |
+
total_class_counts = Counter()
|
| 317 |
+
per_image_results = []
|
| 318 |
+
processed_images = []
|
| 319 |
+
total_detections = 0
|
| 320 |
+
|
| 321 |
+
for file in files:
|
| 322 |
+
image_bytes = await file.read()
|
| 323 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 324 |
+
results = yolo_model.predict(image, save=False, save_txt=False)
|
| 325 |
+
result = results[0]
|
| 326 |
+
class_names = []
|
| 327 |
+
detection_details = []
|
| 328 |
+
if result.boxes is not None:
|
| 329 |
+
for box in result.boxes:
|
| 330 |
+
cls = int(box.cls[0])
|
| 331 |
+
conf = float(box.conf[0])
|
| 332 |
+
xyxy = box.xyxy[0].tolist()
|
| 333 |
+
class_name = yolo_model.names[cls]
|
| 334 |
+
class_names.append(class_name)
|
| 335 |
+
detection_details.append({
|
| 336 |
+
"class": class_name,
|
| 337 |
+
"confidence": conf,
|
| 338 |
+
"bbox": xyxy
|
| 339 |
+
})
|
| 340 |
+
class_counts = Counter(class_names)
|
| 341 |
+
total_class_counts.update(class_counts)
|
| 342 |
+
total_detections += sum(class_counts.values())
|
| 343 |
+
|
| 344 |
+
# Generate processed image with boxes
|
| 345 |
+
img_with_boxes = result.plot()
|
| 346 |
+
img_with_boxes = cv2.cvtColor(img_with_boxes, cv2.COLOR_BGR2RGB)
|
| 347 |
+
img_pil = Image.fromarray(img_with_boxes)
|
| 348 |
+
img_bytes = io.BytesIO()
|
| 349 |
+
img_pil.save(img_bytes, format="JPEG")
|
| 350 |
+
img_bytes.seek(0)
|
| 351 |
+
processed_image_base64 = base64.b64encode(img_bytes.getvalue()).decode("utf-8")
|
| 352 |
+
processed_images.append(processed_image_base64)
|
| 353 |
+
|
| 354 |
+
per_image_results.append({
|
| 355 |
+
"detections": dict(class_counts),
|
| 356 |
+
"total_detections": sum(class_counts.values()),
|
| 357 |
+
"detection_details": detection_details,
|
| 358 |
+
"processed_image_base64": processed_image_base64
|
| 359 |
+
})
|
| 360 |
+
|
| 361 |
+
# Perform batch AI analysis on total counts
|
| 362 |
+
if total_class_counts:
|
| 363 |
+
ai_analysis_result = analyze_detections(dict(total_class_counts), sample_type, stain, magnification)
|
| 364 |
+
else:
|
| 365 |
+
ai_analysis_result = {
|
| 366 |
+
"success": False,
|
| 367 |
+
"error": "No detections found in any image",
|
| 368 |
+
"detection_summary": {}
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
return JSONResponse({
|
| 372 |
+
"success": True,
|
| 373 |
+
"total_counts": dict(total_class_counts),
|
| 374 |
+
"total_detections": total_detections,
|
| 375 |
+
"results": per_image_results,
|
| 376 |
+
"ai_analysis": ai_analysis_result,
|
| 377 |
+
"processed_images": processed_images
|
| 378 |
+
})
|
| 379 |
+
except Exception as e:
|
| 380 |
+
return JSONResponse({
|
| 381 |
+
"success": False,
|
| 382 |
+
"error": f"Batch detection and analysis failed: {str(e)}",
|
| 383 |
+
"total_counts": {},
|
| 384 |
+
"total_detections": 0,
|
| 385 |
+
"results": [],
|
| 386 |
+
"ai_analysis": None
|
| 387 |
+
})
|
| 388 |
+
|
models/yolo11n.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0ebbc80d4a7680d14987a577cd21342b65ecfd94632bd9a8da63ae6417644ee1
|
| 3 |
+
size 5613764
|
package-lock.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "backend",
|
| 3 |
+
"lockfileVersion": 3,
|
| 4 |
+
"requires": true,
|
| 5 |
+
"packages": {}
|
| 6 |
+
}
|
requirements.txt
ADDED
|
Binary file (458 Bytes). View file
|
|
|