Spaces:
Sleeping
Sleeping
darkmortal
commited on
Commit
•
c7995ca
1
Parent(s):
9283957
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,36 @@
|
|
1 |
-
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
from PIL import Image
|
|
|
|
|
4 |
|
5 |
-
|
6 |
|
7 |
-
st.title("
|
8 |
|
9 |
-
file_name = st.file_uploader("Upload
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
if file_name is not None:
|
12 |
col1, col2 = st.columns(2)
|
13 |
|
14 |
image = Image.open(file_name)
|
15 |
col1.image(image, use_column_width=True)
|
16 |
-
predictions = pipeline(image)
|
17 |
|
|
|
|
|
|
|
|
|
18 |
col2.header("Probabilities")
|
19 |
-
for
|
20 |
-
col2.subheader(f"{
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from transformers import pipeline
|
2 |
from PIL import Image
|
3 |
+
import requests
|
4 |
+
from io import BytesIO
|
5 |
|
6 |
+
classifier = pipeline("zero-shot-image-classification", model="google/siglip-base-patch16-224")
|
7 |
|
8 |
+
st.title("Image classifier model demo")
|
9 |
|
10 |
+
file_name = st.file_uploader("Upload an image")
|
11 |
+
|
12 |
+
def scan_image(image, label, tolerance = 0.01):
|
13 |
+
predictions = classifier(image, candidate_labels = [label, "other"])
|
14 |
+
dict = {}
|
15 |
+
for prediction in predictions:
|
16 |
+
dict[prediction['label']] = prediction['score']
|
17 |
+
# print(json.dumps(dict, indent = 3))
|
18 |
+
return (dict[label] > (dict['other'] + tolerance), dict)
|
19 |
|
20 |
if file_name is not None:
|
21 |
col1, col2 = st.columns(2)
|
22 |
|
23 |
image = Image.open(file_name)
|
24 |
col1.image(image, use_column_width=True)
|
|
|
25 |
|
26 |
+
label = st.text_input("What to look for in the image?", "Cats")
|
27 |
+
|
28 |
+
predictions = scan_image(image, label)
|
29 |
+
|
30 |
col2.header("Probabilities")
|
31 |
+
for key, value in predictions[1]:
|
32 |
+
col2.subheader(f"{ key }: { round(value * 100, 1)}%")
|
33 |
+
|
34 |
+
if predictions[1]:
|
35 |
+
st.header("The object is present in the given image")
|
36 |
+
else: st.header("The object is not found in the given image")
|