Python configures opencv for Face Detection and pythonopencv

Source: Internet
Author: User

Python configures opencv for Face Detection and pythonopencv

In the pattern recognition class, the teacher left an experiment and used OpenCV library programming in the VC ++ environment to implement face detection and tracking.

Then we started to download opencv and vs2012. After that, the configuration was still unsuccessful several times. Here we had to talk about Microsoft. Is it so difficult to use the software?

So I tried to complete the experiment task using python. The process is like this:

First, configure the runtime environment:

Download the latest versions of opencv and python. We recommend opencv2.4.X and python2.7.X.

Directly go to the official network to download it. python is installed along with next. after the opencv.exe file is run, it is basically a decompression process. You can select a decompression path (do not show Chinese characters as much as possible) and press it to decompress it.

Find (D: \ My Documents \ Downloads) \ opencv \ build \ python \ 2.7 \ x86, () from the decompressed path of opencv ,() the content is your own installation path. x86 corresponds to 32-bit machines, and x64 represents 64-bit machines. Of course, you have to select the appropriate one based on your actual situation. Copy the cv2.pyd in this path to the module path C: \ Python27 \ Lib \ site-packages in python2.7. python2.7 is installed in the C drive and directory by default.

Open python, enter python in cmd, or directly open "All Programs-> active state active python-> Python Interactive Shell.

Input import cv2 next. An error occurred, right? Why?

This is because the python module numpy is not installed. Download a relatively new version from the official numpy website, because the latest version is generally the source code and needs to be installed in the command line, which is troublesome, we recommend that you find an exe file. Note: In the link provided on the official website, remember to check the full name, and the following will usually prompt which python version of this module is installed in a harmonious manner, select the numpy module corresponding to the python version you have installed. After the download is complete, check whether the python path provided by this module is correct. If yes, click next. If not, Your python is 2.7, but numpy for python 3.0.

Next, import cv2 again. If there is no output, the import is successful.

It's almost several orders of magnitude easier than vs's configuration, right?

After the environment is configured, follow opencv!

 

Create a new py file in pythonwin or idle (python gui) and enter the following code:

Import cv2
Import numpy as np
Cv2.namedWindow ("test ")
Cap = cv2.VideoCapture (0)
Success, frame = cap. read ()
Classifier = cv2.CascadeClassifier ("haarcascade_frontalface_alt.xml") # Make sure that the xml file is in the same folder as The py file. Otherwise, change it to the absolute path. The xml file can be in D: \ My Documents \ Downloads \ opencv \ sources \ data \ haarcascades.

While success:
Success, frame = cap. read ()
Size = frame. shape [: 2]
Image = np. zeros (size, dtype = np. float16)
Image = cv2.cvtColor (frame, cv2.cv. CV_BGR2GRAY)
Cv2.equalizeHist (image, image)
Divisor = 8
H, w = size
MinSize = (w/divisor, h/divisor)
FaceRects = classifier. detectMultiScale (image, 1.2, 2, cv2.CASCADE _ SCALE_IMAGE, minSize)
If len (faceRects)> 0:
For faceRect in faceRects:
X, y, w, h = faceRect
Cv2.circle (frame, (x + w/2, y + h/2), min (w/2, h/2), (255, 0 ))
Cv2.circle (frame, (x + w/4, y + h/4), min (w/8, h/8), (255, 0 ))
Cv2.circle (frame, (x + 3 * w/4, y + h/4), min (w/8, h/8), (255, 0 ))
Cv2.rectangle (frame, (x + 3 * w/8, y + 3 * h/4), (x + 5 * w/8, y + 7 * h/8 ), (255, 0, 0 ))
Cv2.imshow ("test", frame)
Key = cv2.waitKey (10)
C = chr (key & 255)
If c in ['Q', 'Q', chr (27)]:
Break
Cv2.destroyWindow ("test ")

 

Why is there no comment? You may know that dir () and help () are better suited to rainy days.

The function of this Code is to process the video captured by a computer camera so that it can display and track the face. Is the running effect:

 

 

Finally, this process is simple, but it is easy to make mistakes. I hope you can find the cause of the error and solve the problem yourself. If you cannot solve the problem yourself, you may wish to post it in your comments.

 

 

The above resources come from the Internet and are only for personal use. If your copyright is infringed, please contact me. I will delete the infringing part immediately. Thank you!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.