anderni2 commited on
Commit
a42eb3e
·
verified ·
1 Parent(s): 3ce068a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -21
app.py CHANGED
@@ -4,34 +4,44 @@ from PIL import Image
4
  import numpy as np
5
 
6
 
7
- # Laden Sie Ihr benutzerdefiniertes Regressionsmodell
8
- model = tf.keras.models.load_model('Task_Pokemon.keras')
9
-
10
  # Klassennamen, sollten Ihrem Dataset entsprechen
11
- class_names = ['Aerodactyl', 'Charizard', 'Victreebel']
 
 
 
 
 
 
12
 
13
- def classify_image(image):
14
- image = Image.fromarray(image.astype('uint8'), 'RGB')
15
- img = image.resize((224, 224)) # Ändern Sie die Größe auf 224x224
16
- img_array = tf.keras.preprocessing.image.img_to_array(img)
17
- img_array = tf.expand_dims(img_array, 0) # Erstelle einen Batch
18
- predictions = model.predict(img_array)
19
- predicted_class = class_names[np.argmax(predictions[0])]
20
- confidence = np.max(predictions[0])
21
- return {predicted_class: float(confidence)}
22
 
 
23
 
24
- image_input = gr.Image()
25
- label = gr.Label(num_top_classes=3)
 
26
 
 
 
 
 
 
 
 
 
 
 
27
  iface = gr.Interface(
28
- fn=classify_image,
29
- inputs=image_input,
30
- outputs=label,
31
- title='Pokémon-Bildklassifizierer',
32
- description='Lade ein Bild von Aerodactyl, Charizard oder Victreebel hoch. Unser Klassifizierer wird das Pokémon identifizieren und das Vertrauensniveau der Vorhersage anzeigen.'
 
33
  )
34
-
35
  iface.launch()
36
 
37
 
 
4
  import numpy as np
5
 
6
 
 
 
 
7
  # Klassennamen, sollten Ihrem Dataset entsprechen
8
+ class_names = ['Apple__black_rot', 'Apple__healthy', 'Apple__rust', 'Apple__scab']
9
+
10
+ def predict_figure(uploaded_file):
11
+ if uploaded_file is None:
12
+ return "No file uploaded.", None, "No prediction"
13
+
14
+ model = tf.keras.models.load_model('apple_classification.keras')
15
 
16
+ # Load the image from the file path
17
+ with Image.open(uploaded_file).convert('RGB') as img:
18
+ img = img.resize((150, 150))
19
+ img_array = np.array(img)
 
 
 
 
 
20
 
21
+ prediction = model.predict(np.expand_dims(img_array, axis=0))
22
 
23
+ # Identify the most confident prediction
24
+ confidences = {class_names[i]: np.round(float(prediction[0][i]), 2) for i in range(len(class_names))}
25
+ return img, confidences
26
 
27
+ # Define example images
28
+ examples = [
29
+ ["images/Apple__black_rot.jpg"],
30
+ ["images/Apple__healthy.png"],
31
+ ["images/Apple__rust.png"],
32
+ ["images/Apple__scab.png"]
33
+ ]
34
+
35
+
36
+ # Define the Gradio interface
37
  iface = gr.Interface(
38
+ fn=predict_figure, # Function to process the input
39
+ inputs=gr.File(label="Upload File"), # File upload widget
40
+ outputs=["image", "text"], # Output types for image and text
41
+ title="Leaf Disease Classifier", # Title of the interface
42
+ description="Upload a picture of a Apple Leaf to see condition health it is and the model's confidence level.", # Description of the interface
43
+ examples=examples # Example images
44
  )
 
45
  iface.launch()
46
 
47