Spaces:
Runtime error
Runtime error
Commit
·
88bb3ba
1
Parent(s):
6bbd8e9
fixing issues related to this
Browse files- Dockerfile +22 -0
- __pycache__/app.cpython-312.pyc +0 -0
- __pycache__/app.cpython-39.pyc +0 -0
- app.py +86 -0
- car_part_detector_model.pt +3 -0
- damage_general_model.pt +3 -0
- requirements.txt +6 -0
Dockerfile
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9-slim
|
2 |
+
|
3 |
+
WORKDIR /app
|
4 |
+
|
5 |
+
# Update package lists and install system dependencies for OpenCV
|
6 |
+
RUN apt-get install -y \
|
7 |
+
# libgl1-mesa-glx \
|
8 |
+
# libglib2.0-0 \
|
9 |
+
&& rm -rf /var/lib/apt/lists/*
|
10 |
+
|
11 |
+
# Install Python dependencies
|
12 |
+
COPY requirements.txt .
|
13 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
14 |
+
|
15 |
+
# Copy application and model files
|
16 |
+
COPY app.py .
|
17 |
+
COPY car_part_detector_model.pt .
|
18 |
+
COPY damage_general_model.pt .
|
19 |
+
|
20 |
+
EXPOSE 8000
|
21 |
+
|
22 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
|
__pycache__/app.cpython-312.pyc
ADDED
Binary file (5.21 kB). View file
|
|
__pycache__/app.cpython-39.pyc
ADDED
Binary file (3.07 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
from ultralytics import YOLO
|
4 |
+
import numpy as np
|
5 |
+
import cv2
|
6 |
+
from io import BytesIO
|
7 |
+
from PIL import Image
|
8 |
+
import base64
|
9 |
+
|
10 |
+
app = FastAPI(title="Car Parts & Damage Detection API")
|
11 |
+
|
12 |
+
# Load YOLO models
|
13 |
+
try:
|
14 |
+
car_part_model = YOLO("car_part_detector_model.pt")
|
15 |
+
damage_model = YOLO("damage_general_model.pt")
|
16 |
+
except Exception as e:
|
17 |
+
raise RuntimeError(f"Failed to load models: {str(e)}")
|
18 |
+
|
19 |
+
def image_to_base64(img: np.ndarray) -> str:
|
20 |
+
"""Convert numpy image to base64 string for JSON response."""
|
21 |
+
_, buffer = cv2.imencode(".png", img)
|
22 |
+
return base64.b64encode(buffer).decode("utf-8")
|
23 |
+
|
24 |
+
@app.post("/predict", summary="Run inference on an image for car parts and damage")
|
25 |
+
async def predict(file: UploadFile = File(...)):
|
26 |
+
"""
|
27 |
+
Upload an image and get car parts and damage detection results.
|
28 |
+
Returns annotated images as base64 strings and text descriptions.
|
29 |
+
"""
|
30 |
+
try:
|
31 |
+
# Read and process image
|
32 |
+
contents = await file.read()
|
33 |
+
image = Image.open(BytesIO(contents)).convert("RGB")
|
34 |
+
img = np.array(image)
|
35 |
+
|
36 |
+
# Initialize default blank images (gray placeholder)
|
37 |
+
blank_img = np.full((img.shape[0], img.shape[1], 3), 128, dtype=np.uint8)
|
38 |
+
car_part_img = blank_img.copy()
|
39 |
+
damage_img = blank_img.copy()
|
40 |
+
|
41 |
+
# Initialize text results
|
42 |
+
car_part_text = "Car Parts: No detections"
|
43 |
+
damage_text = "Damage: No detections"
|
44 |
+
|
45 |
+
# Process car parts detection
|
46 |
+
try:
|
47 |
+
car_part_results = car_part_model(img)[0]
|
48 |
+
if car_part_results.boxes:
|
49 |
+
car_part_img = car_part_results.plot()[..., ::-1] # BGR to RGB
|
50 |
+
car_part_text = "Car Parts:\n" + "\n".join(
|
51 |
+
f"- {car_part_results.names[int(cls)]} ({conf:.2f})"
|
52 |
+
for conf, cls in zip(car_part_results.boxes.conf, car_part_results.boxes.cls)
|
53 |
+
)
|
54 |
+
except Exception as e:
|
55 |
+
car_part_text = f"Car Parts: Error: {str(e)}"
|
56 |
+
|
57 |
+
# Process damage detection
|
58 |
+
try:
|
59 |
+
damage_results = damage_model(img)[0]
|
60 |
+
if damage_results.boxes:
|
61 |
+
damage_img = damage_results.plot()[..., ::-1] # BGR to RGB
|
62 |
+
damage_text = "Damage:\n" + "\n".join(
|
63 |
+
f"- {damage_results.names[int(cls)]} ({conf:.2f})"
|
64 |
+
for conf, cls in zip(damage_results.boxes.conf, damage_results.boxes.cls)
|
65 |
+
)
|
66 |
+
except Exception as e:
|
67 |
+
damage_text = f"Damage: Error: {str(e)}"
|
68 |
+
|
69 |
+
# Convert output images to base64
|
70 |
+
car_part_img_base64 = image_to_base64(car_part_img)
|
71 |
+
damage_img_base64 = image_to_base64(damage_img)
|
72 |
+
|
73 |
+
return JSONResponse({
|
74 |
+
"car_part_image": car_part_img_base64,
|
75 |
+
"car_part_text": car_part_text,
|
76 |
+
"damage_image": damage_img_base64,
|
77 |
+
"damage_text": damage_text
|
78 |
+
})
|
79 |
+
|
80 |
+
except Exception as e:
|
81 |
+
raise HTTPException(status_code=500, detail=f"Inference error: {str(e)}")
|
82 |
+
|
83 |
+
@app.get("/", summary="Health check")
|
84 |
+
async def root():
|
85 |
+
"""Check if the API is running."""
|
86 |
+
return {"message": "Car Parts & Damage Detection API is running"}
|
car_part_detector_model.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:919ed7512ffbee9013a1384c467c605e1b5beaa21319db6a306b5d0aa4180e9e
|
3 |
+
size 6010902
|
damage_general_model.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4e1ffaafa64edbf5a062384b6e9060bda9087e29ef8005699659c2a21fb56397
|
3 |
+
size 45177135
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn[standard]
|
3 |
+
ultralytics
|
4 |
+
numpy
|
5 |
+
opencv-python
|
6 |
+
pillow
|