Spaces:
Runtime error
Runtime error
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546import onnxruntime as ortimport gradio as grimport numpy as npimport cv2# Define model pathsmodel_paths = {"ResNet": "./Models_onnx/resnet50.onnx","EfficientNet": "./Models_onnx/efficientnet.onnx","VGGNet": "./Models_onnx/vgg_16.onnx",# Add more models as needed}class_names = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"]def preprocess_input(img_path):img = cv2.imread(img_path)img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)img = cv2.resize(img, (150, 150))img = img.astype(np.float32) / 255.0img = np.expand_dims(img, axis=0)return imgdef classify_image(img_path, model_choice):# Run inference based on the selected modelonnx_model = ort.InferenceSession(model_paths[model_choice])input_name = onnx_model.get_inputs()[0].nameimg_preprocessed = preprocess_input(img_path)predictions = onnx_model.run(None, {input_name: img_preprocessed})class_idx = np.argmax(predictions)class_name = class_names[class_idx]return class_name# Create Gradio interfaceiface = gr.Interface(fn=classify_image,inputs=[gr.Image(), gr.Dropdown(list(model_paths.keys()), label="Select Model")],outputs="text",live=True)# Launch the interfaceiface.launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162import onnxruntime as ortimport numpy as npimport 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 = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"]def preprocess_input(img):img = cv2.imread(img)img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)img = cv2.resize(img, (150, 150))img = img.astype(np.float32) / 255.0img = np.expand_dims(img, axis=0)return imgdef classify_image(img, model_choice):# Run inference based on the selected modelmodel = {"InceptionV3": onnx_inception_model,"VGGNet": onnx_vggnet_model,"ResNet": onnx_resnet_model,"EfficientNet": onnx_efficientnet_model,}.get(model_choice)if model is None:return {"Error": f"Model {model_choice} not found"}img_preprocessed = preprocess_input(img)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]confidence_score = round(predictions[0, class_idx] * 100, 2)result = {"Predicted class": class_name,"Confidence Score": confidence_score,}except Exception as e:result = {"Error": str(e)}return resultimage_input = gr.Image()# Create a Gradio interface with a dropdown for model selectioniface = gr.Interface(fn=classify_image,inputs=[image_input, gr.Dropdown(["InceptionV3", "VGGNet", "ResNet", "EfficientNet"], label="Select Model")],outputs="json",live=True)iface.launch(share=True)
app.py+40-24