Amina commited on
Commit
18611fc
·
1 Parent(s): b27fe2a
Files changed (1) hide show
  1. app.py +31 -7
app.py CHANGED
@@ -30,21 +30,45 @@ def read_root():
30
 
31
  @app.post("/detect")
32
  async def detect_damage(file: UploadFile = File(...)):
 
33
  contents = await file.read()
34
  image = Image.open(io.BytesIO(contents)).convert("RGB")
35
  results = model(image)
36
 
37
- output = []
 
 
 
 
 
 
 
 
 
38
  for result in results:
39
- boxes = result.boxes
40
- for box in boxes:
41
- class_id = int(box.cls[0])
42
- class_name = model.names[class_id]
43
  confidence = float(box.conf[0])
 
 
44
  if confidence > 0.5:
45
- output.append({'name': class_name, 'confidence': confidence})
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
- return {"detections": output}
 
48
 
49
 
50
  # --- DEBUGGING STEP 2 ---
 
30
 
31
  @app.post("/detect")
32
  async def detect_damage(file: UploadFile = File(...)):
33
+ # 1. Get image and run the model (same as before)
34
  contents = await file.read()
35
  image = Image.open(io.BytesIO(contents)).convert("RGB")
36
  results = model(image)
37
 
38
+ # 2. Process the results to determine unique conditions
39
+
40
+ # Use a set to automatically handle duplicates (e.g., "broken" is only added once)
41
+ conditions = set()
42
+
43
+ # Define which detections count as "broken" or "dirty"
44
+ broken_classes = {'scratch', 'rust', 'dent'}
45
+ dirty_classes = {'dirt'}
46
+
47
+ # Loop through all detected objects
48
  for result in results:
49
+ for box in result.boxes:
50
+ # Get the name and confidence of the detected object
51
+ class_name = model.names[int(box.cls[0])]
 
52
  confidence = float(box.conf[0])
53
+
54
+ # Only consider detections with high confidence
55
  if confidence > 0.5:
56
+ if class_name in broken_classes:
57
+ conditions.add("Broken")
58
+ elif class_name in dirty_classes:
59
+ conditions.add("Dirty")
60
+
61
+ # 3. Prepare the final output
62
+
63
+ # If the conditions set is empty after checking all boxes, the car is clean
64
+ if not conditions:
65
+ final_output = ["No damage detected."]
66
+ else:
67
+ # Convert the set to a list for the JSON response
68
+ final_output = list(conditions)
69
 
70
+ # Return the simple list of conditions
71
+ return {"conditions": final_output}
72
 
73
 
74
  # --- DEBUGGING STEP 2 ---