Spaces:
Runtime error
Runtime error
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) |