Kupredom commited on
Commit
7041965
·
verified ·
1 Parent(s): b5871a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -61
app.py CHANGED
@@ -1,61 +1,46 @@
1
- import gradio as gr
2
- import tensorflow as tf
3
- print(tf.__version__)
4
- import numpy as np
5
- from PIL import Image
6
-
7
- import os
8
-
9
- #model_path = "apple_model_transferlearning.keras"
10
- model_path = r"C:\Users\dom-k\Visual Studio Code\6. Semester\Project1_Kupredom\Image_Classification\3_Model_Training_and_Application\apple_model_transferlearning.keras"
11
- print("Existiert die Modell-Datei?:", os.path.exists(model_path))
12
- model = tf.keras.models.load_model(model_path)
13
-
14
-
15
-
16
- def predict_pokemon(image):
17
- # Preprocess image
18
- print(type(image))
19
- image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
20
- image = image.resize((150, 150)) # Resize the image to 150x150 pixels
21
- image = np.array(image)
22
- image = np.expand_dims(image, axis=0) # Add batch dimension
23
-
24
- # Predict
25
- prediction = model.predict(image)
26
- # Convert the probabilities to rounded values
27
- prediction = np.round(prediction, 2)
28
-
29
- # Make sure the indices are correct according to your model's training
30
- p_schorf = prediction[0][0] # Probability for "Schorf"
31
- p_schwarzfaeule = prediction[0][1] # Probability for "Schwarzfaeule"
32
- p_zederapfel = prediction[0][2] # Probability for "Zederapfel"
33
- p_gesund = prediction[0][3] # Probability for "Gesund"
34
-
35
- return {'gesund': p_gesund, 'schorf': p_schorf, 'schwarzfaeule': p_schwarzfaeule, 'zederapfel': p_zederapfel}
36
-
37
-
38
-
39
- # Create the Gradio interface
40
- input_image = gr.Image()
41
- iface = gr.Interface(
42
- fn=predict_pokemon,
43
- inputs=input_image,
44
- outputs=gr.Label(),
45
- examples=[r"C:\Users\dom-k\Visual Studio Code\6. Semester\Project1_Kupredom\Image_Classification\2_Data_collection\apple_images\model\Gesund1.jpg",
46
- r"C:\Users\dom-k\Visual Studio Code\6. Semester\Project1_Kupredom\Image_Classification\2_Data_collection\apple_images\model\Gesund2.jpg",
47
- r"C:\Users\dom-k\Visual Studio Code\6. Semester\Project1_Kupredom\Image_Classification\2_Data_collection\apple_images\model\Gesund3.jpg",
48
- r"C:\Users\dom-k\Visual Studio Code\6. Semester\Project1_Kupredom\Image_Classification\2_Data_collection\apple_images\model\Schorf1.jpg",
49
- r"C:\Users\dom-k\Visual Studio Code\6. Semester\Project1_Kupredom\Image_Classification\2_Data_collection\apple_images\model\Schorf2.jpg",
50
- r"C:\Users\dom-k\Visual Studio Code\6. Semester\Project1_Kupredom\Image_Classification\2_Data_collection\apple_images\model\Schorf3.jpg",
51
- r"C:\Users\dom-k\Visual Studio Code\6. Semester\Project1_Kupredom\Image_Classification\2_Data_collection\apple_images\model\Schwarzfaeule1.jpg",
52
- r"C:\Users\dom-k\Visual Studio Code\6. Semester\Project1_Kupredom\Image_Classification\2_Data_collection\apple_images\model\Schwarzfaeule2.jpg",
53
- r"C:\Users\dom-k\Visual Studio Code\6. Semester\Project1_Kupredom\Image_Classification\2_Data_collection\apple_images\model\Schwarzfaeule3.jpg",
54
- r"C:\Users\dom-k\Visual Studio Code\6. Semester\Project1_Kupredom\Image_Classification\2_Data_collection\apple_images\model\Zederapfel1.jpg",
55
- r"C:\Users\dom-k\Visual Studio Code\6. Semester\Project1_Kupredom\Image_Classification\2_Data_collection\apple_images\model\Zederapfel2.jpg",
56
- r"C:\Users\dom-k\Visual Studio Code\6. Semester\Project1_Kupredom\Image_Classification\2_Data_collection\apple_images\model\Zederapfel3.jpg"],
57
- description="Model")
58
-
59
- iface.launch()
60
-
61
-
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ print(tf.__version__)
4
+ import numpy as np
5
+ from PIL import Image
6
+
7
+ import os
8
+
9
+ model_path = "apple_model_transferlearning.keras"
10
+ model = tf.keras.models.load_model(model_path)
11
+
12
+ def predict_apple(image):
13
+ # Preprocess image
14
+ print(type(image))
15
+ image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
16
+ image = image.resize((150, 150)) # Resize the image to 150x150 pixels
17
+ image = np.array(image)
18
+ image = np.expand_dims(image, axis=0) # Add batch dimension
19
+
20
+ # Predict
21
+ prediction = model.predict(image)
22
+ # Convert the probabilities to rounded values
23
+ prediction = np.round(prediction, 2)
24
+
25
+ # Make sure the indices are correct according to your model's training
26
+ p_schorf = prediction[0][0] # Probability for "Schorf"
27
+ p_schwarzfaeule = prediction[0][1] # Probability for "Schwarzfaeule"
28
+ p_zederapfel = prediction[0][2] # Probability for "Zederapfel"
29
+ p_gesund = prediction[0][3] # Probability for "Gesund"
30
+
31
+ return {'gesund': p_gesund, 'schorf': p_schorf, 'schwarzfaeule': p_schwarzfaeule, 'zederapfel': p_zederapfel}
32
+
33
+
34
+ # Create the Gradio interface
35
+ input_image = gr.Image()
36
+ iface = gr.Interface(
37
+ fn=predict_apple,
38
+ inputs=input_image,
39
+ outputs=gr.Label(),
40
+ examples=["images/Gesund1.jpg",
41
+ "images/Gesund3.jpg",
42
+ "images/Gesund3.jpg"],
43
+ description="APPLE MODEL")
44
+
45
+ iface.launch()
46
+