File size: 2,641 Bytes
c9d37f2
5a721da
c9d37f2
 
5a721da
c9d37f2
5a721da
 
 
 
 
 
 
 
c9d37f2
5a721da
 
 
 
 
 
c9d37f2
5a721da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a46df70
 
 
5a721da
 
0f58034
5a721da
 
 
 
 
 
4bb1c1a
5a721da
 
 
 
 
 
 
d737fdb
 
5a721da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154397e
5a721da
 
0f58034
5a721da
0f58034
154397e
 
5a721da
2a20d87
5a721da
ee6828d
5a721da
fa53bb9
 
 
5a721da
 
154397e
2a20d87
154397e
fa53bb9
 
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
'''
    Build Project
'''

# imported necessary libraries
import gradio as gr
import tensorflow as tf
import numpy as np
import os
import yaml
import cv2
from PIL import Image
import io
import json

# read config file
def read_config():
    config = {}
    print(os.path.curdir)
    with open('models_config.yaml', 'r') as cf:
        config = yaml.safe_load(cf)

    for var in config:
        config[var] = config[var].replace(';', os.sep)

    return config

config = read_config()

# loading the models
age_model = tf.keras.models.load_model(config['A_M_PATH'])
gender_model = tf.keras.models.load_model(config['G_M_PATH'])
face_cascade = cv2.CascadeClassifier(config['FD_M_PATH'])
 

def main_pipeline(image):
    
    if image is None:
        image = Image.open(config['BANNER_IMG'])
        return image, "NO FACE DETECTED", "NO FACE DETECTED"


    original_image = image
    image = np.asarray(image)

    gray_img = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    faces = face_cascade.detectMultiScale(gray_img, 1.1, 4)
    
    if len(faces) == 0:
        return original_image , "NO FACE DETECTED", "NO FACE DETECTED"

    x, y, w, h =  faces[0]


    image = Image.fromarray(image)
    image = image.crop((x, y, x + w, y + h))

    image = image.resize((224,224))
    #image = tf.image.resize(image, [224,224]) 
    image = tf.keras.preprocessing.image.img_to_array(image)
    image = image / 255.0      
    image = tf.expand_dims(image, axis=0)

    age_prds = age_model.predict(image)
    gender_prds = gender_model.predict(image)

    age_prds = np.around(age_prds)
    gender_prds = np.around(gender_prds)
    gender = ""

    if gender_prds[0][0] == 0:
        gender = 'male'
    else:
        gender = 'female'

    data = {}
    data['age'] = int(age_prds[0][0])
    data['gender'] = gender

    image_b_box = cv2.rectangle(np.asarray(original_image), (x, y), (x+w, y+h), (255, 0, 0), 2)

    result_image = Image.fromarray(image_b_box)

    return result_image, f"~ {(data['age'])}", data['gender'] 


# initializing the input component
image = gr.inputs.Image()

# initializzing examples
test_imgs = [os.path.join(config['TEST_IMG_PATH'],img) for img in os.listdir(config['TEST_IMG_PATH'])]
examples = test_imgs
 
# launching the interface
gr.Interface(fn = main_pipeline, title= 'Age & Gender predictions', inputs = image,
             outputs = [gr.Image(label= 'Result', shape= (250,250)),
                        gr.Text(label= 'Age'), 
                        gr.Text(label= 'Gender')], flagging_options=["correct", "incorrect", "other"],
                        examples= examples, capture_session = True).launch()