mybYusuf commited on
Commit
ffcfd9d
·
verified ·
1 Parent(s): e9967f3

Update app_huggingface.py

Browse files
Files changed (1) hide show
  1. app_huggingface.py +24 -18
app_huggingface.py CHANGED
@@ -43,8 +43,8 @@ def preprocess_image(img):
43
  img = img.convert('RGB')
44
  img = img.resize((IMAGE_SIZE, IMAGE_SIZE))
45
  img_array = np.array(img)
46
- img_array = img_array / 255.0 # [0,1] aralığına normalizasyon
47
- img_array = np.expand_dims(img_array, axis=0) # Batch boyutu ekleme
48
  return img_array
49
 
50
  # Tahmin fonksiyonu
@@ -52,34 +52,45 @@ def predict(img):
52
  model = load_model()
53
 
54
  if model is None:
55
- # Demo modu - rastgele tahminler üret
56
  import random
57
  predictions = np.zeros(len(class_names))
58
  for i in range(len(predictions)):
59
  predictions[i] = random.random()
60
- predictions = predictions / np.sum(predictions) # 1'e normalize et
61
  else:
62
- # Gerçek tahmin
63
  img_array = preprocess_image(img)
64
  predictions = model.predict(img_array)[0]
65
 
66
- # En iyi 3 tahmini al
67
  top_indices = predictions.argsort()[-3:][::-1]
68
  top_predictions = predictions[top_indices]
69
  top_classes = [class_names[i] for i in top_indices]
70
 
71
- # Sonuçları formatla
72
  results = {}
73
  for i, (label, prob) in enumerate(zip(top_classes, top_predictions)):
74
  results[label] = float(prob)
75
 
76
  return results
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  # Proje başlığı
79
  title = "🦁 Hayvan Görüntü Sınıflandırıcı"
80
 
81
- # Proje açıklaması
82
- description = """
83
  <div style="text-align: center;">
84
  <h2>Hayvan Görüntü Sınıflandırma Uygulaması</h2>
85
  <p>Bu uygulama, derin öğrenme modeli kullanarak hayvan türlerini tanıyabilen bir sınıflandırıcıdır.</p>
@@ -91,6 +102,9 @@ description = """
91
  <li>Bir hayvan resmi yükleyin veya webcam ile çekin</li>
92
  <li>Sonuçları anında görün!</li>
93
  </ol>
 
 
 
94
  </div>
95
  """
96
 
@@ -101,30 +115,25 @@ css = """
101
  background-color: #121212;
102
  color: #f0f0f0;
103
  }
104
-
105
  input, button, textarea, select {
106
  background-color: #1e1e1e !important;
107
  color: #ffffff !important;
108
  border: 1px solid #333 !important;
109
  }
110
-
111
  label, .label, .output_class, .output_label, .upload_box {
112
  color: #f0f0f0 !important;
113
  }
114
-
115
  .gr-image-upload {
116
  background-color: #1e1e1e !important;
117
  border: 1px solid #444 !important;
118
  color: #f0f0f0 !important;
119
  }
120
-
121
  .examples {
122
  background-color: #1a1a1a;
123
  border: 1px solid #333;
124
  border-radius: 10px;
125
  padding: 15px;
126
  }
127
-
128
  .footer {
129
  margin-top: 30px;
130
  text-align: center;
@@ -138,9 +147,6 @@ h1, h2, h3 {
138
  }
139
  """
140
 
141
-
142
-
143
-
144
  # Sayfa altı bilgisi
145
  footer = """
146
  <div class="footer">
@@ -164,4 +170,4 @@ demo = gr.Interface(
164
 
165
  # Uygulamayı başlat
166
  if __name__ == "__main__":
167
- demo.launch()
 
43
  img = img.convert('RGB')
44
  img = img.resize((IMAGE_SIZE, IMAGE_SIZE))
45
  img_array = np.array(img)
46
+ img_array = img_array / 255.0
47
+ img_array = np.expand_dims(img_array, axis=0)
48
  return img_array
49
 
50
  # Tahmin fonksiyonu
 
52
  model = load_model()
53
 
54
  if model is None:
 
55
  import random
56
  predictions = np.zeros(len(class_names))
57
  for i in range(len(predictions)):
58
  predictions[i] = random.random()
59
+ predictions = predictions / np.sum(predictions)
60
  else:
 
61
  img_array = preprocess_image(img)
62
  predictions = model.predict(img_array)[0]
63
 
 
64
  top_indices = predictions.argsort()[-3:][::-1]
65
  top_predictions = predictions[top_indices]
66
  top_classes = [class_names[i] for i in top_indices]
67
 
 
68
  results = {}
69
  for i, (label, prob) in enumerate(zip(top_classes, top_predictions)):
70
  results[label] = float(prob)
71
 
72
  return results
73
 
74
+ # --- Sınıfları tabloya dönüştüren yardımcı fonksiyon ---
75
+ def format_classes_grid(class_names, columns=6):
76
+ rows = [class_names[i:i + columns] for i in range(0, len(class_names), columns)]
77
+ table_html = '<table style="margin:auto; text-align:left; border-spacing: 10px;">'
78
+ for row in rows:
79
+ table_html += "<tr>"
80
+ for item in row:
81
+ table_html += f"<td>• {item}</td>"
82
+ table_html += "</tr>"
83
+ table_html += "</table>"
84
+ return table_html
85
+
86
+ # Sınıf tablosunu oluştur
87
+ class_table_html = format_classes_grid(class_names)
88
+
89
  # Proje başlığı
90
  title = "🦁 Hayvan Görüntü Sınıflandırıcı"
91
 
92
+ # Proje açıklaması (sınıf tablosu dahil)
93
+ description = f"""
94
  <div style="text-align: center;">
95
  <h2>Hayvan Görüntü Sınıflandırma Uygulaması</h2>
96
  <p>Bu uygulama, derin öğrenme modeli kullanarak hayvan türlerini tanıyabilen bir sınıflandırıcıdır.</p>
 
102
  <li>Bir hayvan resmi yükleyin veya webcam ile çekin</li>
103
  <li>Sonuçları anında görün!</li>
104
  </ol>
105
+ <hr>
106
+ <h3 style="text-align:center;">Tanıyabildiği Hayvan Sınıfları (90)</h3>
107
+ {class_table_html}
108
  </div>
109
  """
110
 
 
115
  background-color: #121212;
116
  color: #f0f0f0;
117
  }
 
118
  input, button, textarea, select {
119
  background-color: #1e1e1e !important;
120
  color: #ffffff !important;
121
  border: 1px solid #333 !important;
122
  }
 
123
  label, .label, .output_class, .output_label, .upload_box {
124
  color: #f0f0f0 !important;
125
  }
 
126
  .gr-image-upload {
127
  background-color: #1e1e1e !important;
128
  border: 1px solid #444 !important;
129
  color: #f0f0f0 !important;
130
  }
 
131
  .examples {
132
  background-color: #1a1a1a;
133
  border: 1px solid #333;
134
  border-radius: 10px;
135
  padding: 15px;
136
  }
 
137
  .footer {
138
  margin-top: 30px;
139
  text-align: center;
 
147
  }
148
  """
149
 
 
 
 
150
  # Sayfa altı bilgisi
151
  footer = """
152
  <div class="footer">
 
170
 
171
  # Uygulamayı başlat
172
  if __name__ == "__main__":
173
+ demo.launch()