File size: 1,611 Bytes
9da5b91 758c978 59a3399 70a751e 758c978 70a751e 59a3399 a135d91 c5f5082 a135d91 c5f5082 452a079 59a3399 452a079 59a3399 01d090f 4700b60 70a751e 406e1b2 a135d91 63dbfda 758c978 70a751e 758c978 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import gradio as gr
from transformers import ViTFeatureExtractor, ViTForImageClassification
from hugsvision.inference.VisionClassifierInference import VisionClassifierInference
from PIL import Image, ImageDraw, ImageFont
# Load the pre-trained ViT model
path = "mrm8488/vit-base-patch16-224_finetuned-kvasirv2-colonoscopy"
classifier = VisionClassifierInference(
feature_extractor=ViTFeatureExtractor.from_pretrained(path),
model=ViTForImageClassification.from_pretrained(path),
)
def classify_image(image_file):
"""Classify an image using a pre-trained ViT model."""
label = classifier.predict(img_path=image_file.name)
# Add a confidence score to the output
confidence = classifier.predict_proba(img_path=image_file.name)[0][label]
# Get the PIL Image object for the uploaded image
image = Image.open(image_file)
# Draw the predicted label on the image
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("arial.ttf", 20)
draw.text((10, 10), f"Predicted class: {label} (confidence: {confidence:.2f})", font=font, fill=(255, 255, 255))
# Save the modified image to a BytesIO object
output_image = BytesIO()
image.save(output_image, format="JPEG")
output_image.seek(0)
return output_image, f"Predicted class: {label} (confidence: {confidence:.2f})"
iface = gr.Interface(
fn=classify_image,
inputs=gr.inputs.Image(type="filepath", label="Upload an image"),
outputs=[gr.outputs.Image(type="numpy"), "text"],
title="Image Classifier",
description="Classify images using a pre-trained ViT model",
)
iface.launch()
|