import cv2filename=‘E:\\tensorflow\\bu.jpg‘#图片的地址 # face_cascade=cv2.CascadeClassifier(‘C:\\anconda3\\pkgs\\opencv3-3.1.0-py35_0\\Library\etc\\haarcascades\\haarcascade_frontalface_default.xml‘)‘‘‘调用分类器‘‘‘ img=cv2.imread(filename)#读取照片face_cascade=cv2.CascadeClassifier(‘C:\\anconda3\\pkgs\\opencv3-3.1.0-py35_0\\Library\etc\\haarcascades\\haarcascade_frontalface_default.xml‘)gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)faces=face_cascade.detectMultiScale(gray,1.3,5)#识别人脸for (x,y,h,w) in faces: img=cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) #通过对角线在图片上画人脸框 # f = cv2.resize(gray[y:(y + h), x:(x + w)], (200, 200)) #重新设定图片的大小 f = cv2.resize(gray[y:(y + h), x:(x + w)], (200, 200))
cv2.namedWindow(‘faces Detected1‘) cv2.imshow(‘faces Detected!‘,f)#将图片显示出来 cv2.imwrite(‘E:\\tensorflow\\faces2.jpg‘,f) print("********") cv2.waitKey(2)
原文地址:https://www.cnblogs.com/MyUniverse/p/9465120.html
时间: 2024-11-03 09:10:57