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( '
Face recognition and facial attribute analysis
' ) 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'{len(gallery_1)} Similar Images in Gallery
', 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( '1 Similar Image in Gallery
', 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)