File size: 2,420 Bytes
a337076
 
1636073
a337076
 
 
1636073
a337076
 
 
 
 
 
 
 
 
1636073
a337076
 
1636073
a337076
1636073
 
a337076
1636073
a337076
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1636073
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
from deepface import DeepFace
import pandas as pd
import gradio as gr
import matplotlib.pyplot as plt
import tempfile
import os

def faceAnalyzer(image_path):
    def analyze(image_path, attribute):
        analysis = DeepFace.analyze(img_path=image_path, actions=['gender', 'race', 'emotion', 'age'])
        df = pd.DataFrame(analysis[0])
        plot = df[attribute].plot(kind='line', figsize=(9, 5), title=attribute).get_figure()
        _, temp_filename = tempfile.mkstemp(suffix=".png")
        plot.savefig(temp_filename, dpi=600)
        plt.close(plot)
        return temp_filename

    attributes = ['gender', 'race', 'emotion']
    images = [analyze(image_path, attribute) for attribute in attributes]

    return [gr.Image(image) for attribute, image in zip(attributes, images)]


def faceAnalyzer2(image_path, attribute):

  analysis = DeepFace.analyze(img_path=image_path, actions=['age', 'gender', 'race', 'emotion'])
    # convert the resulting dictionary to a DataFrame
  df = pd.DataFrame(analysis[0])

  if attribute == "gender":
    gender = df['gender'].plot(kind = 'line', figsize = (9, 5), title = 'Gender').get_figure()
    return gender

  elif attribute == "race":
    race = df['race'].plot(kind = 'line', figsize = (9, 5), title = 'Race').get_figure()
    return race
  
  elif attribute == "emotion":
    emotion = df['emotion'].plot(kind = 'line', figsize = (9, 5), title = 'Emotion').get_figure()
    return emotion


app1 = gr.Interface(faceAnalyzer,
                   inputs=gr.Image(label="Upload Photo"),
                   outputs=[gr.Image(label="Gender Analysis"),
                            gr.Image(label="Race Analysis"),
                            gr.Image(label="Emotion Analysis")],
                   theme=gr.themes.Soft())

app2 = gr.Interface(faceAnalyzer2,
                    inputs=[gr.Image(label="Upload Photo"),gr.Radio(choices=["gender","race","emotion"],
                                                                    value="gender",
                                                                    label="Attributes",
                                                                    info="Select an attribute")],
                    outputs=gr.Plot(label="Analysis Output"),
                    theme=gr.themes.Soft())

application = gr.TabbedInterface([app1,app2],["Full Analysis","Select Analysis"],theme=gr.themes.Soft())


application.launch()