|
import streamlit as st |
|
import torch |
|
from torchvision import transforms |
|
from huggingface_hub import HfFileSystem |
|
from PIL import Image |
|
|
|
|
|
fs = HfFileSystem() |
|
model_path = 'dhhd255/efficient_net_parkinsons/best_model.pth' |
|
with fs.open(model_path, 'rb') as f: |
|
model_content = f.read() |
|
|
|
|
|
with open('best_model.pth', 'wb') as f: |
|
f.write(model_content) |
|
|
|
|
|
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best_model.pth') |
|
model.eval() |
|
|
|
|
|
def image_classifier(image): |
|
|
|
data_transform = transforms.Compose([ |
|
transforms.Lambda(lambda x: x.convert('RGB')), |
|
transforms.Resize((224, 224)), |
|
transforms.ToTensor() |
|
]) |
|
image = Image.fromarray(image) |
|
image = data_transform(image) |
|
image = image.unsqueeze(0) |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = model(image) |
|
_, predicted = torch.max(outputs.data, 1) |
|
|
|
|
|
labels = ['Healthy', 'Parkinson'] |
|
predicted_label = labels[predicted.item()] |
|
|
|
|
|
return outputs[0].numpy(), predicted_label |
|
|
|
|
|
uploaded_file = st.file_uploader('Upload an image') |
|
if uploaded_file is not None: |
|
|
|
image = Image.open(uploaded_file) |
|
image = np.array(image) |
|
|
|
|
|
predictions, predicted_label = image_classifier(image) |
|
|
|
|
|
st.write(f'Predictions: {predictions}') |
|
st.write(f'Predicted label: {predicted_label}') |
|
|