Update app.py
Browse files
app.py
CHANGED
@@ -4,78 +4,90 @@ from PIL import Image
|
|
4 |
import requests
|
5 |
from io import BytesIO
|
6 |
|
7 |
-
# This is a placeholder for your image classification function
|
8 |
-
def classify_image1(image):
|
9 |
-
pipe1 = pipeline("image-classification", "SolubleFish/swin_transformer-finetuned-eurosat")
|
10 |
-
return pipe1(image)
|
11 |
-
def classify_image2(image):
|
12 |
-
pipe2 = pipeline("image-classification", "SolubleFish/image_classification_convnext")
|
13 |
-
return pipe2(image)
|
14 |
-
def classify_image3(image):
|
15 |
-
pipe3 = pipeline("image-classification", "SolubleFish/image_classification_vit")
|
16 |
-
return pipe3(image)
|
17 |
|
18 |
# Title
|
19 |
st.title("Image Classification Web App")
|
|
|
20 |
|
21 |
# Intro
|
22 |
-
st.
|
23 |
|
24 |
# Image input via URL
|
25 |
-
url = st.text_input("Image URL")
|
26 |
if url:
|
27 |
try:
|
28 |
response = requests.get(url)
|
29 |
image = Image.open(BytesIO(response.content))
|
30 |
-
st.image(image, caption='Uploaded Image', use_column_width=True)
|
31 |
except Exception as e:
|
32 |
-
st.
|
33 |
|
34 |
-
# Image input via file uploader
|
35 |
-
uploaded_file = st.file_uploader("Or upload an image", type=["jpg", "png"])
|
36 |
if uploaded_file is not None:
|
37 |
image = Image.open(uploaded_file)
|
38 |
st.image(image, caption='Uploaded Image', use_column_width=True)
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
# Create three columns
|
42 |
col1, col2, col3 = st.columns(3)
|
43 |
|
44 |
# Classification button for classify_image1
|
45 |
-
if col1.button("Classify Image by
|
46 |
if url or uploaded_file:
|
47 |
results = classify_image1(image)
|
48 |
if results:
|
49 |
# Use markdown to present the results
|
50 |
for result in results:
|
51 |
-
col1.markdown(f"
|
|
|
52 |
else:
|
53 |
-
col1.
|
54 |
else:
|
55 |
-
col1.
|
56 |
|
57 |
# Classification button for classify_image2
|
58 |
-
if col2.button("Classify Image by
|
59 |
if url or uploaded_file:
|
60 |
results = classify_image2(image)
|
61 |
if results:
|
62 |
# Use markdown to present the results
|
63 |
for result in results:
|
64 |
-
col2.markdown(f"
|
|
|
65 |
else:
|
66 |
-
col2.
|
67 |
else:
|
68 |
-
col2.
|
69 |
|
70 |
# Classification button for classify_image3
|
71 |
-
if col3.button("Classify Image by
|
72 |
if url or uploaded_file:
|
73 |
results = classify_image3(image)
|
74 |
if results:
|
75 |
# Use markdown to present the results
|
76 |
for result in results:
|
77 |
-
col3.markdown(f"
|
|
|
78 |
else:
|
79 |
-
col3.
|
80 |
else:
|
81 |
-
col3.
|
|
|
4 |
import requests
|
5 |
from io import BytesIO
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# Title
|
9 |
st.title("Image Classification Web App")
|
10 |
+
st.markdown("This app uses Hugging Face's 'transformers' library to classify images using pre-trained models. The app uses three different models for image classification: swin, convnext and vit. Please select a model to classify the image you put on the left sidebar.")
|
11 |
|
12 |
# Intro
|
13 |
+
st.sidebar.markdown("**Please provide a Satellite image for classification**")
|
14 |
|
15 |
# Image input via URL
|
16 |
+
url = st.sidebar.text_input("Image URL")
|
17 |
if url:
|
18 |
try:
|
19 |
response = requests.get(url)
|
20 |
image = Image.open(BytesIO(response.content))
|
21 |
+
st.sidebar.image(image, caption='Uploaded Image', use_column_width=True)
|
22 |
except Exception as e:
|
23 |
+
st.sidebar.error("Invalid URL. Please enter a valid URL for an image.")
|
24 |
|
25 |
+
# Image input via file uploader on the sidebar (but display image on the main page)
|
26 |
+
uploaded_file = st.sidebar.file_uploader("Or upload an image", type=["jpg", "png"])
|
27 |
if uploaded_file is not None:
|
28 |
image = Image.open(uploaded_file)
|
29 |
st.image(image, caption='Uploaded Image', use_column_width=True)
|
30 |
|
31 |
+
# Documentation about the 3 models
|
32 |
+
st.sidebar.markdown("## Find more information about the model architecture at the link below : ")
|
33 |
+
st.sidebar.markdown("*Vision Transformer (ViT)* https://huggingface.co/docs/transformers/main/en/model_doc/vit")
|
34 |
+
st.sidebar.markdown("*ConvNext Transformer* https://huggingface.co/docs/transformers/main/en/model_doc/convnext")
|
35 |
+
st.sidebar.markdown("*Swin Transformer* https://huggingface.co/docs/transformers/main/en/model_doc/swin")
|
36 |
+
|
37 |
+
# Image classification function
|
38 |
+
|
39 |
+
def classify_image1(image):
|
40 |
+
pipe1 = pipeline("image-classification", "SolubleFish/swin_transformer-finetuned-eurosat", token=access_token)
|
41 |
+
return pipe1(image)
|
42 |
+
def classify_image2(image):
|
43 |
+
pipe2 = pipeline("image-classification", "SolubleFish/image_classification_convnext", token=access_token)
|
44 |
+
return pipe2(image)
|
45 |
+
def classify_image3(image):
|
46 |
+
pipe3 = pipeline("image-classification", "SolubleFish/image_classification_vit", token=access_token)
|
47 |
+
return pipe3(image)
|
48 |
+
|
49 |
|
50 |
# Create three columns
|
51 |
col1, col2, col3 = st.columns(3)
|
52 |
|
53 |
# Classification button for classify_image1
|
54 |
+
if col1.button("Classify Image by Swin"):
|
55 |
if url or uploaded_file:
|
56 |
results = classify_image1(image)
|
57 |
if results:
|
58 |
# Use markdown to present the results
|
59 |
for result in results:
|
60 |
+
col1.markdown(f"Class name: **{result['label']}** \n\n Confidence: **{str(format(result['score']*100, '.2f'))}**"+"%")
|
61 |
+
col1.success("Classification completed.")
|
62 |
else:
|
63 |
+
col1.error("No results found.")
|
64 |
else:
|
65 |
+
col1.error("Please provide an image for classification.")
|
66 |
|
67 |
# Classification button for classify_image2
|
68 |
+
if col2.button("Classify Image by ConvNext"):
|
69 |
if url or uploaded_file:
|
70 |
results = classify_image2(image)
|
71 |
if results:
|
72 |
# Use markdown to present the results
|
73 |
for result in results:
|
74 |
+
col2.markdown(f"Class name: **{result['label']}** \n\n Confidence: **{str(format(result['score']*100, '.2f'))}**"+"%")
|
75 |
+
col2.success("Classification completed.")
|
76 |
else:
|
77 |
+
col2.error("No results found.")
|
78 |
else:
|
79 |
+
col2.error("Please provide an image for classification.")
|
80 |
|
81 |
# Classification button for classify_image3
|
82 |
+
if col3.button("Classify Image by ViT"):
|
83 |
if url or uploaded_file:
|
84 |
results = classify_image3(image)
|
85 |
if results:
|
86 |
# Use markdown to present the results
|
87 |
for result in results:
|
88 |
+
col3.markdown(f"Class name: **{result['label']}** \n\n Confidence: **{str(format(result['score']*100, '.2f'))}**"+"%")
|
89 |
+
col3.success("Classification completed.")
|
90 |
else:
|
91 |
+
col3.error("No results found.")
|
92 |
else:
|
93 |
+
col3.error("Please provide an image for classification.")
|