分享

python人脸识别

 dinghj 2013-10-12

python人脸识别

编辑-倦容 发布于 2012-01-30 11:37:02,分类:开源项目,0评/4280阅
OpenCV的人脸检测功能在一般场合还是不错的。而ubuntu正好提供了python-opencv这个包,用它可以方便地实现人脸检测的代码。 写代码之前应该先安装python-opencv:$ sudo apt-get install python-opencv 具体原理就不多说了,可以参考一下这篇文章。直接上源代码。http:///it/article.php?id=4800&f=sinat

[Python]代码片段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# face_detect.py
# Face Detection using OpenCV. Based on sample code from:
# Usage: python face_detect.py
import sys, os
from opencv.cv import *
from opencv.highgui import *
from PIL import Image, ImageDraw
from math import sqrt
def detectObjects(image):
    """Converts an image to grayscale and prints the locations of any faces found"""
    grayscale = cvCreateImage(cvSize(image.width, image.height), 8, 1)
    cvCvtColor(image, grayscale, CV_BGR2GRAY)
    storage = cvCreateMemStorage(0)
    cvClearMemStorage(storage)
    cvEqualizeHist(grayscale, grayscale)
    cascade = cvLoadHaarClassifierCascade(
        \'/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml\',
        cvSize(1,1))
    faces = cvHaarDetectObjects(grayscale, cascade, storage, 1.1, 2,
        CV_HAAR_DO_CANNY_PRUNING, cvSize(20,20))
    result = []
    for f in faces:
        result.append((f.x, f.y, f.x+f.width, f.y+f.height))
    return result
def grayscale(r, g, b):
    return int(r * .3 + g * .59 + b * .11)
def process(infile, outfile):
    image = cvLoadImage(infile);
    if image:
        faces = detectObjects(image)
    im = Image.open(infile)
    if faces:
        draw = ImageDraw.Draw(im)
        for f in faces:
            draw.rectangle(f, outline=(255, 0, 255))
        im.save(outfile, "JPEG", quality=100)
    else:
        print "Error: cannot detect faces on %s" % infile
if __name__ == "__main__":
    process(\'input.jpg\', \'output.jpg\')

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多