python人脸识别,python人脸,OpenCV的人脸检测功
python人脸识别,python人脸,OpenCV的人脸检测功
OpenCV的人脸检测功能在一般场合还是不错的。而ubuntu正好提供了python-opencv这个包,用它可以方便地实现人脸检测的代码。 写代码之前应该先安装python-opencv:
$ sudo apt-get install python-opencv
具体原理就不多说了,可以参考一下这篇文章。直接上源代码。
#!/usr/bin/python# -*- coding: UTF-8 -*-# face_detect.py# Face Detection using OpenCV. Based on sample code from:# http://python.pastebin.com/m76db1d6b# Usage: python face_detect.py <image_file>import sys, osfrom opencv.cv import *from opencv.highgui import *from PIL import Image, ImageDrawfrom math import sqrtdef 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 resultdef 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" % infileif __name__ == "__main__": process('input.jpg', 'output.jpg')
检测结果:
来源:http://blog.charlee.li/face-detection-with-python-opencv/
相关内容
- python求txt文件中所有Z后面数字的最小值和最大值,,#
- python实现爬虫下载美女图片,,None
- common iter query,commoniter,def iquery(c
- python查看zip包中的文件及大小,python查看zip包中,#!/us
- 一个关于聊天机器人的调用,聊天机器人调用,#!/user/
- 使用python3.4解析xml文件(sax、dom、etree),python3.4etre
- 多线程下载豆瓣相册,多线程豆瓣相册,None
- Python写的网站C段扫描工具,pythonc段扫描工具,#!/usr/bi
- 使用python进行简单的文本处理,python文本处理,涉及到如
- python 微信爬虫,python信爬虫,# -*- coding
评论关闭