Advertisement
Guest User

Untitled

a guest
Jun 15th, 2021
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. import cv2
  2. import sys
  3. import os
  4. import glob
  5.  
  6.  
  7. i=0
  8. #Load Cascade
  9. cascade = cv2.CascadeClassifier("D:\lbpcascade_animeface-master\lbpcascade_animeface.xml")
  10.  
  11.  
  12. for file_directory in glob.glob("D:\lbpcascade_animeface-master\input\**\*.jpg", recursive=True):
  13.     print(file_directory)
  14.  
  15.     #Convert to grayscale
  16.     image = cv2.imread(file_directory)
  17.     gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  18.     gray = cv2.equalizeHist(gray)
  19.  
  20.     #Detect faces
  21.     faces = cascade.detectMultiScale(gray,
  22.                                      # detector options
  23.                                      scaleFactor = 1.1,
  24.                                      minNeighbors = 5,
  25.                                        minSize = (250, 250))
  26.    
  27.  
  28.     #Crop Faces
  29.     for (x, y, w, h) in faces:
  30.         cropped = image[y: y + h, x: x + w]
  31.         cv2.imwrite(("D:\lbpcascade_animeface-master\cropped\cropped"+str(i)+".png"), cropped)
  32.         print("Image "+str(i)+" cropped.")
  33.     i=i+1
  34.  
  35.     #Delete source file after cropping to save space
  36.     os.remove(file_directory)
  37.     print("File removed.")
  38.  
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement