Spaces:
Runtime error
Runtime error
Update app.py
Browse files2 unmodified lines34567891 unmodified line11121313141516171 unmodified line1920212122232425222324252627282930313233343536373839404127282942433132444546474849505152533435363738394041424344455455474849505657585253545556575859605960616262636465661 unmodified line686970707172727374732 unmodified linesimport cv2import gradio as grimport onnxruntime as ortimport tensorflow as tf
onnx_efficientnet_path = './Models_onnx/efficientnet.onnx'# onnx_inception_path = './Models_onnx/inceptionv3.onnx'1 unmodified lineonnx_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_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)1 unmodified lineclass_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 model processed_img = img / 255.0 return processed_img if model_choice == "ResNet": img = tf.image.resize(img, (150, 150)) # ResNet50 input size img = image.img_to_array(img) img = img.copy() # Make a copy of the image array img = resnet_preprocess_input(img) elif model_choice == "EfficientNet": img = tf.image.resize(img, (150, 150)) img = image.img_to_array(img) img = preprocess_input(img) elif model_choice == "VGGNet": img = tf.image.resize(img, (150, 150)) img = image.img_to_array(img) img = preprocess_input(img) elif model_choice == "InceptionV3": img = tf.image.resize(img, (150, 150)) img = image.img_to_array(img) img = preprocess_input(img) else: return "Invalid model choice"
# Keep the input size as (150, 150) since your model expects that size input_size = (150, 150) img_resized = cv2.resize(img, input_size) img = tf.expand_dims(img, 0) # Add batch dimension
# Preprocess the input image img_preprocessed = preprocess_input(img_resized, model_choice) # Make predictions based on the selected model if model_choice == "ResNet": predictions = onnx_resnet_model.predict(img) elif model_choice == "EfficientNet": predictions = onnx_efficientnet_model.predict(img) elif model_choice == "VGGNet": predictions = onnx_vggnet_model.predict(img) elif model_choice == "InceptionV3": predictions = onnx_inception_model.predict(img)
# Run inference based on the selected model model = { "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].name try: predictions = model.run(None, {input_name: img_preprocessed}) class_idx = np.argmax(predictions) class_name = class_names[class_idx] class_idx = tf.argmax(predictions[0])
# Get the top 3 predictions top_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] # Map class index to class name class_name = class_names[class_idx]
result = { "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() } except Exception as e: result = {"Error": str(e)} # Return the predicted class name as a string return f"Predicted class using {model_choice}: {class_name}"
return result
image_input = gr.Image()
1 unmodified lineiface = gr.Interface( fn=classify_image, inputs=[image_input, gr.Dropdown(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model")], outputs="json", outputs=["json"], live=True)
iface.launch(share=True)).launch(share=True)app.py+40-41