In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Based on how Python carries out age and gender detection, this article introduces in detail the corresponding analysis and answers to this problem, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Preface
I will use the Python programming language to take you through the task of using machine learning for age and gender testing. Age and gender detection is a computer vision task, so I will use the OpenCV library in Python.
Before I start the task of using Python for age and gender testing, I'll first show you what this concept means and how to deal with age and gender testing. It is important to understand this concept so that you can easily use Python and any programming language to perform age and gender testing tasks in the future.
Brief introduction of Age and Sex testing
However, the task of detecting age and gender is an inherent problem, more so than many other computer vision tasks. The main reason for this difficulty gap is the data needed to train these types of systems.
Although the average object detection task can usually access hundreds of thousands or even millions of images for training, data sets with age and / or gender tags are much smaller, usually thousands, up to tens of thousands.
The reason is that in order to tag such an image, we need to access the personal information of the subject in the image. In other words, we need information about their date of birth and gender, especially the date of birth, which is rarely made public.
In other words, we need information about their date of birth and gender, especially the date of birth, which is rarely made public. Therefore, we must solve the nature of this problem, and we are solving and adjusting the network architecture and algorithm methods to deal with these limitations.
Getting started:
Now let's start using the Python programming language for age and gender detection tasks. I will first write the code for face detection, because without face detection, we will not be able to further complete the task of age and sex prediction.
You can download the necessary OpenCV pre-training models needed for age and gender testing tasks here. Now after importing the OpenCV module into your python file, you can start using the following code.
Python code for face detection: 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, 4] * frameHeight) 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
Now the next step is to predict the sex of the human in the image. Here, I load the gender network into memory and transmit the detected faces through the network for gender detection tasks.
Python code for gender detection: genderProto = "gender_deploy.prototxt" genderModel = "gender_net.caffemodel" ageNet = cv.dnn.readNet (ageModel, ageProto) genderList = ['Male',' Female'] blob = cv.dnn.blobFromImage (face, 1, (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: {}" .format (gender))
Now the next task is to predict the age of humans in the image. Here I will load the aging network and use forward delivery to get the output. Because the network structure is similar to the gender network, we can make full use of all the expected age groups that are output to get the task to detect age.
Python code for age detection: ageProto = "age_deploy.prototxt" ageModel = "age_net.caffemodel" ageNet = cv.dnn.readNet (ageModel, ageProto) ageList = ['(0-2)','(4-6)','(8-12)','(15-20)','(25-32)','(38-43)','(48-53)' AgeNet.setInput (blob) agePreds = ageNet.forward () age = ageList [agePreds [0] .argmax ()] print ("Gender Output: {}" .format (agePreds)) print ("Gender: {}" .format (age))
The last piece of code we need to write is to display the output:
Label = "{}, {}" .format (gender, age) cv.putText (frameFace, label, (bbox [0], bbox [1]-20), cv.FONT_HERSHEY_SIMPLEX, 0.8, (255,0,0), 3, cv.LINE_AA) cv.imshow ("Age Gender Demo", frameFace)
This is the answer to the question about how to do age and gender testing based on Python. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.