Frontiers in Computing and Intelligent Systems ISSN: 2832-6024 | Vol. 1, No. 1, 2022 1 Face and Gender Detection Based on BP Neural Network Algorithm Ji Zhang 1, 2, Zijian Ran 1, * 1 School of Control and Computer Engineering, North China Electric Power University, Baoding 071003, Hebei Province, China 2 Engineering Research Center of Intelligent Computing for Complex Energy Systems, Ministry of Education, Baoding 071003, Hebei Province, China * Corresponding author: Zijian Ran (Email: 529726064@qq.com) Abstract: Face recognition technology has a wide range of applications in real life, and many applications can be developed on the basis of face recognition, such as gender recognition, age recognition, face comparison, and beauty image decoration. In this paper, based on the computer vision library OpenCV in the python language, the face is recognized, and the mature BP neural network algorithm is used to learn the photos in the database, and finally the gender detection of the face is successfully realized. Keywords: Face detection; Gender detection; OpenCV; BP neural network; Python. 1. Introduction In the rapidly developing modern society, fast, convenient and safe have become synonymous with contemporary society. In order to make life more convenient and save people from heavy labor, computer technology and its applications have become an indispensable role in people's lives. Among them, the development and application of artificial intelligence technology has become the focus and hotspot of domestic and foreign scholars and researchers. In the field of artificial intelligence, the research of 3D face recognition technology has become a hot research field, and it can be applied to various places that require identity authentication, such as bank transaction authentication in the financial field, Internet or Transaction identity verification on online banking, access security check in national security defense, various monitoring systems and access control systems are effective means of identity recognition [1-3]. As early as 1964, foreign personnel began to engage in the research of face recognition technology. Face recognition is a form of biometric technology, which involves many methods in pattern recognition, computer vision, psychology, physiology, and cognitive science. The realization of identity recognition with the aid of computers is based on the unique characteristics of people. An efficient means of authentication. It can also be said that face recognition also refers to a given face as an input, looking for a match in the database to be recognized, and finding a face image consistent with the input face in the database. A large number of researchers have invested in the research of eyelid recognition, which has made the technology of eyelid recognition develop rapidly. Although face recognition technology has been researched for a long time and has been developed for many years, it still fails to achieve people's expectations. Therefore, people's research on face recognition technology is still under exploration [4-5]. 2. BP neural network algorithm 2.1. Introduction and Practice BP neural network usually refers to a multi-layer forward neural network based on the error back propagation algorithm. The transfer function used by the neurons of the BP network is usually a sigmoid-type differentiable function, so any nonlinear mapping between input and output can be realized, which makes it suitable for applications such as signal processing, computer networks, process control, speech recognition, function approximation, Successful applications have been achieved in areas such as pattern recognition and data compression. 2.2. BP neural network structure Each neuron is represented by a node, and the network consists of input layer, hidden layer and output layer nodes. The hidden layer can be one layer or multiple layers, and the nodes from the front layer to the back layer are connected by weights. Hidden nodes generally use sigmoid functions, and input and output nodes can use sigmoid functions or linear functions. Because the BP algorithm is used, it is often called BP neural network. 2.3. Principle of BP Algorithm The BP algorithm consists of two parts: forward propagation and back propagation. In the process of forward propagation, the input information is transmitted from the input layer to the output layer after being processed by the hidden layer unit. The state of each layer of neurons only affects the state of the next layer of neurons. If the output layer does not get the desired output, it turns to backpropagation, that is, the error signal is returned along the connection path, and the error signal is minimized by modifying the connection weights between neurons in each layer [6]. Since the multiple-input-multiple-output network can be transformed into a multiple-input-single-output situation, a multiple-input-single-output neural model is used here. Suppose the network consists of M layers, and the Mth layer only contains output nodes, and the first layer is the input node. In addition, assuming that there are N standard sample pairs undefined, the output to the Mth layer of the network is O(p), and the function function of the hidden layer node and the output layer node is the SIgmoid line function, that is 2 Qxe xf −+ = 1 1 )( (1) It reflects the saturation property of the neuron and takes values between 0 and 1. In the above formula, Q is a parameter representing the nonlinearity of the neuron, which is called a gain type (Gain), also called an adjustment parameter. The larger the Q value, the steeper the S-shaped curve; on the contrary, the smaller the Q value, the flatter the S-shaped curve; generally, take Q=1. The specific BP algorithm steps can be summarized as follows: The first step is to select initial weights and thresholds. In the second step, the following process is repeated until the performance requirements are met: 1.for learning samples p = 1 to N 1) Calculate the values of yj, nutj and O of each node j in each layer (forward process); 2) For each layer from the M layer to the second layer, for each node of each layer, reverse calculation (reverse process); 2.Correction weights. 3. Experimental tool 3.1. PyCharm PyCharm is a python programming IDE, which is very convenient to install. 3.2. OpenCV OpenCV is an important tool for pre-learning of computer vision, machine vision, and image processing. Due to its lightweight and efficient properties, it is widely used to solve computer problems in various fields. This paper implements face recognition and gender detection by using the python interface in OpenCV. 4. Experiment procedure The input matrix is a large matrix, and each column in the matrix represents a photo. For example, we have 200 photos, and each photo has 10*10=100 pixels, then this matrix is a 100*200 matrix. To get these data, our first step is to find a face recognition library. There are many open source face recognition libraries from which you can get all kinds of photos you want. After obtaining the photo, we first need to identify the face. This function here will identify the face and then grayscale it and re-save it as a numbered photo. Since the pixel value of the recognized face is too large, and different photos contain different pixel values, we need to unify the pixel values of each photo to obtain a grayscale image with uniform pixel values, we need to get all the images into one Inside the large matrix as input to the core function this function returns a matrix. This completes the entire data processing. In the first step, we need to detect the face. Determine if a face is included, and if so, determine the location and size of the face. Because the acquired images are all color images, skin color detection can be performed first. After detecting the skin color pixels, it is necessary to segment the possible face regions according to their chromatic similarity and spatial correlation, and at the same time, use the geometric features or grayscale features of the regions to verify whether it is a human face. Other objects with a color similar to skin tones are excluded. Here we use the classifier that comes with OpenCV to identify the face and draw a frame to display the face area. The second step is to grayscale the read image, because OpenCV processing is to process grayscale photos. The purpose of image processing is to facilitate the extraction of the eigenvalues of the face, and then to compare and identify, so this step is also very important. First, for the segmented face, since noise brings distortion and degradation, it is a necessary step to use filtering to remove noise before feature extraction. The second scale normalization, the idea is to transform the face images of different sizes into a unified standard size image to facilitate the extraction of face features. The third grayscale normalization, the research of face recognition generally takes grayscale images as the research object. For color face images, grayscale processing can be performed first. The fourth grayscale equalization, because the change of illumination during image acquisition easily leads to the image showing different degrees of brightness and darkness, so it is necessary to perform grayscale equalization processing on the face image. The function of gray level equalization is to enhance the overall contrast of the face image and make the gray distribution uniform to eliminate the influence of illumination changes. Step 3 Draw the box and save it. Face feature extraction and recognition are the two most critical issues in face recognition research. Face feature extraction, also known as face description, is a process of extracting various face features based on image preprocessing such as face detection, positioning, and normalization, which lays the foundation for face recognition and classification. After feature extraction is the face recognition classification process. Face recognition classification is the last step of the entire face recognition. Its main task is to compare the face data to be measured according to the extraction results of facial features, and judge according to the degree of similarity. Find out whether the face to be tested is the same person. The final result of face detection is shown in Figure 1. Figure 1. Face detection result The input of the core function of gender detection has two matrices, one is the sample data, the other is the sample label, where the sample data is the pixel value, the sample label is artificially set, we set 0 for girls and 1 for boys. First, the function requires us to manually enter several parameters, namely the number of iteration steps, the learning factor, and the number of hidden neurons. The number of iteration steps 3 will affect our final accuracy and the time required for learning. The learning factor is a parameter of the backpropagation algorithm, and the number of hidden neurons is related to the number of input pixels. The next step is to initialize the parameters. We have three layers in total, input layer, hidden layer and output layer. The number of neurons in the input layer is the number of photo pixels, the output is only one neuron, and the number of hidden layers is artificially set. Figure 2. Gender test results 5. Conclusion This article is based on the python language, learning 200 photos of boys and 200 photos of girls. Using a three-layer neural network, an input layer, a hidden layer and an output layer, the detection of face and gender is finally realized. The experimental results also show that the neural network can achieve good results in the fields of face recognition and gender recognition. The experiment shows that the gender can be successfully detected, and the experimental method in this paper is feasible and effective. References [1] Hu X . Research for Face Recognition Based on Gabor Wavelet and Sparse Representation. IEEE Computer Society, 2014. [2] Liu X M ,Yue L H ,Chen B, et al. Method of target recognition in remote sensing images based on parallel processing[J]. Journal of Computer Applications,2007,27(9):2123-2125. [3] Feng X.Edge detection algorithm based on BP neural networks in digital image[J]. Journal of Xi'an University of Science & Technology, 2005. [4] Tan T,Li J ,Wang K , et al.Target Threat Assessment Using Particle Swarm Optimization and BP Neural Network[C]// the 2019 3rd High Performance Computing and Cluster Technologies Conference. 2019. [5] Antennas Propagat.,to be published.A Strategy for Making Lane-change Decision Based on Improved Driving Risk Field and BP Neural Network [6] Li J ,Qin D.The mutation seagull algorithm optimizes the speech emotion recognition of BP neural network[C]// ICCBN 2021: 2021 9th International Conference on Communications and Broadband Networking. 2021.