YusufArslan commited on
Commit
37ce3b0
·
verified ·
1 Parent(s): a3ca2ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -54
app.py CHANGED
@@ -1,64 +1,36 @@
 
1
  import tensorflow as tf
2
  import numpy as np
3
- import gradio as gr
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
- try:
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
- # Görüntü sınıflandırma fonksiyonu
40
- def classify_image(img):
 
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
- prediction = model.predict(img_array)
46
- label_index = np.argmax(prediction)
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 Arayüzü
56
- iface = gr.Interface(
57
- fn=classify_image,
58
- inputs=gr.Image(type="pil"),
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
- iface.launch()
 
 
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)