Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,15 +8,16 @@ from ultralytics import YOLO
|
|
8 |
model = YOLO('best_V4.pt')
|
9 |
|
10 |
def predict(image):
|
|
|
11 |
results = model(image, conf=0.8)
|
12 |
|
13 |
detected = False
|
14 |
LABEL_MAP = {
|
15 |
0: "Other",
|
16 |
1: "Pneumonia"
|
17 |
-
}
|
18 |
-
|
19 |
-
|
20 |
for result in results:
|
21 |
boxes = result.boxes.xyxy.cpu().numpy()
|
22 |
confidences = result.boxes.conf.cpu().numpy()
|
@@ -27,17 +28,24 @@ def predict(image):
|
|
27 |
label = LABEL_MAP.get(int(class_id), "Unknown")
|
28 |
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
29 |
label_text = f"{label} {confidence:.2f}"
|
30 |
-
|
31 |
-
|
|
|
|
|
32 |
detected = True
|
33 |
|
34 |
-
if not detected:
|
35 |
-
|
36 |
-
return Image.fromarray(result_rgb), "No detected"
|
37 |
|
38 |
-
# แปลงภาพกลับเป็นรูปแบบที่ Gradio สามารถแสดงได้
|
39 |
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
40 |
-
|
|
|
41 |
|
42 |
-
demo = gr.Interface(fn=predict,
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
demo.launch()
|
|
|
8 |
model = YOLO('best_V4.pt')
|
9 |
|
10 |
def predict(image):
|
11 |
+
|
12 |
results = model(image, conf=0.8)
|
13 |
|
14 |
detected = False
|
15 |
LABEL_MAP = {
|
16 |
0: "Other",
|
17 |
1: "Pneumonia"
|
18 |
+
}
|
19 |
+
|
20 |
+
labels_found = []
|
21 |
for result in results:
|
22 |
boxes = result.boxes.xyxy.cpu().numpy()
|
23 |
confidences = result.boxes.conf.cpu().numpy()
|
|
|
28 |
label = LABEL_MAP.get(int(class_id), "Unknown")
|
29 |
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
30 |
label_text = f"{label} {confidence:.2f}"
|
31 |
+
labels_found.append(label_text)
|
32 |
+
|
33 |
+
cv2.putText(image, label_text, (x1, y1 - 10),
|
34 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
35 |
detected = True
|
36 |
|
37 |
+
if not detected:
|
38 |
+
return None, "No detected"
|
|
|
39 |
|
|
|
40 |
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
41 |
+
message = ", ".join(labels_found)
|
42 |
+
return pil_image, message
|
43 |
|
44 |
+
demo = gr.Interface(fn=predict,
|
45 |
+
inputs=gr.Image(type="numpy"),
|
46 |
+
outputs=[
|
47 |
+
gr.Image(type="pil", label="Detection Result"),
|
48 |
+
gr.Textbox(label="Message")
|
49 |
+
]
|
50 |
+
)
|
51 |
demo.launch()
|