Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,64 @@
|
|
| 1 |
import os
|
| 2 |
-
# Kütüphane yükleme hatalarını minimize etmek için
|
| 3 |
-
os.environ['SYSTEM'] = 'spaces'
|
| 4 |
-
|
| 5 |
-
import gradio as gr
|
| 6 |
-
from ultralytics import YOLO
|
| 7 |
import cv2
|
| 8 |
import numpy as np
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
os.environ['YOLO_CONFIG_DIR'] = '/tmp
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def predict_caries(image, conf_threshold):
|
| 17 |
if image is None:
|
| 18 |
return None
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
return cv2.cvtColor(res_plotted, cv2.COLOR_BGR2RGB)
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
with gr.Row():
|
| 26 |
with gr.Column():
|
| 27 |
input_img = gr.Image(type="numpy", label="Upload X-Ray")
|
| 28 |
-
conf_slider = gr.Slider(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
btn = gr.Button("🔍 Run AI Analysis", variant="primary")
|
|
|
|
| 30 |
with gr.Column():
|
| 31 |
output_img = gr.Image(type="numpy", label="Result")
|
| 32 |
|
| 33 |
-
btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
if __name__ == "__main__":
|
| 36 |
-
|
|
|
|
|
|
| 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 izni olan tek yer /tmp dizinidir.
|
| 8 |
+
os.environ['YOLO_CONFIG_DIR'] = '/tmp'
|
| 9 |
|
| 10 |
+
# Modeli yükle (best.pt dosyasının app.py ile aynı klasörde olduğundan emin ol)
|
| 11 |
+
try:
|
| 12 |
+
model = YOLO("best.pt")
|
| 13 |
+
except Exception as e:
|
| 14 |
+
print(f"Model yükleme hatası: {e}")
|
| 15 |
|
| 16 |
def predict_caries(image, conf_threshold):
|
| 17 |
if image is None:
|
| 18 |
return None
|
| 19 |
+
|
| 20 |
+
# Predict işlemi
|
| 21 |
+
# save=False ve exist_ok=True parametreleri hata almanı önler
|
| 22 |
+
results = model.predict(
|
| 23 |
+
source=image,
|
| 24 |
+
conf=conf_threshold,
|
| 25 |
+
iou=0.45,
|
| 26 |
+
imgsz=640,
|
| 27 |
+
device='cpu' # CPU üzerinde çalıştığından emin oluyoruz
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Sonucu görselleştir
|
| 31 |
+
res_plotted = results[0].plot(labels=True, conf=True)
|
| 32 |
+
|
| 33 |
+
# OpenCV BGR formatında döner, Gradio RGB bekler
|
| 34 |
return cv2.cvtColor(res_plotted, cv2.COLOR_BGR2RGB)
|
| 35 |
|
| 36 |
+
# Arayüz Oluşturma
|
| 37 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 38 |
+
gr.Markdown("# 🦷 Dental Caries Detection System")
|
| 39 |
+
gr.Markdown("X-ray görüntüsünü yükle ve AI analizini başlat.")
|
| 40 |
+
|
| 41 |
with gr.Row():
|
| 42 |
with gr.Column():
|
| 43 |
input_img = gr.Image(type="numpy", label="Upload X-Ray")
|
| 44 |
+
conf_slider = gr.Slider(
|
| 45 |
+
minimum=0.0,
|
| 46 |
+
maximum=1.0,
|
| 47 |
+
value=0.25,
|
| 48 |
+
step=0.05,
|
| 49 |
+
label="Confidence Threshold"
|
| 50 |
+
)
|
| 51 |
btn = gr.Button("🔍 Run AI Analysis", variant="primary")
|
| 52 |
+
|
| 53 |
with gr.Column():
|
| 54 |
output_img = gr.Image(type="numpy", label="Result")
|
| 55 |
|
| 56 |
+
btn.click(
|
| 57 |
+
fn=predict_caries,
|
| 58 |
+
inputs=[input_img, conf_slider],
|
| 59 |
+
outputs=output_img
|
| 60 |
+
)
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
+
# Hugging Face için server_name ve server_port ayarları önemlidir
|
| 64 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|