After the face recognition out of the range, it can be identified on the part of the grab. Grab a picture
portion of the graph is the method by crop pillow package implemented
We first learn to read package with pillow picture file, the syntax is:
For example, open test.jpg picture file, then save to img variables:
Then specify the scope of our crawl pictures with crop method, the syntax is:
For example, to crawl (50, 50) to (200, 200) and stored in the picture img2 variables:
Different image crawled down the face size may be inconsistent, in order to facilitate comparison graphics, images may be
adjusted to a fixed size. pillow package resize method may be implemented to reset the size of the image:
Grab the face area in the picture and save
First obtain a face area with OpenCV, then crop the method pillow package gripper face region and stored.
import cv2 from PIL import Image casc_path = "E:\\haarcascade_frontalface_default.xml" faceCascade = cv2.CascadeClassifier(casc_path) imagename = "F:\\pythonBase\\pythonex\\ch10\\media\\person1.jpg" image = cv2.imread(imagename) faces = faceCascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5, minSize=(30,30), flags = cv2.CASCADE_SCALE_IMAGE) count = 1 for (x,y,w,h) in faces: cv2.rectangle(image, (x,y), (x+w,y+h), (128,255,0), 2) filename = "E:\\" + str(count)+ ".jpg" image1 = Image.open(imagename) image2 = image1.crop((x, y, x+w, y+h)) image3 = image2.resize((200, 200), Image.ANTIALIAS) image3.save(filename) count += 1 cv2.namedWindow("facedetect") cv2.imshow("facedetect", image) cv2.waitKey(0) cv2.destroyWindow("facedetect")
import cv2 from PIL import Image casc_path = "E:\\haarcascade_frontalface_default.xml" faceCascade = cv2.CascadeClassifier(casc_path) imagename = "F:\\pythonBase\\pythonex\\ch10\\media\\person3.jpg" image = cv2.imread(imagename) faces = faceCascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5, minSize=(30,30), flags = cv2.CASCADE_SCALE_IMAGE) count = 1 for (x,y,w,h) in faces: cv2.rectangle(image, (x,y), (x+w,y+h), (128,255,0), 2) filename = "E:\\aa\\" + str(count)+ ".jpg" image1 = Image.open(imagename) image2 = image1.crop((x, y, x+w, y+h)) image3 = image2.resize((200, 200), Image.ANTIALIAS) image3.save(filename) count += 1 cv2.namedWindow("facedetect") cv2.imshow("facedetect", image) cv2.waitKey(0) cv2.destroyWindow("facedetect")
import cv2 from PIL import Image casc_path = "E:\\haarcascade_frontalface_default.xml" faceCascade = cv2.CascadeClassifier(casc_path) imagename = "F:\\pythonBase\\pythonex\\ch10\\media\\person8.jpg" image = cv2.imread(imagename) faces = faceCascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5, minSize=(30,30), flags = cv2.CASCADE_SCALE_IMAGE) count = 1 for (x,y,w,h) in faces: cv2.rectangle(image, (x,y), (x+w,y+h), (128,255,0), 2) filename = "E:\\aa\\bb\\" + str(count)+ ".jpg" image1 = Image.open(imagename) image2 = image1.crop((x, y, x+w, y+h)) image3 = image2.resize((200, 200), Image.ANTIALIAS) image3.save(filename) count += 1 cv2.namedWindow("facedetect") cv2.imshow("facedetect", image) cv2.waitKey(0) cv2.destroyWindow("facedetect")