Hachem's picture
Update app.py
555549a verified
import gradio as gr
import cv2
import requests
import json
from PIL import Image
import numpy as np
import os
import gradio as gr
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
import numpy as np
model = load_model('artikel.h5')
def preprocess_image(img):
img = np.array(img) # Ensure img is a numpy array
img = cv2.resize(img, (224, 224)) # Resize using cv2 which is already imported
img = np.expand_dims(img, axis=0) # Expand dims to add the batch size
return preprocess_input(img) # Use the MobileNet-specific preprocessing
def predict_image(img):
processed_image = preprocess_image(img)
prediction = model.predict(processed_image)
predicted_class_index = np.argmax(prediction, axis=1)[0]
return predicted_class_index
def process_image(image):
predicted_class_index=predict_image(image)
with open('artikel.json', 'r') as file:
data = json.load(file)
predicted_class_index=str(data.get(str(predicted_class_index),"-1"))
return(predicted_class_index)
interface = gr.Interface(
fn=process_image,
inputs=[gr.Image(type="numpy")],
outputs=[gr.Textbox(label="Prediction")]
)
interface.launch(share=True)