Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,12 @@ import gradio as gr
|
|
| 2 |
from ultralytics import YOLO
|
| 3 |
from collections import Counter
|
| 4 |
import os
|
| 5 |
-
from glob import glob
|
| 6 |
-
from PIL import Image
|
| 7 |
import uuid
|
| 8 |
|
| 9 |
-
#
|
| 10 |
model = YOLO("best.pt")
|
| 11 |
|
| 12 |
-
# Sınıf
|
| 13 |
id2name = {
|
| 14 |
0: "Silah", 1: "Tank", 2: "Helikopter", 3: "Savaş Uçağı", 4: "Asker",
|
| 15 |
5: "Top", 6: "Füze", 7: "Savaş Gemisi", 8: "Askeri Araç", 9: "Bayrak",
|
|
@@ -18,65 +16,96 @@ id2name = {
|
|
| 18 |
|
| 19 |
PRED_DIR = "yolo_outputs"
|
| 20 |
|
| 21 |
-
def
|
| 22 |
os.makedirs("inputs", exist_ok=True)
|
| 23 |
image_id = str(uuid.uuid4())[:8]
|
| 24 |
image_path = f"inputs/{image_id}.jpg"
|
| 25 |
image.save(image_path)
|
| 26 |
|
| 27 |
-
# YOLO tahmini + save_txt
|
| 28 |
model.predict(source=image_path, save_txt=True, conf=0.25, project=PRED_DIR, name=image_id, exist_ok=True)
|
| 29 |
|
| 30 |
-
# TXT yolunu tanımla
|
| 31 |
txt_path = f"{PRED_DIR}/{image_id}/labels/{image_id}.txt"
|
| 32 |
|
| 33 |
if not os.path.exists(txt_path):
|
| 34 |
-
return "Hiçbir nesne tespit
|
| 35 |
|
| 36 |
-
# TXT dosyasını oku ve class_id çıkar
|
| 37 |
-
class_ids = []
|
| 38 |
with open(txt_path, "r") as f:
|
| 39 |
-
|
| 40 |
-
debug_txt = "".join(lines)
|
| 41 |
-
for line in lines:
|
| 42 |
-
class_id = int(line.strip().split()[0])
|
| 43 |
-
class_ids.append(class_id)
|
| 44 |
|
| 45 |
counts = Counter(class_ids)
|
| 46 |
detected_names = {id2name[i] for i in counts if i in id2name}
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
cevap2 = f"Bulunan savunma sistemleri: {', '.join(savunma & detected_names)}" if savunma & detected_names else "Savunma sistemi tespit edilmedi."
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
yüksek_tehdit = {"Tank", "Füze", "Savaş Uçağı", "Top", "Savaş Gemisi"}
|
| 61 |
-
if yüksek_tehdit & detected_names:
|
| 62 |
-
cevap5 = "Yüksek tehdit içeriyor."
|
| 63 |
-
elif "Asker" in detected_names:
|
| 64 |
-
cevap5 = "Orta seviye tehdit."
|
| 65 |
else:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
return
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
gr.
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from ultralytics import YOLO
|
| 3 |
from collections import Counter
|
| 4 |
import os
|
|
|
|
|
|
|
| 5 |
import uuid
|
| 6 |
|
| 7 |
+
# Model yükle
|
| 8 |
model = YOLO("best.pt")
|
| 9 |
|
| 10 |
+
# Sınıf eşlemesi
|
| 11 |
id2name = {
|
| 12 |
0: "Silah", 1: "Tank", 2: "Helikopter", 3: "Savaş Uçağı", 4: "Asker",
|
| 13 |
5: "Top", 6: "Füze", 7: "Savaş Gemisi", 8: "Askeri Araç", 9: "Bayrak",
|
|
|
|
| 16 |
|
| 17 |
PRED_DIR = "yolo_outputs"
|
| 18 |
|
| 19 |
+
def save_and_predict(image):
|
| 20 |
os.makedirs("inputs", exist_ok=True)
|
| 21 |
image_id = str(uuid.uuid4())[:8]
|
| 22 |
image_path = f"inputs/{image_id}.jpg"
|
| 23 |
image.save(image_path)
|
| 24 |
|
|
|
|
| 25 |
model.predict(source=image_path, save_txt=True, conf=0.25, project=PRED_DIR, name=image_id, exist_ok=True)
|
| 26 |
|
|
|
|
| 27 |
txt_path = f"{PRED_DIR}/{image_id}/labels/{image_id}.txt"
|
| 28 |
|
| 29 |
if not os.path.exists(txt_path):
|
| 30 |
+
return {}, "Hiçbir nesne tespit edilemedi."
|
| 31 |
|
|
|
|
|
|
|
| 32 |
with open(txt_path, "r") as f:
|
| 33 |
+
class_ids = [int(line.strip().split()[0]) for line in f]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
counts = Counter(class_ids)
|
| 36 |
detected_names = {id2name[i] for i in counts if i in id2name}
|
| 37 |
|
| 38 |
+
data = {
|
| 39 |
+
"counts": counts,
|
| 40 |
+
"names": detected_names
|
| 41 |
+
}
|
| 42 |
|
| 43 |
+
return data, "Tahmin başarıyla yüklendi. Sağdaki kutulardan bir soruya tıklayın."
|
|
|
|
| 44 |
|
| 45 |
+
def cevapla(soru_id, data, chat_history):
|
| 46 |
+
if not data:
|
| 47 |
+
return chat_history + [("❓ " + soru_id, "Önce bir görsel yükleyin ve tahmin yapın.")]
|
| 48 |
|
| 49 |
+
counts = data["counts"]
|
| 50 |
+
names = data["names"]
|
| 51 |
+
|
| 52 |
+
if soru_id == "1️⃣ Kaç nesne ve kaçar adet?":
|
| 53 |
+
detaylar = [f"{counts[i]} adet {id2name[i]}" for i in counts]
|
| 54 |
+
cevap = f"{len(counts)} farklı sınıf tespit edildi:\n" + "\n".join(detaylar)
|
| 55 |
+
|
| 56 |
+
elif soru_id == "2️⃣ Savunma sistemleri var mı?":
|
| 57 |
+
savunma = {"Tank", "Füze", "Top", "Helikopter"}
|
| 58 |
+
bulunan = savunma & names
|
| 59 |
+
cevap = f"Bulunan savunma sistemleri: {', '.join(bulunan)}" if bulunan else "Savunma sistemi tespit edilmedi."
|
| 60 |
+
|
| 61 |
+
elif soru_id == "3️⃣ Askeri birlik tespiti?":
|
| 62 |
+
cevap = "Askeri birlik olabilir." if {"Asker", "Askeri Araç"} & names else "Askeri birlik tespit edilmedi."
|
| 63 |
+
|
| 64 |
+
elif soru_id == "4️⃣ Hava unsurları var mı?":
|
| 65 |
+
hava = {"Helikopter", "Savaş Uçağı"}
|
| 66 |
+
bulunan = hava & names
|
| 67 |
+
cevap = f"Hava unsurları tespit edildi: {', '.join(bulunan)}" if bulunan else "Hava unsuru tespit edilmedi."
|
| 68 |
+
|
| 69 |
+
elif soru_id == "5️⃣ Tehdit seviyesi nedir?":
|
| 70 |
+
yüksek = {"Tank", "Füze", "Savaş Uçağı", "Top", "Savaş Gemisi"}
|
| 71 |
+
if yüksek & names:
|
| 72 |
+
cevap = "Yüksek tehdit içeriyor."
|
| 73 |
+
elif "Asker" in names:
|
| 74 |
+
cevap = "Orta seviye tehdit."
|
| 75 |
+
else:
|
| 76 |
+
cevap = "Düşük tehdit seviyesi."
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
else:
|
| 79 |
+
cevap = "Geçersiz soru."
|
| 80 |
+
|
| 81 |
+
return chat_history + [(soru_id, cevap)]
|
| 82 |
+
|
| 83 |
+
# Gradio Blocks yapısı
|
| 84 |
+
with gr.Blocks() as demo:
|
| 85 |
+
gr.Markdown("## 🪖 YOLO Görselden Nesne Tespiti + Soru-Cevap Arayüzü")
|
| 86 |
+
|
| 87 |
+
with gr.Row():
|
| 88 |
+
with gr.Column():
|
| 89 |
+
image_input = gr.Image(type="pil", label="Görsel Yükle")
|
| 90 |
+
tahmin_bilgisi = gr.Textbox(label="Tahmin Durumu")
|
| 91 |
+
hidden_data = gr.State()
|
| 92 |
+
upload_button = gr.Button("📤 Tahmin Yap")
|
| 93 |
+
with gr.Column():
|
| 94 |
+
chatbot = gr.Chatbot(label="📡 Soru-Cevap Paneli")
|
| 95 |
+
with gr.Row():
|
| 96 |
+
b1 = gr.Button("1️⃣ Kaç nesne ve kaçar adet?")
|
| 97 |
+
b2 = gr.Button("2️⃣ Savunma sistemleri var mı?")
|
| 98 |
+
with gr.Row():
|
| 99 |
+
b3 = gr.Button("3️⃣ Askeri birlik tespiti?")
|
| 100 |
+
b4 = gr.Button("4️⃣ Hava unsurları var mı?")
|
| 101 |
+
b5 = gr.Button("5️⃣ Tehdit seviyesi nedir?")
|
| 102 |
+
|
| 103 |
+
# Buton olayları
|
| 104 |
+
upload_button.click(save_and_predict, inputs=image_input, outputs=[hidden_data, tahmin_bilgisi])
|
| 105 |
+
b1.click(cevapla, inputs=["b1", hidden_data, chatbot], outputs=chatbot).then(fn=lambda: "1️⃣ Kaç nesne ve kaçar adet?", outputs="b1")
|
| 106 |
+
b2.click(cevapla, inputs=["b2", hidden_data, chatbot], outputs=chatbot).then(fn=lambda: "2️⃣ Savunma sistemleri var mı?", outputs="b2")
|
| 107 |
+
b3.click(cevapla, inputs=["b3", hidden_data, chatbot], outputs=chatbot).then(fn=lambda: "3️⃣ Askeri birlik tespiti?", outputs="b3")
|
| 108 |
+
b4.click(cevapla, inputs=["b4", hidden_data, chatbot], outputs=chatbot).then(fn=lambda: "4️⃣ Hava unsurları var mı?", outputs="b4")
|
| 109 |
+
b5.click(cevapla, inputs=["b5", hidden_data, chatbot], outputs=chatbot).then(fn=lambda: "5️⃣ Tehdit seviyesi nedir?", outputs="b5")
|
| 110 |
+
|
| 111 |
+
demo.launch()
|