Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2 # OpenCV library for image processing
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from tensorflow.keras.applications.resnet50 import preprocess_input
|
| 5 |
+
from tensorflow.keras.preprocessing import image
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
model = tf.keras.models.load_model('cnn_resnet50_model.h5')
|
| 9 |
+
|
| 10 |
+
def classify_image(img):
|
| 11 |
+
try:
|
| 12 |
+
# Resize the input image to match the expected input shape
|
| 13 |
+
img = cv2.resize(img, (128, 128))
|
| 14 |
+
# Convert the resized image to the appropriate format
|
| 15 |
+
img = image.img_to_array(img)
|
| 16 |
+
img = np.expand_dims(img, axis=0)
|
| 17 |
+
img = preprocess_input(img)
|
| 18 |
+
# Perform model inference
|
| 19 |
+
prediction = model.predict(img)
|
| 20 |
+
return {'class': np.argmax(prediction), 'confidence': np.max(prediction)}
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"Error: {e}")
|
| 23 |
+
return {'error': str(e)}
|
| 24 |
+
|
| 25 |
+
# Définir l'interface
|
| 26 |
+
iface = gr.Interface(fn=classify_image, inputs="image", outputs="json").launch(share="True")
|
| 27 |
+
|
| 28 |
+
|