Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,57 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
def predict_mineral(img):
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from keras.models import load_model
|
3 |
+
from keras.preprocessing import image
|
4 |
+
from keras.applications.vgg16 import preprocess_input
|
5 |
+
import numpy as np
|
6 |
+
import cv2
|
7 |
+
from PIL import Image
|
8 |
+
import uuid
|
9 |
|
10 |
+
# Load your pre-trained model
|
11 |
+
model = KerasModel
|
12 |
+
|
13 |
+
# Function to preprocess the image for prediction
|
14 |
+
def preprocess_image(img_array, s=224):
|
15 |
+
img = (img_array * 255).astype(np.uint8) # Convert back to uint8
|
16 |
+
img_array = cv2.resize(img, (s, s))
|
17 |
+
img_array = img_array.astype(np.uint8)
|
18 |
+
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
19 |
+
return img_array
|
20 |
+
|
21 |
+
# Function to make predictions
|
22 |
def predict_mineral(img):
|
23 |
+
# Convert the PIL Image to a NumPy array
|
24 |
+
img_array = np.array(img)
|
25 |
+
# Generate a unique filename for the temporary image
|
26 |
+
img_path = f"temp_image_{uuid.uuid4()}.jpg"
|
27 |
+
Image.fromarray((img_array * 255).astype(np.uint8)).save(img_path)
|
28 |
+
# Preprocess the image and make predictions
|
29 |
+
processed_img = preprocess_image(img_array)
|
30 |
+
prediction = model.predict(processed_img)
|
31 |
+
class_idx = np.argmax(prediction)
|
32 |
+
# Update this with your class labels
|
33 |
+
class_labels = ['biotite', 'granite', 'olivine', 'plagioclase', 'staurolite']
|
34 |
+
print(f"Prediction scores: {prediction[0]}")
|
35 |
+
print(f"Predicted class: {class_labels[class_idx]}")
|
36 |
+
# Convert prediction scores to percentages
|
37 |
+
prediction_scores = [f"{score*100:.2f}%" for score in prediction[0]]
|
38 |
+
# Create a formatted string to display the class labels and prediction scores
|
39 |
+
class_info = "\n".join([f"{label}: {score}" for label, score in zip(class_labels, prediction_scores)])
|
40 |
+
return class_labels[class_idx], float(prediction[0][class_idx]), class_info
|
41 |
+
|
42 |
+
# Load CSS and JavaScript files
|
43 |
+
|
44 |
+
|
45 |
+
# Create a Gradio interface
|
46 |
+
with gr.Blocks() as iface:
|
47 |
+
with gr.Row():
|
48 |
+
image_input = gr.Image(label="Input Image")
|
49 |
+
output_text = gr.Textbox(label="Predicted Class")
|
50 |
+
output_number = gr.Number(label="Prediction Score")
|
51 |
+
with gr.Row():
|
52 |
+
class_output = gr.Textbox(label="Class Predictions")
|
53 |
+
run_button = gr.Button("Predict")
|
54 |
+
|
55 |
+
run_button.click(predict_mineral, inputs=image_input, outputs=[output_text, output_number, class_output])
|
56 |
+
|
57 |
+
iface.launch(share=True)
|