Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,25 +6,28 @@ import numpy as np
|
|
| 6 |
from ultralytics import YOLO
|
| 7 |
|
| 8 |
# โหลดโมเดล YOLOv8 ที่ฝึกมาเอง
|
| 9 |
-
model = YOLO('best_V5
|
| 10 |
|
| 11 |
def predict(image):
|
| 12 |
# ทำการทำนาย
|
| 13 |
results = model(image)
|
| 14 |
-
df = results.pandas().xyxy[0] # ผลลัพธ์การทำนายในรูปแบบ pandas DataFrame
|
| 15 |
|
| 16 |
# วาด bounding boxes และ labels บนภาพ
|
| 17 |
-
for
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
|
| 29 |
# แปลงภาพกลับเป็นรูปแบบที่ Gradio สามารถแสดงได้
|
| 30 |
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
|
|
|
| 6 |
from ultralytics import YOLO
|
| 7 |
|
| 8 |
# โหลดโมเดล YOLOv8 ที่ฝึกมาเอง
|
| 9 |
+
model = YOLO('best_V5') # เปลี่ยน 'your_model.pt' เป็นโมเดลของคุณ
|
| 10 |
|
| 11 |
def predict(image):
|
| 12 |
# ทำการทำนาย
|
| 13 |
results = model(image)
|
|
|
|
| 14 |
|
| 15 |
# วาด bounding boxes และ labels บนภาพ
|
| 16 |
+
for result in results:
|
| 17 |
+
boxes = result.boxes.xyxy.cpu().numpy()
|
| 18 |
+
labels = result.names
|
| 19 |
+
confidences = result.boxes.conf.cpu().numpy()
|
| 20 |
+
|
| 21 |
+
for box, confidence in zip(boxes, confidences):
|
| 22 |
+
x1, y1, x2, y2 = map(int, box)
|
| 23 |
+
label = labels[box[5]] # Assuming the label is stored in the last column (index 5)
|
| 24 |
|
| 25 |
+
# วาด bounding box
|
| 26 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 27 |
|
| 28 |
+
# วาด label และ confidence
|
| 29 |
+
label_text = f"{label} {confidence:.2f}"
|
| 30 |
+
cv2.putText(image, label_text, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
| 31 |
|
| 32 |
# แปลงภาพกลับเป็นรูปแบบที่ Gradio สามารถแสดงได้
|
| 33 |
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|