# Install on Terminal of MacOS #pip3 install -U opencv-python |
1_MacOS_Terminal.txt
########## Run Terminal on MacOS and execute ### TO UPDATE cd "YOUR_WORKING_DIRECTORY" |
0. Data
haarcascade_frontalface_default.xml
#Download this file from #https://github.com/shantnu/FaceDetect/ #to your working directory. |
Python files
########## Face DETECTION (not Recognition) with Python ########## #Run this py code on Python as follows: # #fd01.py (first argument: your image file for face detection) (cascades xml file) # #For instance, it goes like this. #fd01.py abba.png haarcascade_frontalface_default.xml # Reference # # Face Recognition with Python, in Under 25 Lines of Code # by Shantnu Tiwari # https://realpython.com/face-recognition-with-python/ # Download following files from https://github.com/shantnu/FaceDetect/ to your working directory. # # abba.png # haarcascade_frontalface_default.xml ##### Installing OpenCV import cv2 import sys import os ##### Understanding the Code # Get user supplied values # imageFile = sys.argv[1] #imageFile = "abba.png" cascFile = sys.argv[2] #cascFile = "haarcascade_frontalface_default.xml" #Get working directory workPath = os. getcwd() #print(workPath) imagePath = workPath + "/" + imageFile #print(imagePath) cascPath = workPath + "/" + cascFile print(cascPath) # Create the haar cascade faceCascade = cv2.CascadeClassifier(cascPath) # Read the image image = cv2.imread(imagePath) # Convert it to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Detect faces in the image # # (*1) faces = faceCascade.detectMultiScale( gray, #(*2) scaleFactor=1.1, #(*3) minNeighbors=5, #(*4a) minSize=(30, 30), #(*4b) #flags = cv2.cv.CV_HAAR_SCALE_IMAGE flags = cv2.cv2.CASCADE_SCALE_IMAGE ) # #(*1) detectMultiScale: a general function that detects objects #(*2) grayscale image #(*3) Since some faces may be closer to the camera, they would appear bigger than the faces in the back. The scale factor compensates for this. #(*4a) The detection algorithm uses a moving window to detect objects. minNeighbors defines how many objects are detected near the current one before it declares the face found. #(*4b) minSize, meanwhile, gives the size of each window. #print "Found {0} faces!".format(len(faces)) print ("Detected {0} faces!".format(len(faces))) # Draw a rectangle around the faces for (x, y, w, h) in faces: cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2) imageFile2 = "Face_Detected_" + imageFile cv2.imwrite(imageFile2,image) #cv2.imshow("Faces found", image) cv2.imshow("Faces detected", image) cv2.waitKey(0) cv.destroyAllWindows() #imageFile = "abba.png" #imageFile2 = "Face_Detected_" + imageFile #imagePath2 = workPath + "/" + imageFile2 #cv2.imwrite(imageFile2,image) |
Face_Detected_abba.png
Reference
by Shantnu Tiwari
https://realpython.com/face-recognition-with-python/
No comments:
Post a Comment