Spaces:
Sleeping
Sleeping
| import os | |
| import numpy as np | |
| import gradio as gr | |
| import tensorflow as tf | |
| from PIL import Image | |
| # Sabitler | |
| IMAGE_SIZE = 128 | |
| MODEL_PATH = "best_model.keras" | |
| # 90 sınıflık hayvan isimleri | |
| class_names = [ | |
| 'antelope', 'badger', 'bat', 'bear', 'bee', 'beetle', 'bison', 'boar', 'butterfly', | |
| 'cat', 'caterpillar', 'chimpanzee', 'cockroach', 'cow', 'coyote', 'crab', 'crow', | |
| 'deer', 'dog', 'dolphin', 'donkey', 'dragonfly', 'duck', 'eagle', 'elephant', | |
| 'flamingo', 'fly', 'fox', 'goat', 'goldfish', 'goose', 'gorilla', 'grasshopper', | |
| 'hamster', 'hare', 'hedgehog', 'hippopotamus', 'hornbill', 'horse', 'hummingbird', | |
| 'hyena', 'jellyfish', 'kangaroo', 'koala', 'ladybugs', 'leopard', 'lion', 'lizard', | |
| 'lobster', 'mosquito', 'moth', 'mouse', 'octopus', 'okapi', 'orangutan', 'otter', | |
| 'owl', 'ox', 'oyster', 'panda', 'parrot', 'pelecaniformes', 'penguin', 'pig', | |
| 'pigeon', 'porcupine', 'possum', 'raccoon', 'rat', 'reindeer', 'rhinoceros', | |
| 'sandpiper', 'seahorse', 'seal', 'shark', 'sheep', 'snake', 'sparrow', 'squid', | |
| 'squirrel', 'starfish', 'swan', 'tiger', 'turkey', 'turtle', 'whale', 'wolf', | |
| 'wombat', 'woodpecker', 'zebra' | |
| ] | |
| # Model yükleyici | |
| def load_model(): | |
| try: | |
| if os.path.exists(MODEL_PATH): | |
| model = tf.keras.models.load_model(MODEL_PATH) | |
| print("Model başarıyla yüklendi!") | |
| return model | |
| else: | |
| print("Model dosyası bulunamadı. Demo modunda çalışılacak.") | |
| return None | |
| except Exception as e: | |
| print(f"Model yüklenirken hata oluştu: {e}") | |
| return None | |
| # Görüntü işleme | |
| def preprocess_image(img): | |
| img = img.convert('RGB') | |
| img = img.resize((IMAGE_SIZE, IMAGE_SIZE)) | |
| img_array = np.array(img) / 255.0 | |
| img_array = np.expand_dims(img_array, axis=0) | |
| return img_array | |
| # Tahmin fonksiyonu | |
| def predict(img): | |
| model = load_model() | |
| if model is None: | |
| import random | |
| predictions = np.random.rand(len(class_names)) | |
| predictions /= np.sum(predictions) | |
| else: | |
| img_array = preprocess_image(img) | |
| predictions = model.predict(img_array)[0] | |
| top_indices = predictions.argsort()[-3:][::-1] | |
| top_predictions = predictions[top_indices] | |
| top_classes = [class_names[i] for i in top_indices] | |
| return {label: float(prob) for label, prob in zip(top_classes, top_predictions)} | |
| # Başlık | |
| title = "🦁 Hayvan Görüntü Sınıflandırıcı" | |
| # Açıklama | |
| description = """ | |
| <div style="text-align: center;"> | |
| <h2>Hayvan Görüntü Sınıflandırma Uygulaması</h2> | |
| <p>Bu uygulama, derin öğrenme modeli kullanarak hayvan türlerini tanıyabilen bir sınıflandırıcıdır.</p> | |
| <p>Basitçe bir görüntü yükleyin veya webcam kullanarak fotoğraf çekin, model hangi hayvan olduğunu tahmin edecek!</p> | |
| <p><strong>Model, 90 farklı hayvan sınıfını</strong> tanıyabilmektedir.</p> | |
| <hr> | |
| <p><strong>Nasıl Kullanılır:</strong></p> | |
| <ol> | |
| <li>Bir hayvan resmi yükleyin veya webcam ile çekin</li> | |
| <li>Sonuçları anında görün!</li> | |
| </ol> | |
| </div> | |
| """ | |
| # CSS | |
| css = """ | |
| .gradio-container { | |
| font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; | |
| background-color: #181818; | |
| color: #f0f0f0; | |
| } | |
| input, button, textarea, select { | |
| background-color: #2a2a2a !important; | |
| color: #ffffff !important; | |
| border: 1px solid #444 !important; | |
| } | |
| label, .label, .output_class, .output_label, .upload_box { | |
| color: #ffffff !important; | |
| } | |
| .gr-image-upload { | |
| background-color: #2a2a2a !important; | |
| border: 1px solid #555 !important; | |
| color: #ffffff !important; | |
| } | |
| .examples { | |
| background-color: #222; | |
| border: 1px solid #444; | |
| border-radius: 10px; | |
| padding: 15px; | |
| } | |
| h1, h2 { | |
| color: #ffd700; /* altın sarısı başlıklar */ | |
| } | |
| h3 { | |
| color: #90caf9; /* açık mavi alt başlık */ | |
| } | |
| table { | |
| background-color: #1f1f1f; | |
| border-collapse: collapse; | |
| } | |
| td { | |
| padding: 8px 15px; | |
| background-color: #2a2a2a; | |
| border: 1px solid #444; | |
| color: #ffffff; | |
| border-radius: 4px; | |
| } | |
| .footer { | |
| margin-top: 30px; | |
| text-align: center; | |
| color: #aaa; | |
| border-top: 1px solid #333; | |
| padding-top: 20px; | |
| background-color: #1e1e1e; | |
| } | |
| """ | |
| # Footer | |
| footer = f""" | |
| <div class="footer"> | |
| <p>Bu uygulama TensorFlow ve Gradio kullanılarak geliştirilmiştir.</p> | |
| <p>Derin öğrenme modeli, hayvan görüntülerinden oluşan kapsamlı bir veri seti üzerinde eğitilmiştir.</p> | |
| <hr> | |
| <h3>Tanıyabildiği Hayvan Sınıfları ({len(class_names)})</h3> | |
| <table style="margin:auto;"> | |
| {"".join([ | |
| "<tr>" + "".join( | |
| f"<td style='padding:5px 15px;'>• {name}</td>" for name in class_names[i:i+6] | |
| ) + "</tr>" for i in range(0, len(class_names), 6) | |
| ])} | |
| </table> | |
| <p style="margin-top:20px;">© 2025 Hayvan Sınıflandırıcı</p> | |
| </div> | |
| """ | |
| # Gradio Arayüzü | |
| demo = gr.Interface( | |
| fn=predict, | |
| inputs=gr.Image(type="pil", label="Hayvan Görüntüsü Yükle"), | |
| outputs=gr.Label(num_top_classes=3, label="En İyi 3 Tahmin"), | |
| title=title, | |
| description=description, | |
| article=footer, | |
| css=css, | |
| allow_flagging="never" | |
| ) | |
| # Başlat | |
| if __name__ == "__main__": | |
| demo.launch() | |