Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,54 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
confidence = result[0]["score"]
|
14 |
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import torch
|
3 |
+
import torchvision.transforms as transforms
|
4 |
+
from torchvision.models import resnet50
|
5 |
+
from PIL import Image
|
6 |
+
import requests
|
7 |
+
from io import BytesIO
|
8 |
|
9 |
+
# Load the pre-trained ResNet-50 model
|
10 |
+
model = resnet50(pretrained=True)
|
11 |
+
model.eval()
|
12 |
|
13 |
+
# Define the image transforms
|
14 |
+
transform = transforms.Compose([
|
15 |
+
transforms.Resize(256),
|
16 |
+
transforms.CenterCrop(224),
|
17 |
+
transforms.ToTensor(),
|
18 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
19 |
+
])
|
20 |
|
21 |
+
# Define the label map for ImageNet classes
|
22 |
+
LABELS_URL = "https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json"
|
23 |
+
response = requests.get(LABELS_URL)
|
24 |
+
labels = response.json()
|
|
|
25 |
|
26 |
+
# Streamlit UI
|
27 |
+
st.title("Image Classification with Pre-trained ResNet-50")
|
28 |
+
st.write("Upload an image and the model will predict the class of the object in the image.")
|
29 |
+
|
30 |
+
# File uploader
|
31 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
32 |
+
|
33 |
+
if uploaded_file is not None:
|
34 |
+
# Open the image file
|
35 |
+
image = Image.open(uploaded_file)
|
36 |
+
|
37 |
+
# Display the image
|
38 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
39 |
+
st.write("")
|
40 |
+
st.write("Classifying...")
|
41 |
+
|
42 |
+
# Preprocess the image
|
43 |
+
image = transform(image).unsqueeze(0)
|
44 |
+
|
45 |
+
# Predict the class
|
46 |
+
with torch.no_grad():
|
47 |
+
outputs = model(image)
|
48 |
+
|
49 |
+
# Get the predicted class
|
50 |
+
_, predicted = torch.max(outputs, 1)
|
51 |
+
predicted_class = labels[predicted.item()]
|
52 |
+
|
53 |
+
# Display the result
|
54 |
+
st.write(f"Predicted Class: {predicted_class}")
|