人脸检测 use Opencv and Python detection face
博客专区 > denua_ 的博客 > 博客详情
人脸检测 use Opencv and Python detection face
denua_ 发表于7个月前
人脸检测 use Opencv and Python detection face
  • 发表于 7个月前
  • 阅读 97
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: Face Detection using opencv and python
# coding=utf-8

import cv2
import sys

def detectFace(Im):

    img = Im
    grayIm = cv2.cvtColor(Im, cv2.COLOR_BGR2GRAY)
    face_cascade = cv2.CascadeClassifier("haarcascades/haarcascade_frontalface_alt.xml")

    faces = face_cascade.detectMultiScale(grayIm, 1.3, 5)
    result = []
    for (x, y, width, height) in faces:
        result.append((x, y, x + width, y + height))
    for (x1, y1, x2, y2) in result:
        print 'Ok'
        cv2.putText(img, "Find Face", (x1, y1 - 10), 0, 0.5, (0, 0, 255), 1)
        cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 3)
        cv2.imwrite('face.bmp', img)
    return img

def detecteye(Im):

    img = Im
    grayIm = cv2.cvtColor(Im, cv2.COLOR_BGR2GRAY)
    face_cascade = cv2.CascadeClassifier("haarcascades/haarcascade_eye.xml")

    faces = face_cascade.detectMultiScale(grayIm, 1.3, 5)
    result = []
    for (x, y, width, height) in faces:
        result.append((x, y, x + width, y + height))
    for (x1, y1, x2, y2) in result:
        cv2.putText(img, "Eye", (x1, y1 - 10), 0, 0.5, (0, 255, 0), 1)
        cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)

    return img

if __name__ == '__main__':

    cp = cv2.VideoCapture(0)

    while True:
        ret, frame = cp.read()      # ret: if read frame from cammera
        detectFace(frame)
        frame = detecteye(frame)
        cv2.imshow("Capture", frame)
        if cv2.waitKey(100) & 0xFF == ord('q'):
            break

    cp.release()
    cv2.destroyAllWindows()
    sys.exit(0)

 

我的小站: www.denua.cn

标签: Python
共有 人打赏支持
粉丝 0
博文 4
码字总数 3054
×
denua_
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: