Windows 环境使用 python opencv 面部识别

准备工作(下载) : 
Python 2.7 
Numpy for Python 2.7 
OpenCV 2.4 

 

安装:
1) 首先安装 Python 2.7. 缺省文件夹: C:\Python27\
2) 然后安装 Numpy. 全使用缺省值.
3) 现在安装 OpenCV.exe. 他将问你安装文件夹,填写 C:\. 它将解压全部文件到 C:\opencv\ .
4) 拷贝 C:\opencv\build\python\x86\2.7\ 的所有文件(多数时候只有cv2.pyd)到 C:\Python27\Lib\site-packages\
5) 打开你的 "Python IDLE" -> Python IDLE 输入: import cv2
如果一切OK,它将import cv2模块,否则将显示相应错误信息 

 

一个来自npinto的面部识别demo

cv2_detect.py

import cv2
import cv2.cv as cv


def detect(img, cascade_fn='haarcascade_frontalface_alt.xml',
           scaleFactor=1.1, minNeighbors=4, minSize=(20, 20),
           flags=cv.CV_HAAR_SCALE_IMAGE):

    cascade = cv2.CascadeClassifier(cascade_fn)
    rects = cascade.detectMultiScale(img, scaleFactor=scaleFactor,
                                     minNeighbors=minNeighbors,
                                     minSize=minSize, flags=flags)
    if len(rects) == 0:
        return []
    rects[:, 2:] += rects[:, :2]
    return rects

 

demo.py

import time
from cv2_detect import detect
import cv2
import cv2.cv as cv


def draw_rects(img, rects, color):
    for x1, y1, x2, y2 in rects:
        cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)


def demo(in_fn, out_fn):
    print ">>> Loading image..."
    img_color = cv2.imread(in_fn)
    img_gray = cv2.cvtColor(img_color, cv.CV_RGB2GRAY)
    img_gray = cv2.equalizeHist(img_gray)
    print in_fn, img_gray.shape

    print ">>> Detecting faces..."
    start = time.time()
    rects = detect(img_gray)
    end = time.time()
    print 'time:', end - start
    img_out = img_color.copy()
    draw_rects(img_out, rects, (0, 255, 0))
    cv2.imwrite(out_fn, img_out)


def main():
    demo('pic.jpg', 'pic.detect.jpg')


if __name__ == '__main__':
    main()

 

haarcascade_frontalface_alt.xml

您可能还喜欢...