Web Scraping and Facial Expression Analysis
11
|
How to Crop Images to Just Faces
Introduction
With a collection of images with varied styling, it might be preferable to crop the image for analysis. In this case, cropping the image to just human faces will be useful for analysis in future chapters. This also ensures we don’t include other humans who happen to be in the picture with the CEO.

Cropping Each Image in a List

The function FindFaces in Wolfram Language does just that. It uses deep learning algorithms to identity the portion of an image that corresponds to a human face and then crops the image to just the face. The Table command can be used to create a table of values, each of which is the cropped image. This calculation also excludes the Google logo that is imported in the previous web scrape:
In[]:=
allFaces=Flatten[Table[FindFaces[image,"Image"],{image,allPictures}]]
Out[]=

,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,

Wolfram Language can also use deep learning to identify specific people. FaceRecognize needs a list of tagged images and will then compare that training dataset against a new image to predict the identity of that person. In our case, this is a useful way to exclude any images of people associated with the CEO. We can first test this with a few of the images from the results above:
In[]:=
FaceRecognize
->"Margaret",
->"Other",

Out[]=
{Margaret}
Then Table can be used again to run the same identification on all the images. This confirms there is likely only one image that is not the CEO, which is the sixteenth image in the list above:
In[]:=
TableFaceRecognize
->"Margaret",
->"Other",image,{image,allFaces}
FaceRecognize
:Example faces at positions {{1}} of test images possibly are not human faces.
Out[]=
{{Margaret},{Margaret},{Margaret},{Margaret},{Margaret},{Margaret},{Margaret},{Margaret},{Margaret},{},{Margaret},{Margaret},{Margaret},{Margaret},{Margaret},{Other},{Margaret},{Margaret},{Margaret}}
To remove that image from the list, we can define the sixteenth element using Part as Nothing. Nothing means just that; it will remove the value entirely:
In[]:=
Part[allFaces,16]=Nothing
Out[]=
Nothing
Evaluating the symbol allFaces shows the current elements in the list, which no longer contains that outlier image:
In[]:=
allFaces
Out[]=

,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,


Summary

Table has been used in a few different examples so far and is a very convenient and flexible way to run the same calculation on every element in a list. This means we can process each piece of data in the same manner in a very straightforward way to try creative ideas.
DownloadNotebook»