Spaces:
Runtime error
Runtime error
Download app.py from trs/Image_Classification_with_pre-traind_Models: direct link, hf CLI and curl.
- Browser
- Download file 2.17 kB
-
https://huggingface.co/spaces/trs/Image_Classification_with_pre-traind_Models/resolve/7de6278166e49c52e79df31fa3dd79aa35b9bcf3/app.py
- Command line
-
hf download hf://spaces/trs/Image_Classification_with_pre-traind_Models@7de6278166e49c52e79df31fa3dd79aa35b9bcf3/app.py
-
curl -L -o app.py https://huggingface.co/spaces/trs/Image_Classification_with_pre-traind_Models/resolve/7de6278166e49c52e79df31fa3dd79aa35b9bcf3/app.py
2.17 kB
| import numpy as np | |
| import os | |
| import cv2 | |
| import gradio as gr | |
| import onnxruntime as ort | |
| onnx_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): | |
| # Resize the input image based on the selected model's expected input shape | |
| input_size = (150, 150) # Assuming a common input size for all models | |
| img = tf.image.resize(img, input_size) | |
| img = image.img_to_array(img) | |
| # Preprocess input based on the selected model | |
| tokenizer = AutoTokenizer.from_pretrained(model_choice) | |
| inputs = tokenizer(images=img, return_tensors="tf", padding=True) | |
| # Make predictions based on the selected model | |
| model = AutoModelForImageClassification.from_pretrained(model_choice) | |
| predictions = model(**inputs).logits | |
| class_idx = tf.argmax(predictions[0]) | |
| # Map class index to class name | |
| class_name = tokenizer.convert_ids_to_tokens(class_idx.numpy()) | |
| # Return the predicted class name as a string | |
| return f"Predicted class using {model_choice}: {class_name}" | |
| # Example usage | |
| img = tf.random.normal(shape=(224, 224, 3)) # Replace with your image data | |
| model_choice = "facebook/vit-base-patch16-224-in21k-finetuned-image-coco" # Replace with your desired model | |
| result = classify_image(img, model_choice) | |
| print(result) | |
| image_input = gr.Image() | |
| # Create a Gradio interface with a dropdown for model selection | |
| iface = gr.Interface( | |
| fn=classify_image, | |
| inputs=[image_input, gr.Dropdown(["ResNet", "EfficientNet", "VGGNet", "InceptionV3"], label="Select Model")], | |
| outputs=["json"], | |
| live=True | |
| ).launch(debug=True) | |