| | import gradio as gr |
| | from transformers import pipeline, AutoFeatureExtractor, AutoModel |
| | from PIL import Image |
| | import torch |
| | import numpy as np |
| | import joblib |
| | from huggingface_hub import hf_hub_download |
| |
|
| | |
| | feature_extractor = AutoFeatureExtractor.from_pretrained("google/vit-base-patch16-224") |
| | model = AutoModel.from_pretrained("google/vit-base-patch16-224") |
| |
|
| | |
| | model_path = hf_hub_download(repo_id="canadianjosieharrison/texture-logistic-model", filename="classifier.joblib") |
| | classifier = joblib.load(model_path) |
| |
|
| | |
| | id2label = {0:"metal", 1:"stone", 2:"wood"} |
| |
|
| | |
| | def classify_image(img: Image.Image): |
| | |
| | inputs = feature_extractor(images=img, return_tensors="pt") |
| | with torch.no_grad(): |
| | outputs = model(**inputs) |
| | embedding = outputs.last_hidden_state[:, 0, :].numpy() |
| |
|
| | |
| | prediction = classifier.predict(embedding) |
| | return f"Predicted Material: {id2label[prediction[0]]}" |
| |
|
| | |
| | interface = gr.Interface( |
| | fn=classify_image, |
| | inputs=gr.Image(type="pil"), |
| | outputs="text", |
| | title="Material texture image classifier", |
| | description="Upload a detail texture image of a material and the model will predict whether it's metal, stone, or wood.", |
| | examples = ["metal.PNG", "stone.PNG", "wood.PNG"], |
| | ) |
| |
|
| | if __name__ == "__main__": |
| | interface.launch(share=True) |