File size: 6,635 Bytes
32544a0
6a1ec3e
 
08e7ad4
 
32544a0
6a1ec3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32544a0
6a1ec3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08e7ad4
 
6a1ec3e
 
 
 
 
 
6955362
 
 
 
 
 
08e7ad4
 
6955362
 
 
 
 
 
6a1ec3e
08e7ad4
 
 
 
 
 
6a1ec3e
 
08e7ad4
6a1ec3e
 
 
 
 
 
6955362
6a1ec3e
6955362
6a1ec3e
 
08e7ad4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a1ec3e
 
 
 
 
 
 
 
 
 
6955362
 
6a1ec3e
 
 
 
 
405f8e6
6a1ec3e
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
import gradio as gr
from transformers import pipeline
from PIL import Image
import cv2
import numpy as np

# Function to classify the face shape
def classify_face_shape(image):
    
    # Initialize the pipeline
    pipe = pipeline("image-classification", model="metadome/face_shape_classification")
    
    # Run the pipeline on the uploaded image
    output = pipe(image)
    # Log the output for debugging
    print("Pipeline output for shape:", output)
    # Format the output to be compatible with gr.outputs.Label
    formatted_output = {item['label']: item['score'] for item in output}
    
    return formatted_output
    
def classify_age(image):
    pipe = pipeline("image-classification", model="nateraw/vit-age-classifier")
    # Run the pipeline on the uploaded image
    output = pipe(image)
    
    print("Pipeline output for age:", output)
    # Format the output to be compatible with gr.outputs.Label
    formatted_output = {item['label']: item['score'] for item in output}
    
    return formatted_output
    
def classify_skin_type(image):
    pipe = pipeline("image-classification", model="dima806/skin_types_image_detection")
    
    # Run the pipeline on the uploaded image
    output = pipe(image)
    
    print("Pipeline output for skin_type:", output)
    # Format the output to be compatible with gr.outputs.Label
    formatted_output = {item['label']: item['score'] for item in output}
    
    return formatted_output

def classify_acne_type(image):
    pipe = pipeline("image-classification", model="imfarzanansari/skintelligent-acne")
    
    # Run the pipeline on the uploaded image
    output = pipe(image)
    
    print("Pipeline output for acne:", output)
    # Format the output to be compatible with gr.outputs.Label
    formatted_output = {item['label']: item['score'] for item in output}
    
    return formatted_output

def classify_hair_color(image):
    
    #pipe = pipeline("image-classification", model="enzostvs/hair-color")
    pipe = pipeline("image-classification", model="londe33/hair_v02")
    
    # Run the pipeline on the uploaded image
    output = pipe(image)
    
    print("Pipeline output for hir color:", output)
    # Format the output to be compatible with gr.outputs.Label
    formatted_output = {item['label']: item['score'] for item in output}
    
    return formatted_output

def classify_eye_shape(image):
    
    pipe = pipeline("image-classification", model="justingrammens/eye-shape")
    
    # Run the pipeline on the uploaded image
    #output = pipe(image)
    output = pipe("eye_regions.jpg") # use the eye_regions image instead
    
    print("Pipeline output for eye shape:", output)
    # Format the output to be compatible with gr.outputs.Label
    formatted_output = {item['label']: item['score'] for item in output}
    
    return formatted_output

def classify_eye_color(image):

    pipe = pipeline("image-classification", model="justingrammens/eye-color")
    
    # Run the pipeline on the uploaded image
    #output = pipe(image)
    output = pipe("eye_regions.jpg") #use the eye_regions image instead
    
    print("Pipeline output for eye color:", output)
    # Format the output to be compatible with gr.outputs.Label
    formatted_output = {item['label']: item['score'] for item in output}
    
    return formatted_output
    

def process_gradio_image(pil_image):
    # Convert PIL image to NumPy array
    image = np.array(pil_image)
    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)  # Convert RGB (from PIL) to BGR (OpenCV default)
    return image
    
def classify_image_with_multiple_models(image):
    create_eye_region(image)
    face_shape_result = classify_face_shape(image)
    age_result = classify_age(image)
    skin_type_result = classify_skin_type(image)
    acne_results = classify_acne_type(image)
    hair_color_results = classify_hair_color(image)
    eye_shape = classify_eye_shape(image)
    eye_color = classify_eye_color(image)

    return face_shape_result, age_result, skin_type_result, acne_results, hair_color_results, eye_shape, eye_color


def create_eye_region(image):
    # Load the pre-trained face detector
    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
    eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')

    image = process_gradio_image(image)
    # Convert the image to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Detect faces in the image
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x, y, w, h) in faces:
        # Draw a rectangle around the face
        cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)

        # Region of Interest (ROI) for the face
        roi_gray = gray[y:y + h, x:x + w]
        roi_color = image[y:y + h, x:x + w]

        # Detect eyes in the face ROI
        eyes = eye_cascade.detectMultiScale(roi_gray)

        for (ex, ey, ew, eh) in eyes:
            # Draw a rectangle around the eyes
            cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)

            # Extract the eye region
            eye_roi = roi_color[ey:ey + eh, ex:ex + ew]
            cv2.imwrite('eye_regions.jpg', eye_roi)

            # Calculate the average color of the eye region
            avg_color = np.mean(eye_roi, axis=(0, 1))

            # Classify eye color based on average color
            if avg_color[0] > avg_color[1] and avg_color[0] > avg_color[2]:
                color = "Brown"
            elif avg_color[1] > avg_color[0] and avg_color[1] > avg_color[2]:
                color = "Green"
            else:
                color = "Blue"

            # Display the eye color
            cv2.putText(image, color, (ex, ey - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)

            cv2.imwrite('segmented_face.jpg', image)


# Create the Gradio interface
demo = gr.Interface(
    fn=classify_image_with_multiple_models,  # The function to run
    inputs=gr.Image(type="pil"),
    outputs=[
        gr.Label(num_top_classes=5, label="Face Shape"), 
        gr.Label(num_top_classes=5, label="Age"), 
        gr.Label(num_top_classes=3, label="Skin Type"), 
        gr.Label(num_top_classes=5, label="Acne Type"), 
        gr.Label(num_top_classes=5, label="Hair Color"), 
        gr.Label(num_top_classes=4, label="Eye Shape"),
        gr.Label(num_top_classes=5, label="Eye Color"),
    ],
    title="Multiple Model Classification",
    description="Upload an image to classify the face using mutiple classification models"
)
    
demo.launch(auth=("admin", "pass1234"))
#demo.launch()