The teacher in the pattern recognition class left an experiment to implement face detection and tracking using OpenCV library programming in VC++ environment.
Then started to download opencv and vs2012, and then, configured several times or configuration is not successful, here have to spit under the Microsoft, the software to do so big, so difficult to use really good?
So there was an attempt to use python to accomplish the experimental task, and this is roughly how the process went:
First, configure the runtime environment:
Download the more recent versions of opencv and python, recommending opencv2. and python2.
Go directly to the official website to download on the ok, python installation all the way to next on the line, after running the downloaded file is basically a decompression process, choose a decompression path (try not to appear in Chinese), and then sit and wait for the decompression is complete.
Then from the path after opencv decompression to find (D:\My Documents\Downloads)\opencv\build\python\2.7\x86, () inside the part is your own installation path, where x86 corresponds to 32-bit machines, x64 represents 64-bit machines, of course, according to the actual situation of your machine to choose! You have to choose according to the actual situation of your machine, of course. Copy the contents of this path to the python2.7 module path C:\Python27\Lib\site-packages, python2.7 default installation in the C disk and directory.
At this point, open python, in the cmd under the type python, or directly open "All Programs - & gt; active state active python - & gt; Python Interactive Shell" can be.
Next type import cv2, something went wrong right? Why?
This is because there is no installation of numpy this python module, go to the numpy official website to download a relatively new version, because the latest version is generally the source code, you need to go to the command line to install, it is more troublesome, it is recommended to find an exe file. Note that in the link given by the official website, remember to look at the full name, the back of the general will suggest that the module in which the python version of the installation of the more harmonious, choose your own installation of the python version of the corresponding numpy module. After the download is completed, when installing the module, look at the python path given by the module is not correct, if it is correct, then next on the line, if it is not correct, it may be that your python is 2.7, but under the numpy for python 3.0.
At this point, go to import cv2 again, if nothing is output then the import is successful.
Simply orders of magnitude easier to configure than under vs, right?
After configuring your environment, get high with opencv!
Then create a new py file under pythonwin or idle (python gui) and enter the following code:
import cv2 import numpy as np ("test") cap=(0) success,frame=() classifier=("haarcascade_frontalface_alt.xml") # Make sure this xml file is in the same folder as the py file, otherwise change here to an absolute path, this xml file can be found under D:\My Documents\Downloads\opencv\sources\data\haarcascades. while success: success,frame=() size=[:2] image=(size,dtype=np.float16) image=(frame,.CV_BGR2GRAY) (image,image) divisor=8 h,w=size minSize=(w/divisor,h/divisor) faceRects=(image,1.2,2,cv2.CASCADE_SCALE_IMAGE,minSize) if len(faceRects)>0: for faceRect in faceRects: x,y,w,h=faceRect (frame,(x+w/2,y+h/2),min(w/2,h/2),(255,0,0)) (frame,(x+w/4,y+h/4),min(w/8,h/8),(255,0,0)) (frame,(x+3*w/4,y+h/4),min(w/8,h/8),(255,0,0)) (frame,(x+3*w/8,y+3*h/4),(x+5*w/8,y+7*h/8),(255,0,0)) ("test",frame) key=(10) c=chr(key&255) if c in ['q','Q',chr(27)]: break ("test")
Why is there no comment, I'm afraid you know that dir() and help() go better together on a rainy day, yo.
The function of this code is to process the video captured by the computer's camera so that it displays and tracks faces. The image below shows the effect of running it:
As a final note, this process is easy to say, but it's very easy to make mistakes, so I hope you can find out what's wrong and fix the mistakes yourself. If you can't solve it yourself, post the problem in the comments and let's work together to solve it and make progress.
The above mentioned is the whole content of this article, I hope you will enjoy it.