Spaces:
Runtime error
Runtime error
Update app.py
Browse files17 unmodified lines18192021222324252627282930313233343536373839404142434445464748492 unmodified lines5253545517 unmodified linesclass_names = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"]def classify_image(img, model_choice):# Resize the input image based on the selected model's expected input shapeinput_size = (150, 150) # Assuming a common input size for all modelsimg = tf.image.resize(img, input_size)img = image.img_to_array(img)# Preprocess input based on the selected modeltokenizer = AutoTokenizer.from_pretrained(model_choice)inputs = tokenizer(images=img, return_tensors="tf", padding=True)# Make predictions based on the selected modelmodel = AutoModelForImageClassification.from_pretrained(model_choice)predictions = model(**inputs).logitsclass_idx = tf.argmax(predictions[0])# Map class index to class nameclass_name = tokenizer.convert_ids_to_tokens(class_idx.numpy())# Return the predicted class name as a stringreturn f"Predicted class using {model_choice}: {class_name}"# Example usageimg = tf.random.normal(shape=(150, 150, 3)) # Replace with your image datamodel_choice = "facebook/vit-base-patch16-224-in21k-finetuned-image-coco" # Replace with your desired modelresult = classify_image(img, model_choice)print(result)image_input = gr.Image()# Create a Gradio interface with a dropdown for model selection2 unmodified linesinputs=[image_input, gr.Dropdown(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model")],outputs=["json"],live=True).launch(debug=True)17 unmodified lines181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465662 unmodified lines6970717217 unmodified linesclass_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]try: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]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 resultimage_input = gr.Image()# Create a Gradio interface with a dropdown for model selection2 unmodified linesinputs=[image_input, gr.Dropdown(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model")],outputs=["json"],live=True).launch(share=True)
app.py+38-21