File size: 7,549 Bytes
1b3fb15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from gradio import AnnotatedImage, Blocks, Button, Column, Dropdown, Examples, Gallery, HTML, Image, Row, SelectData, Tab, Textbox

import db
import deeperface
import os.path

################################################################################


def delete(id):
    db.delete_by_id(id)


def emotions():
    return deeperface.Emotion.labels


def process(path):
    img = deeperface.Image.read(path)

    ############################################################################

    if img.nsfw():
        img.pixelate().write(path)

    ############################################################################

    if db.exists(path):
        id, metadata = db.get(path)
    else:
        metadata = deeperface.Metadata(img)

        id = db.update(path, metadata)

    ############################################################################

    annotations = []
    for face in metadata:
        annotations.extend(img.annotate(face, metadata[face]['emotion']))

    ############################################################################

    verified_paths = []
    for test_id, test_path, test_metadata in db.tuples():
        if test_path != path:
            if deeperface.verify(metadata.representations(),
                                 test_metadata.representations()):
                verified_paths.append((test_path, os.path.basename(test_id)))

    ############################################################################

    return id, (path, annotations), verified_paths


def search(filter='All'):
    filtered_paths = []
    for id, path, metadata in db.tuples():
        if filter == 'All' or filter in metadata.emotions():
            filtered_paths.append((path, os.path.basename(id)))

    return filtered_paths


################################################################################

with Blocks(title='Face recognition and facial attribute analysis') as blocks:
    HTML(
        '<h1><p align="center">Face recognition and facial attribute analysis</p></h1>'
    )
    with Row():
        with Column(scale=3):
            with Row():
                with Column():
                    textbox = Textbox(visible=False)
                    annotated_image = AnnotatedImage(color_map={
                        'face': '#f97316',
                        'emotion': '#f3f4f6'
                    },
                                                     show_legend=False,
                                                     visible=False)
                    button = Button('Delete', visible=False)
                with Column():
                    gallery_1 = Gallery(columns=3,
                                        container=False,
                                        show_download_button=False,
                                        show_share_button=False,
                                        visible=False)
                    html = HTML(visible=False)
        with Column(scale=2):
            with Tab(label='Upload / Camera'):
                image = Image(container=False,
                              sources=['upload', 'webcam'],
                              type='filepath')
                Examples('examples', image)
            with Tab(label='Gallery') as tab:
                dropdown = Dropdown(['All'] + emotions(),
                                    container=False,
                                    filterable=False,
                                    value=0)
                gallery_2 = Gallery(allow_preview=False,
                                    columns=3,
                                    container=False,
                                    show_share_button=False)

    ############################################################################

    def on_button_click(textbox, dropdown):
        if not textbox or not dropdown:
            return AnnotatedImage(), Button(), Gallery(), HTML(), Gallery()

        delete(textbox)
        gallery_2 = search(dropdown)

        return AnnotatedImage(visible=False), Button(visible=False), Gallery(
            visible=False), HTML(visible=False), Gallery(gallery_2,
                                                         selected_index=None)

    button.click(on_button_click, [textbox, dropdown],
                 [annotated_image, button, gallery_1, html, gallery_2],
                 show_progress='hidden')

    ############################################################################


    def on_image_change_or_select(image, dropdown):
        if not image or not dropdown:
            return Textbox(), AnnotatedImage(), Button(), Gallery(), HTML(
            ), Gallery()

        textbox, annotated_image, gallery_1 = process(image)
        gallery_2 = search(dropdown)

        if len(gallery_1) > 1:
            return textbox, AnnotatedImage(
                annotated_image, label=textbox, visible=True
            ), Button(visible=True), Gallery(gallery_1, visible=True), HTML(
                f'<i><p align="center">{len(gallery_1)} Similar Images in Gallery</p></i>',
                visible=True), Gallery(gallery_2, selected_index=None)
        elif len(gallery_1) > 0:
            return textbox, AnnotatedImage(
                annotated_image, label=textbox, visible=True
            ), Button(visible=True), Gallery(gallery_1, visible=True), HTML(
                '<i><p align="center">1 Similar Image in Gallery</p></i>',
                visible=True), Gallery(gallery_2, selected_index=None)
        else:
            return textbox, AnnotatedImage(
                annotated_image, label=textbox,
                visible=True), Button(visible=True), Gallery(
                    visible=False), HTML(visible=False), Gallery(
                        gallery_2, selected_index=None)

    image.change(
        on_image_change_or_select, [image, dropdown],
        [textbox, annotated_image, button, gallery_1, html, gallery_2],
        show_progress='hidden')

    image.select(
        on_image_change_or_select, [image, dropdown],
        [textbox, annotated_image, button, gallery_1, html, gallery_2],
        show_progress='hidden')

    ############################################################################


    def on_tab_select(dropdown):
        if not dropdown:
            return Gallery()

        gallery_2 = search(dropdown)

        return Gallery(gallery_2, selected_index=None)

    tab.select(on_tab_select, dropdown, gallery_2, show_progress='hidden')

    ############################################################################


    def on_dropdown_select_event(event: SelectData):
        dropdown = event.value

        gallery_2 = on_tab_select(dropdown)

        return gallery_2

    dropdown.select(on_dropdown_select_event,
                    outputs=gallery_2,
                    show_progress='hidden')

    ############################################################################


    def on_gallery_2_select_event(event: SelectData, dropdown):
        image = event.value['image']['path']

        textbox, annotated_image, button, gallery_1, html, gallery_2 = on_image_change_or_select(
            image, dropdown)

        return textbox, annotated_image, button, gallery_1, html, gallery_2

    gallery_2.select(
        on_gallery_2_select_event,
        dropdown,
        [textbox, annotated_image, button, gallery_1, html, gallery_2],
        show_progress='hidden')

    ############################################################################

blocks.launch(show_api=False)