Launch nano and specify a filename to use. Then import the 3 required Python libraries:
nano ~/FaceDetection.py
import numpy
import cv2
import urllib
Download and place our sample photo in the Edison’s web server directory with the new filename, in.jpg.
print("Downloading Images and Necessary Files")
urllib.urlretrieve(https://cdn.makezine.com/make/43/Intel_CES_Team.png, '/usr/lib/edison_config_tools/public/in.jpg')
Next, download the XML file that defines the parameters for the OpenCV facial-recognition algorithm. This file is also saved to the public directory of the Edison’s web server as haarcascade_frontal face_alt.xml.
urllib.urlretrieve('https://raw.githubusercontent.com/Itseez/opencv/master/data/haarcascades/haarcascade_frontalface_alt.xml', '/usr/lib/edison_config_tools/public/haarcascade_frontalface_alt.xml')
Import the photo using OpenCV and convert it to grayscale for use in the facial-recognition process:
img = cv2.imread('/usr/lib/edison_config_tools/public/in.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
Using the OpenCV libraries, create the facial-recognition algorithm and process the grayscale image:
faceCascade =
cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
faces =
faceCascade.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5,
minSize=(30, 30), flags = cv2.cv.CV_HAAR_SCALE_IMAGE)
The faces variable now contains an array of rectangular coordinates that surround each face that OpenCV found in the image. These coordinates are then used to draw a box around each face in the original color image, which you’ll save as a new file:
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.imwrite('in_facefound.png',img)
Finally, save the text file by pressing Ctrl-X on your keyboard. When prompted to save the file, type Y and Enter.