python人脸识别
发布时间:2020-12-17 17:22:41 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 ?OpenCV的人脸检测功能在一般场合还是不错的。而ubuntu正好提供了python-opencv这个包,用它可以方便地实现人脸检测的代码。 $ sudo apt-get install
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考
?OpenCV的人脸检测功能在一般场合还是不错的。而ubuntu正好提供了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,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,255)) im.save(outfile,"JPEG",quality=100) else: print "Error: cannot detect faces on %s" % infile if __name__ == "__main__": process('input.jpg','output.jpg')来源:http://blog.charlee.li/face-detection-with-python-opencv/ 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 将python模块拆分为子模块的正确方法是什么?
- python – Pyinstaller导入错误没有模块命名路径
- python – Pandas – 删除多个空列
- 某个网站居然要冲VIP!限制访问?利用Python爬取最爱看的小
- python – 如何计算最小的非零特征值
- 对javascript的一点点认识总结《javascript高级程序设计》读
- python – 将矩阵中的每个元素重复为一个新的矩阵
- 动态规划--0,1背包问题(再也不怕类似背包问题了)
- Python pexpect:fdpexpect pyserial == timeout不起作用?
- python – Splinter Selenium Phantomjs驱动程序在实例化时