Amina commited on
Commit
2e5eed7
·
1 Parent(s): 87af6f1

404 solution

Browse files
Files changed (1) hide show
  1. app.py +25 -21
app.py CHANGED
@@ -1,41 +1,45 @@
 
1
  from fastapi import FastAPI, File, UploadFile
2
  from PIL import Image
3
- import torch
4
  import io
5
- import os
6
 
 
 
 
 
 
7
  os.environ['TORCH_HOME'] = '/tmp/torch_cache'
8
- app = FastAPI(title="Car Damage Detection API")
9
 
10
- # Load custom YOLOv8 model.
11
- model = torch.hub.load('ultralytics/yolov8', 'custom', path='best.pt')
 
 
 
 
12
 
13
 
14
  @app.get("/")
15
  def read_root():
16
- """ A simple endpoint to check if the server is running. """
17
- return {"status": "ok", "message": "Car Damage Detection API is running!"}
18
 
19
 
20
  @app.post("/detect")
21
  async def detect_damage(file: UploadFile = File(...)):
22
- """
23
- This endpoint receives an image, runs inference, and returns detections.
24
- """
25
- # Read image content from the uploaded file
26
  contents = await file.read()
27
  image = Image.open(io.BytesIO(contents)).convert("RGB")
28
-
29
- # Run the model on the image
30
  results = model(image)
31
 
32
- # Extract results into a clean format
33
- predictions_df = results.pandas().xyxy[0]
34
-
35
- # Filter detections by a confidence threshold (e.g., > 50%)
36
- detections = predictions_df[predictions_df['confidence'] > 0.5]
37
-
38
- # Convert the results to a JSON-friendly list of dictionaries
39
- output = detections[['name', 'confidence']].to_dict(orient='records')
 
 
 
40
 
41
  return {"detections": output}
 
1
+ import os
2
  from fastapi import FastAPI, File, UploadFile
3
  from PIL import Image
 
4
  import io
 
5
 
6
+ # --- NEW CODE: Import YOLO directly ---
7
+ from ultralytics import YOLO
8
+
9
+ # Set the cache directory for PyTorch Hub to a writable location
10
+ # (This line is still good practice, so we'll keep it)
11
  os.environ['TORCH_HOME'] = '/tmp/torch_cache'
 
12
 
13
+ # Initialize the FastAPI app
14
+ app = FastAPI(title="YOLOv8 Car Damage Detection API")
15
+
16
+ # --- MODIFIED LINE: Load the model directly ---
17
+ # This is the new, recommended way to load a local model
18
+ model = YOLO('best.pt')
19
 
20
 
21
  @app.get("/")
22
  def read_root():
23
+ return {"message": "Welcome to the Car Damage Detection API!"}
 
24
 
25
 
26
  @app.post("/detect")
27
  async def detect_damage(file: UploadFile = File(...)):
28
+ # ... (the rest of your code remains the same)
 
 
 
29
  contents = await file.read()
30
  image = Image.open(io.BytesIO(contents)).convert("RGB")
 
 
31
  results = model(image)
32
 
33
+ # --- IMPORTANT CHANGE FOR NEW METHOD ---
34
+ # The output format is slightly different. We need to access the results differently.
35
+ output = []
36
+ for result in results:
37
+ boxes = result.boxes
38
+ for box in boxes:
39
+ class_id = int(box.cls[0])
40
+ class_name = model.names[class_id]
41
+ confidence = float(box.conf[0])
42
+ if confidence > 0.5:
43
+ output.append({'name': class_name, 'confidence': confidence})
44
 
45
  return {"detections": output}