File size: 1,837 Bytes
a2cce3c 683c547 a2cce3c 7f11cbb a2cce3c 683c547 a2cce3c a05f916 9811d4c 683c547 a2cce3c 0e7dc80 76f07f2 683c547 1f3f0b0 683c547 76f07f2 683c547 f2009fd 683c547 76f07f2 f2009fd 683c547 76f07f2 0e7dc80 ea38376 f2009fd |
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 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import streamlit as st
import torch
from torchvision import transforms
from huggingface_hub import HfFileSystem
from PIL import Image
# Authenticate and download the custom model from Hugging Face Spaces
fs = HfFileSystem()
model_path = 'dhhd255/efficient_net_parkinsons/best_model.pth'
with fs.open(model_path, 'rb') as f:
model_content = f.read()
# Save the model file to disk
with open('best_model.pth', 'wb') as f:
f.write(model_content)
# Load your custom model onto the CPU
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best_model.pth')
model.eval()
# Define a function that takes an image as input and uses the model for inference
def image_classifier(image):
# Preprocess the input 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)
# Use your custom model for inference
with torch.no_grad():
outputs = model(image)
_, predicted = torch.max(outputs.data, 1)
# Map the index to a class label
labels = ['Healthy', 'Parkinson']
predicted_label = labels[predicted.item()]
# Return the result
return outputs[0].numpy(), predicted_label
# Create a Streamlit app with an image upload input
uploaded_file = st.file_uploader('Upload an image')
if uploaded_file is not None:
# Convert the UploadedFile object to a NumPy array
image = Image.open(uploaded_file)
image = np.array(image)
# Use the image for inference
predictions, predicted_label = image_classifier(image)
# Display the result
st.write(f'Predictions: {predictions}')
st.write(f'Predicted label: {predicted_label}')
|