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

Update app_huggingface.py

Browse files
Files changed (1) hide show
  1. app_huggingface.py +51 -68
app_huggingface.py CHANGED
@@ -1,5 +1,4 @@
1
  import os
2
- import sys
3
  import numpy as np
4
  import gradio as gr
5
  import tensorflow as tf
@@ -7,24 +6,25 @@ from PIL import Image
7
 
8
  # Sabitler
9
  IMAGE_SIZE = 128
10
- MODEL_PATH = "best_model.keras" # Kaydedilmiş model dosyasının yolu
11
 
12
- # Hayvan sınıfları (90 sınıf)
13
  class_names = [
14
  'antelope', 'badger', 'bat', 'bear', 'bee', 'beetle', 'bison', 'boar', 'butterfly',
15
  'cat', 'caterpillar', 'chimpanzee', 'cockroach', 'cow', 'coyote', 'crab', 'crow',
16
- 'deer', 'dog', 'dolphin', 'donkey', 'dragonfly', 'duck', 'eagle', 'elephant', 'flamingo',
17
- 'fly', 'fox', 'goat', 'goldfish', 'goose', 'gorilla', 'grasshopper', 'hamster', 'hare',
18
- 'hedgehog', 'hippopotamus', 'hornbill', 'horse', 'hummingbird', 'hyena', 'jellyfish',
19
- 'kangaroo', 'koala', 'ladybugs', 'leopard', 'lion', 'lizard', 'lobster', 'mosquito',
20
- 'moth', 'mouse', 'octopus', 'okapi', 'orangutan', 'otter', 'owl', 'ox', 'oyster',
21
- 'panda', 'parrot', 'pelecaniformes', 'penguin', 'pig', 'pigeon', 'porcupine',
22
- 'possum', 'raccoon', 'rat', 'reindeer', 'rhinoceros', 'sandpiper', 'seahorse',
23
- 'seal', 'shark', 'sheep', 'snake', 'sparrow', 'squid', 'squirrel', 'starfish',
24
- 'swan', 'tiger', 'turkey', 'turtle', 'whale', 'wolf', 'wombat', 'woodpecker', 'zebra'
 
25
  ]
26
 
27
- # Model yükleme fonksiyonu
28
  def load_model():
29
  try:
30
  if os.path.exists(MODEL_PATH):
@@ -38,12 +38,11 @@ def load_model():
38
  print(f"Model yüklenirken hata oluştu: {e}")
39
  return None
40
 
41
- # Görüntü ön işleme
42
  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
47
  img_array = np.expand_dims(img_array, axis=0)
48
  return img_array
49
 
@@ -53,10 +52,8 @@ def predict(img):
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]
@@ -65,98 +62,84 @@ def predict(img):
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>
97
  <p>Basitçe bir görüntü yükleyin veya webcam kullanarak fotoğraf çekin, model hangi hayvan olduğunu tahmin edecek!</p>
98
- <p>Model, <strong>90 farklı hayvan sınıfını</strong> tanıyabilmektedir.</p>
99
  <hr>
100
  <p><strong>Nasıl Kullanılır:</strong></p>
101
  <ol>
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
 
111
- # Özel CSS stilleri
112
  css = """
113
  .gradio-container {
114
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
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;
140
- color: #999;
141
- border-top: 1px solid #333;
142
  padding-top: 20px;
143
- background-color: #1a1a1a;
144
  }
145
  h1, h2, h3 {
146
- color: #ffffff;
147
  }
148
  """
149
 
150
- # Sayfa altı bilgisi
151
- footer = """
152
  <div class="footer">
153
  <p>Bu uygulama TensorFlow ve Gradio kullanılarak geliştirilmiştir.</p>
154
  <p>Derin öğrenme modeli, hayvan görüntülerinden oluşan kapsamlı bir veri seti üzerinde eğitilmiştir.</p>
155
- <p>© 2023 Hayvan Sınıflandırıcı</p>
 
 
 
 
 
 
 
 
 
156
  </div>
157
  """
158
 
