Introduction:
Once you get into Python, you won’t have to keep learning it!
Hello! Hello, everyone, I’m Muzi. Today is another weekend. It’s a good time to watch TV series and watch Douyin.
Think today is the weekend, to send a wave of welfare! It is not clear whether we often see this picture when brushing Douyin, face recognition detection small program, open the program, take a picture on the scene can identify your suitable hair style, appearance level score, identify age……. And so on ~
In addition to work, I would talk about some extra topics, such as how old someone is and how long he graduated. Something like that.
A lot of little elder sister ask age this problem compare “shy”, cough cough…….
So I think you are too embarrassed to say how old you are, you must want me as a technician to make an age detection machine for you!
I was kind enough to secretly age a bunch of the company’s little sisters!
Hush hush! This I only tell you not to say ~
The body of the
Principle Introduction:
(1) Preload three network models (2) open the video stream of the camera/load the image;
(3) face detection for each frame; The gender and age of the detected faces were predicted. Analytic prediction results; Display the results.
Code implementation details
Loading model:
MODEL_MEAN_VALUES = (78.4263377603, 87.7689143744, 114.895847746) ageList = [' (2-0) ', '(4-6)', '(8-12)', '(15-20)', '(25-32)', '(38-43)', '(48-53)', '(60-100)'] genderList = ['Male', 'Female'] # Load network ageNet = cv.dnn.readNet(ageModel, ageProto) genderNet = cv.dnn.readNet(genderModel, genderProto) faceNet = cv.dnn.readNet(faceModel, faceProto)Copy the code
Face detection:
frameOpencvDnn = frame.copy() frameHeight = frameOpencvDnn.shape[0] frameWidth = frameOpencvDnn.shape[1] blob = CV. DNN. BlobFromImage (frameOpencvDnn, 1.0, (300, 300), [104, 117, 123], True, False) net.setInput(blob) detections = net.forward() bboxes = [] for i in range(detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence > conf_threshold: x1 = int(detections[0, 0, i, 3] * frameWidth) y1 = int(detections[0, 0, i, 4] * frameHeight) x2 = int(detections[0, 0, i, 5] * frameWidth) y2 = int(detections[0, 0, i, 6] * frameHeight) bboxes.append([x1, y1, x2, y2]) cv.rectangle(frameOpencvDnn, (x1, y1), (x2, y2), (0, 255, 0), int(round(frameHeight/150)), 8)Copy the code
Gender and age prediction:
for bbox in bboxes: # print(bbox) face = frame[max(0,bbox[1]-padding):min(bbox[3]+padding,frame.shape[0]-1),max(0,bbox[0]-padding):min(bbox[2]+padding, Frame. Shape [1]] blob = cv.dnn.blobFromImage(face, 1.0, (227, 227), MODEL_MEAN_VALUES, swapRB=False) genderNet.setInput(blob) genderPreds = genderNet.forward() gender = genderList[genderPreds[0].argmax()] # print("Gender Output : {}".format(genderPreds)) print("Gender : {}, conf = {:.3f}".format(gender, genderPreds[0].max())) ageNet.setInput(blob) agePreds = ageNet.forward() age = ageList[agePreds[0].argmax()] print("Age Output : {}".format(agePreds)) print("Age : {}, conf = {:.3f}".format(age, agePreds[0].max())) label = "{},{}".format(gender, age) cv.putText(frameFace, label, (bbox[0], bbox[1]-10), CV.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 255), CV.LINE_AA) CV. Imshow ("Age Gender Demo", frameFace) print("time : {:.3f} ms".format(time.time() - t))Copy the code
Effect:
The attached:
Import cv2 as CV import time def getFaceBox(net, frame, conf_threshold=0.7): frameOpencvDnn = frame.copy() frameHeight = frameOpencvDnn.shape[0] frameWidth = frameOpencvDnn.shape[1] blob = CV. DNN. BlobFromImage (frameOpencvDnn, 1.0, (300, 300), [104, 117, 123], True, False) net.setInput(blob) detections = net.forward() bboxes = [] for i in range(detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence > conf_threshold: x1 = int(detections[0, 0, i, 3] * frameWidth) y1 = int(detections[0, 0, i, 4] * frameHeight) x2 = int(detections[0, 0, i, 5] * frameWidth) y2 = int(detections[0, 0, i, 6] * frameHeight) bboxes.append([x1, y1, x2, y2]) cv.rectangle(frameOpencvDnn, (x1, y1), (x2, y2), (0, 255, 0), int(round(frameHeight/150)), 8) return frameOpencvDnn, bboxes faceProto = "D:/projects/opencv_tutorial/data/models/face_detector/opencv_face_detector.pbtxt" faceModel = "D:/projects/opencv_tutorial/data/models/face_detector/opencv_face_detector_uint8.pb" ageProto = "D:/projects/opencv_tutorial/data/models/cnn_age_gender_models/age_deploy.prototxt" ageModel = "D:/projects/opencv_tutorial/data/models/cnn_age_gender_models/age_net.caffemodel" genderProto = "D:/projects/opencv_tutorial/data/models/cnn_age_gender_models/gender_deploy.prototxt" genderModel = "D:/projects/opencv_tutorial/data/models/cnn_age_gender_models/gender_net.caffemodel" MODEL_MEAN_VALUES = (78.4263377603, 87.7689143744, 114.895847746) ageList = [' (2-0) ', '(4-6)', '(8-12)', '(15-20)', '(25 to 32)', '(43) 38 -' '(48-53)', '(60-100)'] genderList = ['Male', 'Female'] # Load network ageNet = cv.dnn.readNet(ageModel, ageProto) genderNet = cv.dnn.readNet(genderModel, genderProto) faceNet = cv.dnn.readNet(faceModel, faceProto) # Open a video file or an image file or a camera stream cap = cv.VideoCapture(0) padding = 20 while cv.waitKey(1) < 0: # Read frame t = time.time() hasFrame, frame = cap.read() frame = cv.flip(frame, 1) if not hasFrame: cv.waitKey() break frameFace, bboxes = getFaceBox(faceNet, frame) if not bboxes: print("No face Detected, Checking next frame") continue for bbox in bboxes: # print(bbox) face = frame[max(0,bbox[1]-padding):min(bbox[3]+padding,frame.shape[0]-1),max(0,bbox[0]-padding):min(bbox[2]+padding, Frame. Shape [1]] blob = cv.dnn.blobFromImage(face, 1.0, (227, 227), MODEL_MEAN_VALUES, swapRB=False) genderNet.setInput(blob) genderPreds = genderNet.forward() gender = genderList[genderPreds[0].argmax()] # print("Gender Output : {}".format(genderPreds)) print("Gender : {}, conf = {:.3f}".format(gender, genderPreds[0].max())) ageNet.setInput(blob) agePreds = ageNet.forward() age = ageList[agePreds[0].argmax()] print("Age Output : {}".format(agePreds)) print("Age : {}, conf = {:.3f}".format(age, agePreds[0].max())) label = "{},{}".format(gender, age) cv.putText(frameFace, label, (bbox[0], bbox[1]-10), CV.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 255), CV.LINE_AA) CV. Imshow ("Age Gender Demo", frameFace) print("time : {:.3f} ms".format(time.time() - t))Copy the code
conclusion
The little sister of the company heard that I gave them secretly made a detection age machine, came over to buckle my milk tea!
Need complete project source code: # source base: 959755565#It’s free!
Remember three lianha ~The following is my hope of the son: count on you ~~~~