Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,129 +1,66 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
import cv2
|
| 4 |
-
|
| 5 |
import numpy as np
|
| 6 |
-
|
| 7 |
import gradio as gr
|
| 8 |
-
|
| 9 |
from ultralytics import YOLO
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
# Hugging Face Spaces'te yazma hatalarını önlemek için geçici dizin ayarı
|
| 14 |
-
|
| 15 |
os.environ['YOLO_CONFIG_DIR'] = '/tmp'
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
# Modeli yükle
|
| 20 |
-
|
| 21 |
try:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
model = YOLO("best.pt")
|
| 26 |
-
|
| 27 |
except Exception as e:
|
| 28 |
-
|
| 29 |
-
print(f"Model yüklenirken bir hata oluştu: {e}")
|
| 30 |
-
|
| 31 |
-
|
| 32 |
|
| 33 |
def predict_caries(image, conf_threshold):
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
conf=
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
device='cpu'
|
| 55 |
-
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
# Sonuçları çizdir (Plotted image)
|
| 60 |
-
|
| 61 |
-
# Gradio 6 ve Ultralytics uyumu için numpy formatına çeviriyoruz
|
| 62 |
-
|
| 63 |
-
res_plotted = results[0].plot(labels=True, conf=True)
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
# BGR'den RGB'ye dönüştür (Gradio RGB bekler)
|
| 67 |
-
|
| 68 |
-
return cv2.cvtColor(res_plotted, cv2.COLOR_BGR2RGB)
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
# Arayüz - Gradio 6 Modern Tasarımı
|
| 73 |
-
|
| 74 |
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
| 75 |
-
|
| 76 |
-
gr.Markdown("
|
| 77 |
-
|
| 78 |
-
gr.
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
btn = gr.Button("🔍 Run AI Analysis", variant="primary")
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
with gr.Column():
|
| 105 |
-
|
| 106 |
-
output_img = gr.Image(type="numpy", label="Detection Result")
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
# Buton tetikleyicisi
|
| 111 |
-
|
| 112 |
-
btn.click(
|
| 113 |
-
|
| 114 |
-
fn=predict_caries,
|
| 115 |
-
|
| 116 |
-
inputs=[input_img, conf_slider],
|
| 117 |
-
|
| 118 |
-
outputs=output_img
|
| 119 |
-
|
| 120 |
-
)
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
# Hugging Face Spaces üzerinde çalışması için port yapılandırması
|
| 125 |
-
|
| 126 |
if __name__ == "__main__":
|
| 127 |
-
|
| 128 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 129 |
-
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import cv2
|
|
|
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
from ultralytics import YOLO
|
| 6 |
|
|
|
|
|
|
|
| 7 |
# Hugging Face Spaces'te yazma hatalarını önlemek için geçici dizin ayarı
|
|
|
|
| 8 |
os.environ['YOLO_CONFIG_DIR'] = '/tmp'
|
| 9 |
|
|
|
|
|
|
|
| 10 |
# Modeli yükle
|
|
|
|
| 11 |
try:
|
| 12 |
+
# Boşluklara dikkat: try bloğunun altındaki satırlar içeride olmalı
|
| 13 |
+
model = YOLO("best.pt")
|
|
|
|
|
|
|
|
|
|
| 14 |
except Exception as e:
|
| 15 |
+
print(f"Model yüklenirken bir hata oluştu: {e}")
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def predict_caries(image, conf_threshold):
|
| 18 |
+
if image is None:
|
| 19 |
+
return None
|
| 20 |
+
|
| 21 |
+
# Tahmin (Inference) yap
|
| 22 |
+
results = model.predict(
|
| 23 |
+
source=image,
|
| 24 |
+
conf=conf_threshold,
|
| 25 |
+
iou=0.45,
|
| 26 |
+
imgsz=640,
|
| 27 |
+
device='cpu'
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Sonuçları çizdir
|
| 31 |
+
# conf=False: Karelerin üzerindeki güven skorunu (sayıları) gizler
|
| 32 |
+
res_plotted = results[0].plot(labels=True, conf=False)
|
| 33 |
+
|
| 34 |
+
# BGR'den RGB'ye dönüştür
|
| 35 |
+
return cv2.cvtColor(res_plotted, cv2.COLOR_BGR2RGB)
|
| 36 |
+
|
| 37 |
+
# Arayüz
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
| 39 |
+
gr.Markdown("# 🦷 Dental Caries Detection System For Akansh Mani")
|
| 40 |
+
gr.Markdown("Upload the X-ray image and wait for the AI to analyze it.")
|
| 41 |
+
|
| 42 |
+
with gr.Row():
|
| 43 |
+
with gr.Column():
|
| 44 |
+
input_img = gr.Image(type="numpy", label="Upload X-Ray")
|
| 45 |
+
conf_slider = gr.Slider(
|
| 46 |
+
minimum=0.01,
|
| 47 |
+
maximum=1.0,
|
| 48 |
+
value=0.25,
|
| 49 |
+
step=0.01,
|
| 50 |
+
label="Confidence Threshold"
|
| 51 |
+
)
|
| 52 |
+
btn = gr.Button("🔍 Run AI Analysis", variant="primary")
|
| 53 |
+
|
| 54 |
+
with gr.Column():
|
| 55 |
+
output_img = gr.Image(type="numpy", label="Detection Result")
|
| 56 |
+
|
| 57 |
+
# Buton tetikleyicisi
|
| 58 |
+
btn.click(
|
| 59 |
+
fn=predict_caries,
|
| 60 |
+
inputs=[input_img, conf_slider],
|
| 61 |
+
outputs=output_img
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Başlatma
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
if __name__ == "__main__":
|
| 66 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|