码迷,mamicode.com
首页 > 其他好文 > 详细

7人脸识别

时间:2020-01-12 15:14:28      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:desktop   video   inpu   变换   灰度   class   esc   调整   import   

1图片静态识别

import cv2 as cv
import numpy as np


def face_deftect_demo():
    #转化为灰度图
    gray =cv.cvtColor(src,cv.COLOR_BGR2GRAY)
    #加载特征数据
    face_detector = cv.CascadeClassifier(  "D:/sofeware/sofeware/python37/Lib/site-packages/cv2/data/haarcascade_frontalface_alt_tree.xml")
    #在多个尺度空间进行检测(图像名,向上或者向下变换尺度值(原图几倍),变换图清晰度低选择低的值,调整1.02为1.1可以加快速度)
    faces = face_detector.detectMultiScale(gray, 1.02, 2)
    #绘制矩形,提取长宽高,设置线的颜色,宽度
    for x, y, w, h in faces:
        cv.rectangle(src, (x, y), (x+w, y+h), (0, 0, 255), 2)
    cv.imshow("result", src)


print("--------- Python OpenCV Tutorial ---------")
src = cv.imread("C:/Users/wml/Desktop/wml/ym.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.namedWindow("result", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
face_deftect_demo()
cv.waitKey(0)
cv.destroyAllWindows()

2视频动态识别

import cv2 as cv
import numpy as np
def face_deftect_demo(image):
    gray =cv.cvtColor(image,cv.COLOR_BGR2GRAY)
    face_detector = cv.CascadeClassifier(  "D:/sofeware/sofeware/python37/Lib/site-packages/cv2/data/haarcascade_frontalface_alt_tree.xml")
    faces = face_detector.detectMultiScale(gray, 1.02, 1)
    for x, y, w, h in faces:
        cv.rectangle(image, (x, y), (x+w, y+h), (0, 0, 255), 2)
    cv.imshow("result", image)


print("--------- Python OpenCV Tutorial ---------")
capture = cv.VideoCapture(0)
cv.namedWindow("result", cv.WINDOW_AUTOSIZE)
while(True):
    ret, frame = capture.read()
    frame = cv.flip(frame, 1)#镜像变换
    face_deftect_demo(frame)
    c=cv.waitKey(10)
    if(c==27):#esc停止执行
        break
# cv.imshow("input image", src)
# face_deftect_demo()
cv.waitKey(0)
cv.destroyAllWindows()

7人脸识别

标签:desktop   video   inpu   变换   灰度   class   esc   调整   import   

原文地址:https://www.cnblogs.com/wml2018/p/12181681.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!