zoukankan      html  css  js  c++  java
  • 用OpenCv来做人脸识别

    参考这篇文章: http://tech.idv2.com/2012/01/20/face-detection-with-python-opencv/

    python比较简单,只需安装 python-opencv 就行:

    $ sudo apt-get install python-opencv

    python的实现也很简单,参考:http://opencv.willowgarage.com/documentation/python/objdetect_cascade_classification.html

    代码:

    #!/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
    import cv
    from PIL import Image, ImageDraw

    def detectObjects(image):
    """Converts an image to grayscale and prints the locations of any faces found"""
    storage = cv.CreateMemStorage()

    cascade = cv.Load('haarcascade_frontalface_alt.xml')
    faces = cv.HaarDetectObjects(image, cascade, storage)

    result = []
    for (x,y,w,h),n in faces:
    result.append((x, y, x+w, y+h))

    return result

    def process(infile, outfile):

    image = cv.LoadImage(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')

    注:haarcascade_frontalface_alt.xml 可以在 https://github.com/talvarez/Face.js/tree/master/cascades 找到

    Node.js的话,有一个叫Face.js的项目,对OpenCv做了简单封装,地址是: https://github.com/talvarez/Face.js 
    这个需要先安装OpenCv库,目前版本是2.3.1,按照可以参考: UBUNTU 下编译安装opencv 2.3.1

    Face.js的具体的示例代码可以在Face.js里面的example里面找到,这里贴个简单的: 

    var Face = require('../build/default/face.node'),
    detector = new Face.init();

    detector.img = './samples/frame1.png';
    detector.maxsize = 20;
    detector.pathto = '../cascades/'

    detector.oncomplete = function(faces){
    console.log("I found " + faces.length + " faces");
    for(var i = 0; i < faces.length; i++) {
    console.log(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
    }
    };

    detector.run();


    最后贴张识别后的图:




    作者:QLeelulu Follow QLeelulu on Twitter
    出处:http://QLeelulu.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利
  • 相关阅读:
    <<网络是怎样连接的>>笔记第一章browser生成message
    豆知识( DNS; HTTP入门;网络协议)
    日期和时间的操作
    类型转换
    分组查询
    存储过程
    触发器
    表连接
    变量
    union以及一些扩展
  • 原文地址:https://www.cnblogs.com/QLeelulu/p/2343260.html
Copyright © 2011-2022 走看看