Spaces:
Runtime error
Runtime error
Update app.py
Browse files2 unmodified lines3456789101112131415161718192021222324252627282930313233343536373839404142434445464748494 unmodified lines545556575859603 unmodified lines646566676869707172731 unmodified line757677782 unmodified linesimport gradio as grimport cv2onnx_efficientnet_path = './Models_onnx/efficientnet.onnx'onnx_inception_path = './Models_onnx/inceptionv3.onnx'onnx_vggnet_path = './Models_onnx/vgg_16.onnx'onnx_resnet_path = './Models_onnx/resnet50.onnx'# Uncomment or remove the following line based on your needsonnx_inception_model = ort.InferenceSession(onnx_inception_path)onnx_vggnet_model = ort.InferenceSession(onnx_vggnet_path)onnx_resnet_model = ort.InferenceSession(onnx_resnet_path)onnx_efficientnet_model = ort.InferenceSession(onnx_efficientnet_path)class_names = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"]def classify_image(img, model_choice):def preprocess_input(img, model):# Placeholder for preprocessing steps# Replace with actual preprocessing steps based on the modelprocessed_img = img / 255.0return processed_img# Read the image using cv2.imreadimg = cv2.imdecode(np.frombuffer(img.read(), np.uint8), -1)# Keep the input size as (150, 150) since your model expects that sizeinput_size = (150, 150)img_resized = cv2.resize(img, input_size)# Preprocess the input imageimg_preprocessed = preprocess_input(img_resized, model_choice)# Run inference based on the selected modelmodel = {"ResNet": onnx_resnet_model,"EfficientNet": onnx_efficientnet_model,"VGGNet": onnx_vggnet_model,# "InceptionV3": onnx_inception_model # Uncomment or remove based on your needs}[model_choice]input_name = model.get_inputs()[0].nametry:predictions = model.run(None, {input_name: img_preprocessed})class_idx = np.argmax(predictions)class_name = class_names[class_idx]4 unmodified linesresult = {"Original Image": img.tolist(), # Convert to list for compatibility with Gradio"Resized Image": img_resized.tolist(),f"Predicted class using {model_choice}": class_name,"Top 3 Predictions": list(zip(top_classes, top_probs)),"Confidence Scores": predictions[0].tolist()3 unmodified linesreturn resultimage_input = gr.Image()model_dropdown = gr.Dropdown(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model")# Create a Gradio interface with image input and a dropdown for model selectioniface = gr.Interface(fn=classify_image,inputs=[image_input, model_dropdown],1 unmodified linelive=True)iface.launch(share=True)2 unmodified lines34567891011121314151617181920212223242526272829303132333435363738394041424344454 unmodified lines5051525354553 unmodified lines5960616263646566671 unmodified line69707172732 unmodified linesimport gradio as grimport cv2# Load ONNX modelsonnx_inception_model = ort.InferenceSession('./Models_onnx/inceptionv3.onnx')onnx_vggnet_model = ort.InferenceSession('./Models_onnx/vgg_16.onnx')onnx_resnet_model = ort.InferenceSession('./Models_onnx/resnet50.onnx')onnx_efficientnet_model = ort.InferenceSession('./Models_onnx/efficientnet.onnx')# Class names for classificationclass_names = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"]def preprocess_input(img, target_size=(150, 150)):"""Preprocess input image for model inference."""img = cv2.imread(img)img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)img = cv2.resize(img, target_size)img = img.astype(np.float32) / 255.0img = np.expand_dims(img, axis=0)return imgdef classify_image(img, model_choice):"""Classify the input image using the selected ONNX model."""try:# Get the selected modelmodel = {"ResNet": onnx_resnet_model,"EfficientNet": onnx_efficientnet_model,"VGGNet": onnx_vggnet_model,"InceptionV3": onnx_inception_model}[model_choice]# Preprocess the input imageimg_preprocessed = preprocess_input(img)# Run inferenceinput_name = model.get_inputs()[0].namepredictions = model.run(None, {input_name: img_preprocessed})# Get the predicted classclass_idx = np.argmax(predictions)class_name = class_names[class_idx]4 unmodified linesresult = {"Original Image": img.tolist(), # Convert to list for compatibility with Gradiof"Predicted class using {model_choice}": class_name,"Top 3 Predictions": list(zip(top_classes, top_probs)),"Confidence Scores": predictions[0].tolist()3 unmodified linesreturn result# Define Gradio inputs and interfaceimage_input = gr.Image()model_dropdown = gr.Dropdown(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model")iface = gr.Interface(fn=classify_image,inputs=[image_input, model_dropdown],1 unmodified linelive=True)# Launch the Gradio interfaceiface.launch(share=True)
app.py+32-37