In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "Python how to realize AI Face Recognition." In daily operation, I believe many people have doubts about how Python realizes AI Face Recognition. The editor consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts of "Python how to realize AI Face Recognition"! Next, please follow the small series to learn together!
1. Buy a small computer
Since you want to do Face Recognition, you have to find a small computer with a camera. First of all, the price is cheap. After a simple search, there are basically the following options:
Raspberry Pi 4: ARM system, ecological good. The price is right,$55. CPU is the best among the three, with a computing power of 0.1TFLOPS.
K210: RISC-V (non-ARM), the price is the most affordable, 299 yuan. 0.8 TOPS
Jetson Nano: ARM system, more expensive than the Raspberry Pi 4, but with an extra NVIDIA GPU (of course, the cheap GPU), the price of 99 dollars. Calculate power 0.47TFLOPS
Among these three, considering that Face Recognition should have more AI attributes, isn't it more fragrant to do AI reasoning with GPU, so I chose NVIDIA's Jetson Nano development board (mainly to get started with NVIDIA's GPU faction first, who calls NVIDIA more fragrant now).
2. Start the system
Here you need to first brush the "system image" into the tf card, then insert the tf card into the development board, and then boot up. There are two points to note when starting:
Jumper cap, need to plug in (otherwise the power point does not light up).
The first boot will jam, you need to restart once.
After starting, I found that it was an Ubuntu system with an interface, connected to the mouse + keyboard, which was the most familiar small computer.
Connect to the network, set up the domestic Ubuntu source, install the jtop command (because nano cannot type nvidia-smi command).
CUDA is a compiler.
Since you chose NVIDIA's GPU development board, just know what CUDA is.
3.1 Write a CUDA program
It can be seen that C language, a total of two functions. A main function, a useCUDA function.
To run, you need to compile first. It is interesting to find that instead of using gcc, we use nvcc compiler.
Jetson Nano's image has nvcc installed, so you can use it directly, but you need to set the path before using it.
export CUDA_HOME=/usr/local/cuda-10.0export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64:$LD_LIBRARY_PATHexport PATH=/usr/local/cuda-10.0/bin:$PATH
After setting, you can type the nvcc command.
So start compiling:
nvcc -o main.out main.cu foo.cu (same as gcc compilation)
Run: ./ main.out. Ah, CUDA is a compiler ah (compiled to run for the GPU program).
3.2 What's CUDA doing?
Ordinary programs are compiled to run for CPU; write a program, want to run for GPU, you have to use cuda compiler. After all, our GPU is a little weaker, there are also 128 cores, running this simple cuda program is OK.
In addition, CUDA also provides some ready-made GPU operation functions, such as: matrix multiplication, matrix transpose and the like. CUDA can only be used with NVIDIA GPUs for complex parallel computing using GPUs. Then a lot of AI frameworks are based on CUDA, so running a cuda program to help understand is good.
4, detection camera function OK
Here you need to use the nvgstcapture-1.0 command, after checking, the letter gst is originally an abbreviation of Gstreamer.
Directly knock:
nvgstcapture-1.0
I found that I could turn on the camera.
4.1 Gstreamer
After searching, I found a pipeline framework for audio and video stream processing.
For example, the pipe character of the Shell command is:|
To avoid conflicts, Gstreamer's pipe symbol uses a similar one: ! Symbol.
5, the face can be recognized normally
Copy a face_detect.py file.
Run directly:
python ./ face_detect.py
You can recognize faces if you find them, amazing... (Well, it's just that the image is upside down)
So I simply looked at the code (I can't Python, but I can probably understand it) and found that there is an image mode parameter "flip_method=0". I changed it to 6 and found that the image turned around normally.
Looking at it again, I found that getting the pictures taken by the camera was still achieved through Gstreamer.
Is Opencv the Hidden Boss?
Through the above chapters, I found that there are only a few lines of code in total. How can I recognize faces? Although Python is not familiar, but fortunately the code is small, a closer look: found that the main function is to call opencv can recognize the face, that means opencv still have to understand.
You look at the code for recognizing faces, there are only 10 lines in total, which is simple:
6.1 Haar classifier
Search again, found that the Face Recognition here is realized by "Haar Classifier." Learning, belonging to the category of machine learning, without convolutional neural networks.
7. Face Recognition reproduced on Windows
Since I can recognize faces through opencv, I don't need a camera. Can I run away with pictures directly? So try installing an opencv on Windows.
7.1 install Python
Go to https://www.python.org/ to download the latest Python and install Python. Of course, the execution order is py.
7.2 pip command
Found pip command, or not. Find the original in:
C:\Users\tsjsdbd\AppData\Local\Programs\Python\Python39\Scripts
under this directory.
So add this path to the local environment variable:
So pip ordered ok
7.3 Set pip domestic source vi <$/pip/pip. ini
Then set the content:
[global]index-url = https://pypi.tuna.tsinghua.edu.cn/simple7.4 Install opencv package pip install opencv-python
This command installs the numpy package at the same time.
Ps: If agents are needed. set about to
export http_proxy=http://proxy: port export https_proxy=http://proxy: port 7.5 reproduce Haar Face Recognition
Download a jpg photo with a human face, here assumed to be face.jpg
detect.py code is as follows:
import cv2# load modeldetector=cv2.CascadeClassifier('C:/Users/t00402375/AppData/Local/Programs/Python/Python39/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml')src = cv2.imread("./ face.jpg")cv2.namedWindow("image", cv2.WINDOW_AUTOSIZE);# detectgray=cv2.cvtColor(src,cv2.COLOR_BGR2GRAY)faces=detector.detectMultiScale(gray,1.2,5)# boxfor x,y,w,h in faces: cv2.rectangle(src,(x,y),(x+w,y+h),(255,0,0),2) # showcv2.imshow("image", src);cv2.waitKey(0);cv2.destroyAllWindows();
Just run it.
py detect.py
Get results:
It was found that it could indeed be used.
8. Face Recognition through Neural Network
Now video image recognition, generally go CNN, so we have to play it again. Jetson development board, its own set of jetson-inference reasoning project, is used to run GPU reasoning.
At this point, the study of "How Python realizes AI Face Recognition" is over, hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
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.