|
import gradio as gr |
|
from transformers import AutoModelForImageClassification, AutoFeatureExtractor |
|
import torch |
|
from PIL import Image |
|
import numpy as np |
|
|
|
|
|
model_name = "microsoft/beit-base-patch16-224" |
|
model = AutoModelForImageClassification.from_pretrained(model_name) |
|
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name) |
|
|
|
|
|
imagenet_labels = [ |
|
"tench", "goldfish", "great white shark", "tiger shark", "hammerhead shark", |
|
"electric ray", "stingray", "cock", "hen", "ostrich", |
|
"brambling", "goldfinch", "house finch", "junco", "indigo bunting", |
|
|
|
"sports car", "convertible", "minivan", "pickup", "SUV" |
|
] |
|
|
|
|
|
def classify_image(image): |
|
|
|
if isinstance(image, Image.Image): |
|
image = np.array(image) |
|
|
|
|
|
inputs = feature_extractor(images=image, return_tensors="pt") |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
logits = outputs.logits |
|
predicted_class_idx = logits.argmax(-1).item() |
|
|
|
|
|
if predicted_class_idx < len(imagenet_labels): |
|
class_name = imagenet_labels[predicted_class_idx] |
|
else: |
|
class_name = f"Unknown Class (ID: {predicted_class_idx})" |
|
|
|
return f"Predicted class: {class_name} (ID: {predicted_class_idx})" |
|
|
|
|
|
demo = gr.Interface(fn=classify_image, inputs="image", outputs="text", title="Image Classification Demo") |
|
demo.launch() |
|
|