File size: 2,626 Bytes
e4facfd
 
 
 
 
 
 
 
ceaa804
 
e4facfd
 
 
e032796
77e9932
4ea0bc3
ee297ce
e4facfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
441f680
e4facfd
 
 
 
 
 
441f680
e4facfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
441f680
e4facfd
ee297ce
 
d509efb
 
ee297ce
77e9932
ee297ce
 
 
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
import gradio as gr
import cv2
import dlib

def age_predict(photo):

    # photo = "face_age/001/16.png"

    # img = cv2.imread(photo)
    img = cv2.resize(photo, (720, 640))
    frame = img.copy()

    # Model for Age detection
    age_weights = "age_deploy.prototxt"
    age_config = "age_net1.caffemodel"
    age_Net = cv2.dnn.readNet(age_config, age_weights)
    

    # Model requirements for image
    ageList = ['(0-2)', '(4-6)', '(8-12)', '(15-20)', '(25-32)', '(38-43)', '(48-53)', '(60-100)']
    model_mean = (78.4263377603, 87.7689143744, 114.895847746) # Taken from the official site

    # Storing the image dimensions
    fH = img.shape[0]
    fW = img.shape[1]

    Boxes = [] # to store the face co-ordinates
    mssg = 'Face Detected'

    # Model for face detection
    face_detector = dlib.get_frontal_face_detector()
    # converting to grayscale
    img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Detecting the faces
    faces = face_detector(img_gray)

    # If no faces are detected
    if not faces:
        print("No faces")
        mssg = 'No face detected'
        # cv2.putText(img, f'{mssg}', (40,40), cv2.FONT_HERSHEY_SIMPLEX, 2, (200), 2)
        # cv2.imshow('image',img)
        # cv2.waitKey(0)
        return mssg
    else:
        print("have faces")
        # Bounding face
        for face in faces:
            x = face.left() # extracting the face coordinates
            y = face.top()
            x2 = face.right()
            y2 = face.bottom()

            # Rescaling those coordinates for our image
            box = [x, y, x2, y2]
            Boxes.append(box)
            cv2.rectangle(frame, (x,y), (x2, y2), (00, 200, 200), 2)

        for box in Boxes:
            face = frame[box[1]:box[3], box[0]:box[2]]
            
            # Image preprocessing
            blob = cv2.dnn.blobFromImage(face, 1.0, (227, 227), model_mean, swapRB = False)
            
            # Age Prediction
            age_Net.setInput(blob)
            age_preds = age_Net.forward()
            age = ageList[age_preds[0].argmax()]
            
            # cv2.putText(frame, f'{mssg}:{age}', (box[0], box[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,255,255),2,cv2.LINE_AA)
            
            # cv2.imshow('image', frame)
            # cv2.waitKey(0)
            print(f'{mssg}:{age}')
            return f'{mssg}:{age}'
inter_arguments = {
    "fn": age_predict, 
    "inputs":gr.components.Image(),
    "outputs": gr.components.Textbox(),
    "title": "Age Detector",
    "description": "Detect your age class",

    }
gr.Interface(**inter_arguments).launch(share=True)