Spaces:
Runtime error
Runtime error
Update app.py
Browse files17 unmodified lines1819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656617 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 selection17 unmodified lines181920212223242526272829303132333435363738394041424344454647484917 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=(224, 224, 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 selection
app.py+20-37