159
- # Gradio arayüzü oluşturma
160
  demo = gr.Interface(
161
  fn=predict,
162
  inputs=gr.Image(type="pil", label="Hayvan Görüntüsü Yükle"),
@@ -168,6 +151,6 @@ demo = gr.Interface(
168
  allow_flagging="never"
169
  )
170
 
171
- # Uygulamayı başlat
172
  if __name__ == "__main__":
173
  demo.launch()
 
1
  import os
 
2
  import numpy as np
3
  import gradio as gr
4
  import tensorflow as tf
 
6
 
7
  # Sabitler
8
  IMAGE_SIZE = 128
9
+ MODEL_PATH = "best_model.keras"
10
 
11
+ # 90 sınıflık hayvan isimleri
12
  class_names = [
13
  'antelope', 'badger', 'bat', 'bear', 'bee', 'beetle', 'bison', 'boar', 'butterfly',
14
  'cat', 'caterpillar', 'chimpanzee', 'cockroach', 'cow', 'coyote', 'crab', 'crow',
15
+ 'deer', 'dog', 'dolphin', 'donkey', 'dragonfly', 'duck', 'eagle', 'elephant',
16
+ 'flamingo', 'fly', 'fox', 'goat', 'goldfish', 'goose', 'gorilla', 'grasshopper',
17
+ 'hamster', 'hare', 'hedgehog', 'hippopotamus', 'hornbill', 'horse', 'hummingbird',
18
+ 'hyena', 'jellyfish', 'kangaroo', 'koala', 'ladybugs', 'leopard', 'lion', 'lizard',
19
+ 'lobster', 'mosquito', 'moth', 'mouse', 'octopus', 'okapi', 'orangutan', 'otter',
20
+ 'owl', 'ox', 'oyster', 'panda', 'parrot', 'pelecaniformes', 'penguin', 'pig',
21
+ 'pigeon', 'porcupine', 'possum', 'raccoon', 'rat', 'reindeer', 'rhinoceros',
22
+ 'sandpiper', 'seahorse', 'seal', 'shark', 'sheep', 'snake', 'sparrow', 'squid',
23
+ 'squirrel', 'starfish', 'swan', 'tiger', 'turkey', 'turtle', 'whale', 'wolf',
24
+ 'wombat', 'woodpecker', 'zebra'
25
  ]
26
 
27
+ # Model yükleyici
28
  def load_model():
29
  try:
30
  if os.path.exists(MODEL_PATH):
 
38
  print(f"Model yüklenirken hata oluştu: {e}")
39
  return None
40
 
41
+ # Görüntü işleme
42
  def preprocess_image(img):
43
  img = img.convert('RGB')
44
  img = img.resize((IMAGE_SIZE, IMAGE_SIZE))
45
+ img_array = np.array(img) / 255.0
 
46
  img_array = np.expand_dims(img_array, axis=0)
47
  return img_array
48
 
 
52
 
53
  if model is None:
54
  import random
55
+ predictions = np.random.rand(len(class_names))
56
+ predictions /= np.sum(predictions)
 
 
57
  else:
58
  img_array = preprocess_image(img)
59
  predictions = model.predict(img_array)[0]
 
62
  top_predictions = predictions[top_indices]
63
  top_classes = [class_names[i] for i in top_indices]
64
 
65
+ return {label: float(prob) for label, prob in zip(top_classes, top_predictions)}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
+ # Başlık
68
  title = "🦁 Hayvan Görüntü Sınıflandırıcı"
69
 
70
+ # Açıklama
71
+ description = """
72
  <div style="text-align: center;">
73
  <h2>Hayvan Görüntü Sınıflandırma Uygulaması</h2>
74
  <p>Bu uygulama, derin öğrenme modeli kullanarak hayvan türlerini tanıyabilen bir sınıflandırıcıdır.</p>
75
  <p>Basitçe bir görüntü yükleyin veya webcam kullanarak fotoğraf çekin, model hangi hayvan olduğunu tahmin edecek!</p>
76
+ <p><strong>Model, 90 farklı hayvan sınıfını</strong> tanıyabilmektedir.</p>
77
  <hr>
78
  <p><strong>Nasıl Kullanılır:</strong></p>
79
  <ol>
80
  <li>Bir hayvan resmi yükleyin veya webcam ile çekin</li>
81
  <li>Sonuçları anında görün!</li>
82
  </ol>
 
 
 
83
  </div>
84
  """
85
 
86
+ # CSS
87
  css = """
88
  .gradio-container {
89
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
90
+ background-color: #f5f5f5;
91
+ color: #222;
92
  }
93
  input, button, textarea, select {
94
+ background-color: #ffffff !important;
95
+ color: #000000 !important;
96
+ border: 1px solid #ccc !important;
97
  }
98
  label, .label, .output_class, .output_label, .upload_box {
99
+ color: #000 !important;
100
  }
101
  .gr-image-upload {
102
+ background-color: #fff !important;
103
+ border: 1px solid #ccc !important;
 
104
  }
105
  .examples {
106
+ background-color: #fafafa;
107
+ border: 1px solid #ddd;
108
  border-radius: 10px;
109
  padding: 15px;
110
  }
111
  .footer {
112
  margin-top: 30px;
113
  text-align: center;
114
+ color: #444;
115
+ border-top: 1px solid #ccc;
116
  padding-top: 20px;
117
+ background-color: #fafafa;
118
  }
119
  h1, h2, h3 {
120
+ color: #222;
121
  }
122
  """
123
 
124
+ # Footer
125
+ footer = f"""
126
  <div class="footer">
127
  <p>Bu uygulama TensorFlow ve Gradio kullanılarak geliştirilmiştir.</p>
128
  <p>Derin öğrenme modeli, hayvan görüntülerinden oluşan kapsamlı bir veri seti üzerinde eğitilmiştir.</p>
129
+ <hr>
130
+ <h3>Tanıyabildiği Hayvan Sınıfları ({len(class_names)})</h3>
131
+ <table style="margin:auto;">
132
+ {"".join([
133
+ "<tr>" + "".join(
134
+ f"<td style='padding:5px 15px;'>• {name}</td>" for name in class_names[i:i+6]
135
+ ) + "</tr>" for i in range(0, len(class_names), 6)
136
+ ])}
137
+ </table>
138
+ <p style="margin-top:20px;">© 2025 Hayvan Sınıflandırıcı</p>
139
  </div>
140
  """
141
 
142
+ # Gradio Arayüzü
143
  demo = gr.Interface(
144
  fn=predict,
145
  inputs=gr.Image(type="pil", label="Hayvan Görüntüsü Yükle"),
 
151
  allow_flagging="never"
152
  )
153
 
154
+ # Başlat
155
  if __name__ == "__main__":
156
  demo.launch()