圆月山庄资源网 Design By www.vgjia.com

此程序为先调用opencv自带的人脸检测模型,检测到人脸后,再调用我自己训练好的模型去识别人脸,使用时更改模型地址即可

#!usr/bin/env python
import cv2
font=cv2.FONT_HERSHEY_SIMPLEX
cascade1 = cv2.CascadeClassifier("D:\\opencv249\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt_tree.xml")
cascade2 = cv2.CascadeClassifier("D:\\opencv249\\opencv\\sources\\data\\haarcascades\\xml.xml")
cap = cv2.VideoCapture(0)
while True:
 ret,frame = cap.read()
 gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
 rect = cascade1.detectMultiScale(gray,scaleFactor=1.3,minNeighbors=9,minSize=(50,50),flags = cv2.cv.CV_HAAR_SCALE_IMAGE)
 if not rect is (): 
  for x,y,z,w in rect:
   roiImg = gray[y:y+w,x:x+z]
   rect1 = cascade2.detectMultiScale(roiImg,scaleFactor=1.3,minNeighbors=9,minSize=(50,50),flags = cv2.cv.CV_HAAR_SCALE_IMAGE)
   if not rect1 is ():
    for (a,b,c,d) in rect1:
     print "rect",rect1
     cv2.putText(frame,'Chenym',(x,y), font, 2,(0,0,255),2)
     cv2.rectangle(frame,(x+a,y+b),(x+a+c,y+b+d),(0,0,255),2)
 cv2.imshow('frame',frame)
 if cv2.waitKey(1) &0xFF == ord('q'):
  break
cap.release()
cv2.destroyAllWindows()

以上这篇python 用opencv调用训练好的模型进行识别的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

标签:
python,opencv

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com