Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import cv2 # OpenCV library for image processing | |
| import tensorflow as tf | |
| from tensorflow.keras.applications.resnet50 import preprocess_input | |
| from tensorflow.keras.preprocessing import image | |
| import numpy as np | |
| model = tf.keras.models.load_model('cnn_resnet50_model.h5') | |
| def classify_image(img): | |
| try: | |
| # Resize the input image to match the expected input shape | |
| img = cv2.resize(img, (128, 128)) | |
| # Convert the resized image to the appropriate format | |
| img = image.img_to_array(img) | |
| img = np.expand_dims(img, axis=0) | |
| img = preprocess_input(img) | |
| # Perform model inference | |
| prediction = model.predict(img) | |
| return {'class': np.argmax(prediction), 'confidence': np.max(prediction)} | |
| except Exception as e: | |
| print(f"Error: {e}") | |
| return {'error': str(e)} | |
| # Définir l'interface | |
| iface = gr.Interface(fn=classify_image, inputs="image", outputs="json").launch(share="True") | |