File size: 4,547 Bytes
0f7e74b
fee4ed8
0f7e74b
 
fee4ed8
0f7e74b
047d5b0
 
 
 
cc65489
13e77a1
 
cc65489
047d5b0
73bea8b
b4a533c
78895ec
 
 
 
 
46a5120
 
 
 
 
 
 
2c39f53
047d5b0
29401a0
22b8a05
 
73bea8b
22b8a05
cc65489
73bea8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a35f404
cc65489
fa64131
 
 
22b8a05
 
fa64131
 
78895ec
596475f
 
 
 
 
fa64131
 
78895ec
22b8a05
fa64131
73bea8b
78895ec
 
22b8a05
fa64131
22b8a05
 
 
78895ec
22b8a05
 
0f7e74b
78895ec
a587147
1341b34
 
78895ec
 
 
 
 
1341b34
 
78895ec
 
a587147
047d5b0
b4a533c
a587147
 
a57bf86
 
78895ec
641cb95
78895ec
2c39f53
 
b4a533c
 
a587147
29401a0
b4a533c
 
 
 
 
78895ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f7e74b
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import streamlit as st
from transformers import pipeline
from PIL import Image
from datasets import load_dataset, Image, list_datasets
from PIL import Image

MODELS = [
            "google/vit-base-patch16-224", #Classifição geral
            "nateraw/vit-age-classifier" #Classifição de idade
]
DATASETS = [
            "Nunt/testedata",
            "Nunt/backup_leonardo_2024-02-01" 
]
MAX_N_LABELS = 5
SPLIT_TO_CLASSIFY = 'pasta'

# COL1, COL2 = st.columns([3, 1])
# CONTAINER_TOP = st.container()
# CONTAINER_BODY = st.container()
# CONTAINER_FULL = st.container()
# CONTAINER_LOOP = st.container()
COL1=""        
COL2="" 
COLS="" 
CONTAINER_TOP="" 
CONTAINER_BODY="" 
CONTAINER_FULL="" 
CONTAINER_LOOP="" 



#(image_object, classifier_pipeline)
#def classify_one_image(classifier_model, dataset_to_classify):
#classify_one_image(image_object, classifier_pipeline)
def classify_one_image(classifier_model, dataset_to_classify):
       
       
          
    #image_object = dataset[SPLIT_TO_CLASSIFY][i]["image"]
    #st.image(image_object, caption="Uploaded Image", width=300)

       
    #for i in range(len(dataset_to_classify)): 
    #for image in dataset_to_classify:
        #image_object = dataset[SPLIT_TO_CLASSIFY][i]["image"]
        #st.image(image_object, caption="Uploaded Image", width=300)
        
        #st.write(f"Image classification: ", image['file'])

        # image_path = image['file']
        # img = Image.open(image_path)
        # st.image(img, caption="Original image", use_column_width=True)
        # results = classifier(image_path, top_k=MAX_N_LABELS)
        # st.write(results)
        # st.write("----")
    
    return "done" 



def classify_full_dataset(shosen_dataset_name, chosen_model_name):
    image_count = 0
     
    #dataset
    dataset = load_dataset(shosen_dataset_name,"testedata_readme")
    with CONTAINER_LOOP:
        #Image teste load 
        image_object = dataset['pasta'][0]["image"]
        st.image(image_object, caption="Uploaded Image", width=300)
        st.write("### FLAG 3")
        
    #modle instance
    classifier_pipeline = pipeline('image-classification', model=chosen_model_name)
    CONTAINER_LOOP.write("### FLAG 4")
    
    #classification
    classification_result = classifier_pipeline(image_object)
    CONTAINER_LOOP.write(classification_result)
    CONTAINER_LOOP.write("### FLAG 5")
    #classification_array.append(classification_result)
    
    #save classification
    
    image_count += 1
    CONTAINER_LOOP.write(f"Image count: {image_count}")
    
    return image_count


def make_template():
    COLS = st.columns([3, 1])
    
    CONTAINER_FULL = st.container()
    with CONTAINER_FULL:
        CONTAINER_TOP = st.container()
        CONTAINER_BODY = st.container()
        with CONTAINER_BODY:  
            #COL1, COL2 = st.columns([3, 1])
            with COLS[1]:
                CONTAINER_LOOP = st.container()


def main():
    
    make_template()
    
    with CONTAINER_TOP:
        st.title("Bulk Image Classification DEMO")

    
    # TODO Restart or reset your app
    # if st.button("Restart"):
    # # Code to restart or reset your app goes here
        #     import subprocess
        #     subprocess.call(["shutdown", "-r", "-t", "0"])
        
    with CONTAINER_BODY:

        with COL1:
            st.markdown("This app uses several 🤗 models to classify images stored in 🤗 datasets.")
            st.write("Soon we will have a dataset template")

        #Model
        chosen_model_name = st.selectbox("Select the model to use",  MODELS, index=0)
        if chosen_model_name is not None:
            COL1.st.write("You selected", chosen_model_name) 

        #Dataset
        shosen_dataset_name = st.selectbox("Select the dataset to use",  DATASETS, index=0)
        if shosen_dataset_name is not None:
            COL1.st.write("You selected", shosen_dataset_name)

        #click to classify
        #image_object = dataset['pasta'][0]  
        if chosen_model_name is not None and shosen_dataset_name is not None:
            if COL1.button("Classify images"):
            
                #classification_array =[]
                classification_result = classify_full_dataset(shosen_dataset_name, chosen_model_name)
                CONTAINER_LOOP.write(f"Classification result: {classification_result}")
                #classification_array.append(classification_result)
                #st.write("# FLAG 6")
                #st.write(classification_array)   

if __name__ == "__main__":
    main()