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 = """
Bu uygulama, derin öğrenme modeli kullanarak hayvan türlerini tanıyabilen bir sınıflandırıcıdır.
Basitçe bir görüntü yükleyin veya webcam kullanarak fotoğraf çekin, model hangi hayvan olduğunu tahmin edecek!
Model, 90 farklı hayvan sınıfını tanıyabilmektedir.
Nasıl Kullanılır: