Spaces:
Runtime error
Runtime error
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667import numpy as npimport osimport cv2import gradio as grimport onnxruntime as ortonnx_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 needs# onnx_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# 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]predictions = model.run(None, {"input": img_preprocessed})class_idx = np.argmax(predictions)class_name = class_names[class_idx]# Get the top 3 predictionstop_predictions = sorted(enumerate(predictions[0]), key=lambda x: x[1], reverse=True)[:3]top_classes = [class_names[idx] for idx, _ in top_predictions]top_probs = [round(prob * 100, 2) for _, prob in top_predictions]return {"Original Image": img,"Resized Image": img_resized,"Predicted class using {}: {}".format(model_choice, class_name),"Top 3 Predictions": list(zip(top_classes, top_probs)),"Confidence Scores": predictions[0]}image_input = gr.Image()# Create a Gradio interface with a dropdown for model selectioniface = gr.Interface(fn=classify_image,inputs=[image_input, gr.Dropdown(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model")],outputs=["image", "image", "text", "text", "text"],live=True).launch(debug=True)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566# -*- coding: utf-8 -*-"""gradio interface.ipynbAutomatically generated by Colaboratory.Original file is located athttps://colab.research.google.com/drive/1AxnKS2DJb_EJ2GlZuu7YZekaD0PXQN8O"""import gradio as grfrom PIL import Imageimport torchfrom torchvision import transformsfrom transformers import AutoModelForImageClassification, AutoTokenizer# Load models from Hugging Face model hubefficientnet_model = AutoModelForImageClassification.from_pretrained("trs/EfficientNet")resnet_model = AutoModelForImageClassification.from_pretrained("trs/ResNet")vggnet_model = AutoModelForImageClassification.from_pretrained("trs/VGGNet")inceptionv3_model = AutoModelForImageClassification.from_pretrained("trs/InceptionV3")# Define class names corresponding to your datasetclass_names = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"] # Replace with your actual class names# Define the classify_image functiondef classify_image(img, model_choice):# Resize and preprocess the input imagepreprocess = transforms.Compose([transforms.Resize((150, 150)),transforms.ToTensor(),])img = preprocess(Image.fromarray(img))# Add batch dimensionimg = img.unsqueeze(0)# Make predictions based on the selected modelif model_choice == "ResNet":predictions = resnet_model(img)elif model_choice == "EfficientNet":predictions = efficientnet_model(img)elif model_choice == "VGGNet":predictions = vggnet_model(img)elif model_choice == "InceptionV3":predictions = inceptionv3_model(img)else:return "Invalid model choice"class_idx = torch.argmax(predictions[0])# Map class index to class nameclass_name = class_names[class_idx]# Return the predicted class name as a stringreturn f"Predicted class using {model_choice}: {class_name}"# Create a Gradio interface with a dropdown for model selectioniface = gr.Interface(fn=classify_image,inputs=["image", gr.Image()],outputs="text",live=True)# Launch the Gradio interfaceiface.launch(share=True)
app.py+50-51