|
import torch |
|
from transformers import ViTForImageClassification, ViTFeatureExtractor |
|
from PIL import Image |
|
import json |
|
|
|
|
|
with open('metadata.json') as f: |
|
metadata = json.load(f) |
|
|
|
def predict(image_path: str): |
|
|
|
model = ViTForImageClassification.from_pretrained("yigagilbert/image-quality-model") |
|
feature_extractor = ViTFeatureExtractor.from_pretrained("yigagilbert/image-quality-model") |
|
|
|
|
|
image = Image.open(image_path) |
|
inputs = feature_extractor(images=image, return_tensors="pt") |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
predicted_value = outputs.logits.squeeze().item() |
|
|
|
|
|
max_value = metadata.get('max_value', 1.0) |
|
predicted_value_scaled = predicted_value * max_value |
|
|
|
return predicted_value_scaled |
|
|