Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,6 +21,7 @@ def detect_objects(image, score_threshold):
|
|
| 21 |
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes)
|
| 22 |
|
| 23 |
labels_output = []
|
|
|
|
| 24 |
|
| 25 |
# Draw bounding boxes around detected objects
|
| 26 |
draw = ImageDraw.Draw(image)
|
|
@@ -31,11 +32,26 @@ def detect_objects(image, score_threshold):
|
|
| 31 |
|
| 32 |
for score, label, box in zip(scores, labels, boxes):
|
| 33 |
if score >= score_threshold: # Only draw if score is above threshold
|
|
|
|
| 34 |
box = [round(i, 2) for i in box.tolist()]
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
draw.rectangle(box, outline="red", width=3)
|
| 37 |
draw.text((box[0], box[1]), f"{label_name}: {round(score.item(), 3)}", fill="red")
|
| 38 |
labels_output.append(f"{label_name}: {round(score.item(), 3)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
return image, "\n".join(labels_output)
|
| 41 |
|
|
|
|
| 21 |
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes)
|
| 22 |
|
| 23 |
labels_output = []
|
| 24 |
+
no_detection = True
|
| 25 |
|
| 26 |
# Draw bounding boxes around detected objects
|
| 27 |
draw = ImageDraw.Draw(image)
|
|
|
|
| 32 |
|
| 33 |
for score, label, box in zip(scores, labels, boxes):
|
| 34 |
if score >= score_threshold: # Only draw if score is above threshold
|
| 35 |
+
no_detection = False
|
| 36 |
box = [round(i, 2) for i in box.tolist()]
|
| 37 |
+
|
| 38 |
+
#label_name = "Pneumonia" if label.item() == 0 else "Other"
|
| 39 |
+
if label.item() == 0:
|
| 40 |
+
label_name = "Pneumonia"
|
| 41 |
+
elif label.item() == 1:
|
| 42 |
+
label_name = "Normal"
|
| 43 |
+
elif label.item() == 2:
|
| 44 |
+
label_name = "Pneumonia_bacteria"
|
| 45 |
+
else:
|
| 46 |
+
label_name = "Pneumonia_virus"
|
| 47 |
+
|
| 48 |
draw.rectangle(box, outline="red", width=3)
|
| 49 |
draw.text((box[0], box[1]), f"{label_name}: {round(score.item(), 3)}", fill="red")
|
| 50 |
labels_output.append(f"{label_name}: {round(score.item(), 3)}")
|
| 51 |
+
|
| 52 |
+
# If no detections, set label as 'Other'
|
| 53 |
+
if no_detection:
|
| 54 |
+
labels_output.append("Other")
|
| 55 |
|
| 56 |
return image, "\n".join(labels_output)
|
| 57 |
|