anderni2 commited on
Commit
dab2414
·
verified ·
1 Parent(s): 162362a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -15
app.py CHANGED
@@ -6,21 +6,23 @@ import numpy as np
6
  # Klassennamen, sollten Ihrem Dataset entsprechen
7
  class_names = ['Apple__black_rot', 'Apple__healthy', 'Apple__rust', 'Apple__scab']
8
 
9
- def predict_figure(image):
10
- if image is None:
11
- return "Kein Bild bereitgestellt.", None, "Keine Vorhersage"
12
 
13
  model = tf.keras.models.load_model('apple_classification-2.keras')
14
- img = Image.fromarray(image).convert('RGB')
15
- img = img.resize((150, 150))
16
- img_array = np.array(img)
17
- prediction = model.predict(np.expand_dims(img_array, axis=0))
18
 
19
- confidences = {class_names[i]: np.round(float(prediction[0][i]), 2) for i in range(len(class_names))}
20
- max_confidence_class = max(confidences, key=confidences.get)
21
- response = generate_response(max_confidence_class)
 
22
 
23
- return img, confidences, response
 
 
 
 
 
24
 
25
  def generate_response(predicted_class):
26
  responses = {
@@ -39,12 +41,13 @@ examples = [
39
  ["images/Apple__scab.JPG"]
40
  ]
41
 
 
42
  iface = gr.Interface(
43
  fn=predict_figure,
44
- inputs=gr.components.Image(shape=(150, 150), source="webcam", tool="editor", label="Foto aufnehmen oder hochladen"),
45
- outputs=["image", "label", "text"],
46
  title="Klassifikator für Apfelblattkrankheiten",
47
- description="Nimm ein Foto eines Apfelblatts auf oder lade es hoch, um den Gesundheitszustand zu überprüfen.",
48
  examples=examples
49
  )
50
- iface.launch()
 
6
  # Klassennamen, sollten Ihrem Dataset entsprechen
7
  class_names = ['Apple__black_rot', 'Apple__healthy', 'Apple__rust', 'Apple__scab']
8
 
9
+ def predict_figure(uploaded_file):
10
+ if uploaded_file is None:
11
+ return "Keine Datei hochgeladen.", None, "Keine Vorhersage"
12
 
13
  model = tf.keras.models.load_model('apple_classification-2.keras')
 
 
 
 
14
 
15
+ with Image.open(uploaded_file).convert('RGB') as img:
16
+ img = img.resize((150, 150))
17
+ img_array = np.array(img)
18
+ prediction = model.predict(np.expand_dims(img_array, axis=0))
19
 
20
+ # Identify the most confident prediction
21
+ confidences = {class_names[i]: np.round(float(prediction[0][i]), 2) for i in range(len(class_names))}
22
+ max_confidence_class = max(confidences, key=confidences.get)
23
+ response = generate_response(max_confidence_class)
24
+
25
+ return img, confidences, response
26
 
27
  def generate_response(predicted_class):
28
  responses = {
 
41
  ["images/Apple__scab.JPG"]
42
  ]
43
 
44
+ # Define the Gradio interface
45
  iface = gr.Interface(
46
  fn=predict_figure,
47
+ inputs=gr.File(label="Datei hochladen"),
48
+ outputs=["image", "text", "text"],
49
  title="Klassifikator für Apfelblattkrankheiten",
50
+ description="Lade ein Bild eines Apfelblatts hoch, um den Gesundheitszustand und die Vertrauenswürdigkeit des Modells zu sehen.",
51
  examples=examples
52
  )
53
+ iface.launch()