File size: 980 Bytes
0eb8bd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7e6513
0eb8bd8
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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")