Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,48 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def coordinates(image):
|
| 4 |
return draw(image)
|
|
@@ -7,8 +51,10 @@ demo = gr.Interface(
|
|
| 7 |
fn=coordinates,
|
| 8 |
inputs=[gr.Image(type="numpy")],
|
| 9 |
outputs=[gr.Image(label="Результат детекции"), gr.Textbox(label="Обнаруженные объекты", lines=10)],
|
| 10 |
-
title="Детек
|
| 11 |
)
|
| 12 |
|
| 13 |
if __name__ == "__main__":
|
| 14 |
demo.launch()
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
+
|
| 6 |
+
# Загрузка модели (путь должен быть относительным или абсолютным)
|
| 7 |
+
model = YOLO("models/best.pt")
|
| 8 |
+
|
| 9 |
+
def draw(image_for_drawing):
|
| 10 |
+
#detections = []
|
| 11 |
+
results = model.predict(image_for_drawing, conf=0.5)
|
| 12 |
+
#Перебираем результаты
|
| 13 |
+
for result in results:
|
| 14 |
+
# Получаем bounding boxes в формате [x1, y1, x2, y2, conf, class_id]
|
| 15 |
+
boxes = result.boxes.xyxy.cpu().numpy() # Координаты в пикселях
|
| 16 |
+
confidences = result.boxes.conf.cpu().numpy() # Уверенность
|
| 17 |
+
class_ids = result.boxes.cls.cpu().numpy().astype(int) # ID классов
|
| 18 |
+
|
| 19 |
+
door_boxes = boxes[class_ids == 1] # Массив координат дверей
|
| 20 |
+
windows_boxes = boxes[class_ids == 4]
|
| 21 |
+
door_confs = confidences[class_ids == 1] # Уверенность для дверей
|
| 22 |
+
windows_confs = confidences[class_ids == 4] # Уверенность для окон
|
| 23 |
+
detections = []
|
| 24 |
+
# Выводим координаты и добавляем надписи
|
| 25 |
+
for box, conf in zip(door_boxes, door_confs):
|
| 26 |
+
x1, y1, x2, y2 = box
|
| 27 |
+
detections.append(f"Дверь: x1={x1:.0f}, y1={y1:.0f}, x2={x2:.0f}, y2={y2:.0f}, confidence={conf:.2f}")
|
| 28 |
+
cv2.rectangle(image_for_drawing, (int(x1), int(y1)), (int(x2), int(y2)), (255, 0, 0), 2)
|
| 29 |
+
|
| 30 |
+
# Добавляем текст
|
| 31 |
+
text = f"Door: {conf:.2f}"
|
| 32 |
+
cv2.putText(image_for_drawing, text, (int(x1), int(y1) - 10),
|
| 33 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 0, 0), 1)
|
| 34 |
+
|
| 35 |
+
for box, conf in zip(windows_boxes, windows_confs):
|
| 36 |
+
x1, y1, x2, y2 = box
|
| 37 |
+
detections.append(f"Окно: x1={x1:.0f}, y1={y1:.0f}, x2={x2:.0f}, y2={y2:.0f}, confidence={conf:.2f}")
|
| 38 |
+
cv2.rectangle(image_for_drawing, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
|
| 39 |
+
|
| 40 |
+
# Добавляем текст
|
| 41 |
+
text = f"window: {conf:.2f}"
|
| 42 |
+
cv2.putText(image_for_drawing, text, (int(x1), int(y1) - 10),
|
| 43 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 255, 0), 1)
|
| 44 |
+
#return image_for_drawing
|
| 45 |
+
return image_for_drawing, "\n".join(detections)
|
| 46 |
|
| 47 |
def coordinates(image):
|
| 48 |
return draw(image)
|
|
|
|
| 51 |
fn=coordinates,
|
| 52 |
inputs=[gr.Image(type="numpy")],
|
| 53 |
outputs=[gr.Image(label="Результат детекции"), gr.Textbox(label="Обнаруженные объекты", lines=10)],
|
| 54 |
+
title="Детекция дверей и окон"
|
| 55 |
)
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|
| 58 |
demo.launch()
|
| 59 |
+
|
| 60 |
+
|