Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,36 @@
|
|
|
|
|
| 1 |
import tensorflow as tf
|
| 2 |
import numpy as np
|
| 3 |
-
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import requests
|
| 6 |
-
import os
|
| 7 |
-
|
| 8 |
-
# Hugging Face model URL'leri
|
| 9 |
-
MODEL_URL = "https://huggingface.co/YOUR_HF_USERNAME/YOUR_REPO_NAME/resolve/main/keras_model.h5"
|
| 10 |
-
LABELS_URL = "https://huggingface.co/YOUR_HF_USERNAME/YOUR_REPO_NAME/resolve/main/labels.txt"
|
| 11 |
-
|
| 12 |
-
# Dosya yolları
|
| 13 |
-
MODEL_PATH = "keras_model.h5"
|
| 14 |
-
LABELS_PATH = "labels.txt"
|
| 15 |
-
|
| 16 |
-
# Eğer dosya yoksa indir
|
| 17 |
-
def download_file(url, save_path):
|
| 18 |
-
if not os.path.exists(save_path):
|
| 19 |
-
print(f"{save_path} indiriliyor...")
|
| 20 |
-
response = requests.get(url)
|
| 21 |
-
with open(save_path, "wb") as f:
|
| 22 |
-
f.write(response.content)
|
| 23 |
-
|
| 24 |
-
download_file(MODEL_URL, MODEL_PATH)
|
| 25 |
-
download_file(LABELS_URL, LABELS_PATH)
|
| 26 |
|
| 27 |
# Modeli yükle
|
| 28 |
-
|
| 29 |
-
model = tf.keras.models.load_model(MODEL_PATH)
|
| 30 |
-
print("Model başarıyla yüklendi!")
|
| 31 |
-
except Exception as e:
|
| 32 |
-
print(f"Model yüklenirken hata oluştu: {e}")
|
| 33 |
-
|
| 34 |
-
# Etiketleri yükle
|
| 35 |
-
with open(LABELS_PATH, "r", encoding="utf-8") as f:
|
| 36 |
-
labels = [line.strip() for line in f.readlines()]
|
| 37 |
-
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
def
|
|
|
|
| 41 |
img = img.resize((224, 224))
|
| 42 |
-
img_array = np.asarray(img).astype(np.float32) / 255.0
|
| 43 |
-
img_array = np.expand_dims(img_array, axis=0)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
print("Tahmin Edilen Etiket Sırası:", label_index)
|
| 49 |
-
print("Tahmin Edilen Etiket:", labels[label_index])
|
| 50 |
-
print("Tüm Tahminler:", prediction)
|
| 51 |
-
|
| 52 |
-
return {labels[i]: float(prediction[0][i]) for i in range(len(labels))}
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
# Gradio
|
| 56 |
-
iface = gr.Interface(
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
outputs=gr.Label(),
|
| 60 |
-
title="Geri Dönüşüm Malzeme Tanıma",
|
| 61 |
-
description="Bir geri dönüşüm malzemesinin fotoğrafını yükleyin veya kameranızı kullanarak sınıflandırın."
|
| 62 |
-
)
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
+
from tensorflow.keras.preprocessing import image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Modeli yükle
|
| 7 |
+
model = tf.keras.models.load_model("model.h5")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Tahmin fonksiyonu
|
| 10 |
+
def predict_image(img):
|
| 11 |
+
# Görüntüyü 224x224 boyutuna küçült
|
| 12 |
img = img.resize((224, 224))
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Görüntüyü modelin beklediği formatta (numpy array) hazırla
|
| 15 |
+
img_array = np.array(img) / 255.0 # Piksel değerlerini normalize et
|
| 16 |
+
img_array = np.expand_dims(img_array, axis=0) # Modelin beklediği 4D input formatına getir
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Tahmin yap
|
| 19 |
+
prediction = model.predict(img_array)
|
| 20 |
+
|
| 21 |
+
# En yüksek tahmin sonucunu al
|
| 22 |
+
predicted_class = np.argmax(prediction, axis=1)[0]
|
| 23 |
+
|
| 24 |
+
# Geri dönüşüm malzemesi etiketlerini burada belirle
|
| 25 |
+
class_names = ['Plastik', 'Kağıt', 'Cam', 'Metal']
|
| 26 |
+
|
| 27 |
+
# Sonuçları döndür
|
| 28 |
+
return class_names[predicted_class]
|
| 29 |
|
| 30 |
+
# Gradio arayüzünü oluştur
|
| 31 |
+
iface = gr.Interface(fn=predict_image,
|
| 32 |
+
inputs=gr.inputs.Image(shape=(224, 224)), # Görüntüyü 224x224 boyutlarına getir
|
| 33 |
+
outputs="text") # Çıktıyı metin olarak göster (yani tahmin edilen etiket)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# Uygulamayı başlat
|
| 36 |
+
iface.launch(share=True)
|