Caries1 commited on
Commit
010e950
·
verified ·
1 Parent(s): 2850096

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -46
app.py CHANGED
@@ -1,66 +1,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
- # best.pt dosyanın ana dizinde olduğundan emin ol
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 (Plotted image)
31
- # conf=False yaparak karelerin üzerindeki güven skorunu (sayıyı) kaldırdık
32
- res_plotted = results[0].plot(labels=True, conf=False)
33
-
34
- # BGR'den RGB'ye dönüştür (Gradio RGB bekler)
35
- return cv2.cvtColor(res_plotted, cv2.COLOR_BGR2RGB)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  # Arayüz - Gradio 6 Modern Tasarımı
 
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 col1 := 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 col2 := 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
  # Hugging Face Spaces üzerinde çalışması için port yapılandırması
 
65
  if __name__ == "__main__":
66
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
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
+ # best.pt dosyanın ana dizinde olduğundan emin ol
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
+ if image is None:
36
+
37
+ return None
38
+
39
+
40
+ # Tahmin (Inference) yap
41
+
42
+ # imgsz=640 standarttır, device='cpu' ücretsiz Hugging Face katmanı içindir
43
+
44
+ results = model.predict(
45
+
46
+ source=image,
47
+
48
+ conf=conf_threshold,
49
+
50
+ iou=0.45,
51
+
52
+ imgsz=640,
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("# 🦷 Dental Caries Detection System For Akansh Mani")
77
+
78
+ gr.Markdown("Upload the X-ray image and wait for the AI ​​to analyze it.")
79
+
80
+
81
+ with gr.Row():
82
+
83
+ with gr.Column():
84
+
85
+ input_img = gr.Image(type="numpy", label="Upload X-Ray")
86
+
87
+ conf_slider = gr.Slider(
88
+
89
+ minimum=0.01,
90
+
91
+ maximum=1.0,
92
+
93
+ value=0.25,
94
+
95
+ step=0.01,
96
+
97
+ label="Confidence Threshold"
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
+