Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,46 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
-
from transformers import pipeline
|
| 4 |
import torch
|
| 5 |
-
import
|
|
|
|
| 6 |
|
| 7 |
-
# Check if
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Set up a title for the app
|
| 14 |
st.title("Image Recognition App")
|
| 15 |
|
| 16 |
-
# Load a pre-trained image classification pipeline from Hugging Face
|
| 17 |
-
try:
|
| 18 |
-
classifier = pipeline('image-classification', model='google/efficientnet-b0')
|
| 19 |
-
except RuntimeError as e:
|
| 20 |
-
st.error("Error loading the model: Please ensure PyTorch or TensorFlow is installed.")
|
| 21 |
-
st.error(str(e))
|
| 22 |
-
|
| 23 |
# Upload an image
|
| 24 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
| 25 |
|
| 26 |
if uploaded_file is not None:
|
| 27 |
# Display the uploaded image
|
| 28 |
-
image = Image.open(uploaded_file)
|
| 29 |
st.image(image, caption='Uploaded Image.', use_column_width=True)
|
| 30 |
-
|
| 31 |
-
#
|
| 32 |
st.write("Classifying...")
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
st.write("Predictions:")
|
| 37 |
-
for
|
| 38 |
-
st.write(f"Label: {
|
| 39 |
-
except Exception as e:
|
| 40 |
-
st.error("An error occurred during classification.")
|
| 41 |
-
st.error(str(e))
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
|
|
|
| 3 |
import torch
|
| 4 |
+
import torchvision.transforms as transforms
|
| 5 |
+
from torchvision import models
|
| 6 |
|
| 7 |
+
# Check if CUDA (GPU) is available, otherwise use CPU
|
| 8 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 9 |
+
|
| 10 |
+
# Load the pre-trained model (ResNet-50)
|
| 11 |
+
model = models.resnet50(pretrained=True)
|
| 12 |
+
model = model.to(device)
|
| 13 |
+
model.eval()
|
| 14 |
+
|
| 15 |
+
# Define a function to preprocess the image
|
| 16 |
+
def preprocess_image(image):
|
| 17 |
+
transform = transforms.Compose([
|
| 18 |
+
transforms.Resize((224, 224)),
|
| 19 |
+
transforms.ToTensor(),
|
| 20 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
| 21 |
+
])
|
| 22 |
+
return transform(image).unsqueeze(0).to(device)
|
| 23 |
|
| 24 |
# Set up a title for the app
|
| 25 |
st.title("Image Recognition App")
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# Upload an image
|
| 28 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
| 29 |
|
| 30 |
if uploaded_file is not None:
|
| 31 |
# Display the uploaded image
|
| 32 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 33 |
st.image(image, caption='Uploaded Image.', use_column_width=True)
|
| 34 |
+
|
| 35 |
+
# Preprocess the image and make predictions
|
| 36 |
st.write("Classifying...")
|
| 37 |
+
input_tensor = preprocess_image(image)
|
| 38 |
+
with torch.no_grad():
|
| 39 |
+
output = model(input_tensor)
|
| 40 |
+
probabilities = torch.nn.functional.softmax(output[0], dim=0)
|
| 41 |
+
top5_prob, top5_classes = torch.topk(probabilities, 5)
|
| 42 |
+
|
| 43 |
+
# Display the top 5 predictions
|
| 44 |
st.write("Predictions:")
|
| 45 |
+
for i in range(5):
|
| 46 |
+
st.write(f"Label: {top5_classes[i].item()}, Confidence: {top5_prob[i].item():.2f}")
|
|
|
|
|
|
|
|
|