当前位置:   article > 正文

基础知识(七)opencv、python、人脸框检测_x,y,w,h=face

x,y,w,h=face

一、环境搭建

电脑系统:window7 64位

opencv版本:opencv 2.49

python版本:python 2.7

1、首先就是安装opencv,从官网下载.exe文件,然后直接安装。安装完opencv后,找到文件:cv2.pyd。这个文件,我安装完后,是放在:opencv\build\python\2.7\x64。因为我的电脑是64位所以选择x64下的文件cv2.pyd。

2、把cv2.pyd文件放到python安装目录里的:Lib\site-packages文件夹下。这样就OK了,接着就可以直接调用opencv库了。

二、人脸检测

  1. <span style="font-size:18px;">import numpy as np
  2. import cv2
  3. import cv2.cv as cv
  4. from matplotlib import pyplot as plt
  5. face_cascade = cv2.CascadeClassifier('C:\Program Files\opencv\sources\data\haarcascades\haarcascade_frontalface_default.xml')
  6. img = cv2.imread('113.jpg')
  7. gray = cv2.cvtColor(img,cv2.cv.CV_BGR2GRAY)
  8. divisor=8
  9. h, w =np.shape(img)[:2]
  10. minSize=(w/divisor, h/divisor)
  11. color = (0,0,0)
  12. faces = face_cascade.detectMultiScale(gray, 1.2, 2, cv2.CASCADE_SCALE_IMAGE,minSize)
  13. if len(faces)>0:
  14. for faceRect in faces:
  15. x, y, w, h = faceRect
  16. cv2.rectangle(img, (x, y), (x+w, y+h), color)
  17. roi = img[y:(h+y), x:(w+x)]
  18. cv2.imshow('img',img)
  19. cv2.imshow('facerect',roi)
  20. cv2.waitKey(0)
  21. cv2.destroyAllWindows()</span>
上面的cv2.CascadeClassifier函数的参数是.xml所在的路径。结果如下:



  1. import numpy as np
  2. import cv2
  3. import cv2.cv as cv
  4. from matplotlib import pyplot as plt
  5. def GetFileList(FindPath,FlagStr=[]):
  6. import os
  7. FileList=[]
  8. FileNames=os.listdir(FindPath)
  9. if len(FileNames)>0:
  10. for fn in FileNames:
  11. if len(FlagStr)>0:
  12. if IsSubString(FlagStr,fn):
  13. fullfilename=os.path.join(FindPath,fn)
  14. FileList.append(fullfilename)
  15. else:
  16. fullfilename=os.path.join(FindPath,fn)
  17. FileList.append(fullfilename)
  18. if len(FileList)>0:
  19. FileList.sort()
  20. return FileList
  21. def IsSubString(SubStrList,Str):
  22. flag=True
  23. for substr in SubStrList:
  24. if not(substr in Str):
  25. flag=False
  26. return flag
  27. def facedetect(newname,filepath,face_cascade):
  28. img = cv2.imread(filepath)
  29. print filepath
  30. gray = cv2.cvtColor(img,cv2.cv.CV_BGR2GRAY)
  31. divisor=8
  32. height, weight =np.shape(img)[:2]
  33. minSize=(weight/divisor, height/divisor)
  34. color = (0,0,0)
  35. faces = face_cascade.detectMultiScale(gray, 1.2, 2, cv2.CASCADE_SCALE_IMAGE,minSize)
  36. print faces
  37. if len(faces)>0:
  38. for faceRect in faces:
  39. x, y, w, h = faceRect
  40. img0=np.zeros((h+0.2*h,w+0.2*w,3))
  41. m,n,c=img0.shape
  42. miny=max(0,y-0.3*h)
  43. minx=max(0,(x-0.3*w))
  44. maxy=min(height,(y+1.3*h))
  45. maxx=min(weight,(x+1.3*w))
  46. roi=img[miny:maxy,minx:maxx]
  47. rectshape=roi.shape
  48. maxlenght=max(rectshape[0],rectshape[1])
  49. img0=np.zeros((maxlenght,maxlenght,3))
  50. img0[(maxlenght*.5-rectshape[0]*.5):(maxlenght*.5+rectshape[0]*.5),(maxlenght*.5-rectshape[1]*.5):(maxlenght*.5+rectshape[1]*.5)]=roi
  51. cv2.imwrite(newname,img0)
  52. face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  53. imgfile=GetFileList('face','.jpg')
  54. i=0
  55. for imgf in imgfile:
  56. name='face1/'+str(i)+'.jpg'
  57. i+=1
  58. facedetect(name,imgf,face_cascade)

人脸裁剪为矩阵,并填充

  1. import numpy as np
  2. import cv2
  3. import cv2.cv as cv
  4. from matplotlib import pyplot as plt
  5. def GetFileList(FindPath,FlagStr=[]):
  6. import os
  7. FileList=[]
  8. FileNames=os.listdir(FindPath)
  9. if len(FileNames)>0:
  10. for fn in FileNames:
  11. if len(FlagStr)>0:
  12. if IsSubString(FlagStr,fn):
  13. fullfilename=os.path.join(FindPath,fn)
  14. FileList.append(fullfilename)
  15. else:
  16. fullfilename=os.path.join(FindPath,fn)
  17. FileList.append(fullfilename)
  18. if len(FileList)>0:
  19. FileList.sort()
  20. return FileList
  21. def IsSubString(SubStrList,Str):
  22. flag=True
  23. for substr in SubStrList:
  24. if not(substr in Str):
  25. flag=False
  26. return flag
  27. def facedetect(newname,filepath,face_cascade):
  28. img = cv2.imread(filepath)
  29. gray = cv2.cvtColor(img,cv2.cv.CV_BGR2GRAY)
  30. divisor=8
  31. height, weight =np.shape(img)[:2]
  32. minSize=(weight/divisor, height/divisor)
  33. color = (0,0,0)
  34. faces = face_cascade.detectMultiScale(gray, 1.2, 2, cv2.CASCADE_SCALE_IMAGE,minSize)
  35. print faces
  36. if len(faces)>0:
  37. for faceRect in faces:
  38. x, y, w, h = faceRect
  39. img0=np.zeros((h+0.2*h,w+0.2*w,3))
  40. m,n,c=img0.shape
  41. miny=max(0,y-0.3*h)
  42. minx=max(0,(x-0.3*w))
  43. maxy=min(height,(y+1.3*h))
  44. maxx=min(weight,(x+1.3*w))
  45. roi=img[miny:maxy,minx:maxx]
  46. rectshape=roi.shape
  47. maxlenght=max(rectshape[0],rectshape[1])
  48. img0=np.zeros((maxlenght,maxlenght,3))
  49. img0[(maxlenght*.5-rectshape[0]*.5):(maxlenght*.5+rectshape[0]*.5),(maxlenght*.5-rectshape[1]*.5):(maxlenght*.5+rectshape[1]*.5)]=roi
  50. cv2.imwrite(newname,img0)
  51. face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  52. imgfile=GetFileList('ff')
  53. i=0
  54. for imgf in imgfile:
  55. name='ff_face/'+str(i)+'.jpg'
  56. i+=1
  57. facedetect(name,imgf,face_cascade)





**********************作者:hjimce   时间:2015.9.1  联系QQ:1393852684   地址:http://blog.csdn.net/hjimce 转载请保留本行信息********************



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/819625
推荐阅读
相关标签
  

闽ICP备14008679号