Nuno Tome
commited on
Commit
•
eac9ed5
1
Parent(s):
14d39d1
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
MODEL_1 = "google/vit-base-patch16-224"
|
6 |
+
MIN_ACEPTABLE_SCORE = 0.1
|
7 |
+
MAX_N_LABELS = 5
|
8 |
+
MODEL_2 = "nateraw/vit-age-classifier"
|
9 |
+
MODELS = [
|
10 |
+
"google/vit-base-patch16-224", #Classifição geral
|
11 |
+
"nateraw/vit-age-classifier", #Classifição de idade
|
12 |
+
"microsoft/resnet-50", #Classifição geral
|
13 |
+
#NOT OK "microsoft/beit-base-patch16-224-pt22k-ft22k", #Classifição geral
|
14 |
+
"Falconsai/nsfw_image_detection", #Classifição NSFW
|
15 |
+
"cafeai/cafe_aesthetic", #Classifição de estética
|
16 |
+
"timm/vit_large_patch14_clip_224.openai_ft_in12k_in1k", #Classifição geral
|
17 |
+
"timm/vit_base_patch16_224_in21k", #Classifição geral escolhida pelo copilot
|
18 |
+
"microsoft/resnet-18", #Classifição geral
|
19 |
+
"microsoft/resnet-34", #Classifição geral escolhida pelo copilot
|
20 |
+
"microsoft/resnet-101", #Classifição geral escolhida pelo copilot
|
21 |
+
"microsoft/resnet-152", #Classifição geral escolhida pelo copilot
|
22 |
+
"microsoft/resnet-50-kinetics-400", #Classifição geral escolhida pelo copilot
|
23 |
+
"microsoft/swin-tiny-patch4-window7-224",#Classifição geral
|
24 |
+
""
|
25 |
+
|
26 |
+
]
|
27 |
+
|
28 |
+
def classify(image, model):
|
29 |
+
classifier = pipeline("image-classification", model=model)
|
30 |
+
result= classifier(image)
|
31 |
+
return result
|
32 |
+
|
33 |
+
def save_result(result):
|
34 |
+
st.write("In the future, this function will save the result in a database.")
|
35 |
+
|
36 |
+
def print_result(result):
|
37 |
+
|
38 |
+
comulative_discarded_score = 0
|
39 |
+
for i in range(len(result)):
|
40 |
+
if result[i]['score'] < MIN_ACEPTABLE_SCORE:
|
41 |
+
comulative_discarded_score += result[i]['score']
|
42 |
+
else:
|
43 |
+
st.write(result[i]['label'])
|
44 |
+
st.progress(result[i]['score'])
|
45 |
+
st.write(result[i]['score'])
|
46 |
+
|
47 |
+
st.write(f"comulative_discarded_score:")
|
48 |
+
st.progress(comulative_discarded_score)
|
49 |
+
st.write(comulative_discarded_score)
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
def main():
|
54 |
+
st.title("Image Classification")
|
55 |
+
input_image = st.file_uploader("Upload Image")
|
56 |
+
shosen_model = st.selectbox("Select the model to use", MODELS)
|
57 |
+
|
58 |
+
if input_image is not None:
|
59 |
+
image_to_classify = Image.open(input_image)
|
60 |
+
st.image(image_to_classify, caption="Uploaded Image", use_column_width=True)
|
61 |
+
|
62 |
+
if st.button("Classify"):
|
63 |
+
image_to_classify = Image.open(input_image)
|
64 |
+
classification_obj1 =[]
|
65 |
+
avable_models = st.selectbox
|
66 |
+
|
67 |
+
classification_result = classify(image_to_classify, shosen_model)
|
68 |
+
classification_obj1.append(classification_result)
|
69 |
+
print_result(classification_result)
|
70 |
+
save_result(classification_result)
|
71 |
+
|
72 |
+
|
73 |
+
if __name__ == "__main__":
|
74 |
+
main()
|