I've download a lot of images in one directory including “People” and “Landscape.”
I want to split the directory into two.
I'm looking for a Linux command line tool to filter images by face.
I've download a lot of images in one directory including “People” and “Landscape.”
I want to split the directory into two.
I'm looking for a Linux command line tool to filter images by face.
There is facedetect, which is a simple Python wrapper for OpenCV:
$ facedetect hasface.jpg
343 392 576 576
$ facedetect -q hasface.jpg;echo $?
0
$ facedetect noface.jpg
$ facedetect -q noface.jpg;echo $?
2
Installation in OS X:
curl https://raw.githubusercontent.com/wavexx/facedetect/master/facedetect>/usr/local/bin/facedetect;chmod +x /usr/local/bin/facedetect
brew tap homebrew/science;
brew install opencv;
sed -i '' s,/usr/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml,/usr/local/opt/opencv/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml, /usr/local/bin/facedetect
export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
You can sorta already do this with Picasa + gData API + GoogleCL.
As you know, Picasa has facial recognition, and the Google Data web API can access this. So the last piece of the puzzle is GoogleCL, which gives you access to Google services (via gData) from the command line.
I'm not sure how easy it is to do this out of the box. Perhaps you'll be the first person to attempt to access Picasa's facial recognition data via GoogleCL and you'll need to add support for it yourself. But GoogleCL already supports at least some Picasa API functions, so it shouldn't be too hard to figure out if you have some programming experience.
Of course, if you want a solution that doesn't require you to upload your images to the web, then this won't work, in which case, give pyFaces or OpenCV (Open Source Computer Vision) a try.
Face detection: face_detection tool from the face_recognition Python package
https://github.com/ageitgey/face_recognition
Install on Ubuntu 24.04:
sudo apt install imagemagick
virtualenv -p python3 .venv
. .venv/bin/activate
pip install \
face-recognition==1.3.0 \
git+https://github.com/ageitgey/face_recognition_models@e67de717267507d1e9246de95692eb8be736ab61 \
setuptools==72.2.0
Then you can check if running:
face_detection myimg.jpg
produces any output or not. If it doesn't, there are no faces, so:
if [ -n "$(face_detection myimg.jpg)" ]; then
echo hasface
else
echo noface
fi
Face recognition: face_recognition tool from the face_recognition Python package
Answering the question title rather than the question body. Their API is kind of cute. You make:
known/ with known face labelsunknown/ to search for those faces inand then just:
face_recognition known unknown
Example:
known
mkdir -p known
wget -O known/obama.jpg https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/President_Barack_Obama.jpg/615px-President_Barack_Obama.jpg
wget -O known/trump.jpg https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/Donald_Trump_official_portrait.jpg/606px-Donald_Trump_official_portrait.jpg
unknown
mkdir -p unknown
wget -O unknown/obama-biden.jpg https://upload.wikimedia.org/wikipedia/commons/8/80/Official_portrait_of_President_Obama_and_Vice_President_Biden_2009.jpg
wget -O unknown/trump-pence.jpg https://upload.wikimedia.org/wikipedia/commons/thumb/f/f0/Trump_on_China_-_Putting_America_First_%28November_2%2C_2020%29%2C_front_cover_%28cropped%29.png/1024px-Trump_on_China_-_Putting_America_First_%28November_2%2C_2020%29%2C_front_cover_%28cropped%29.png
wget -O unknown/trump-obama.jpg https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Barack_Obama_and_Donald_Trump.jpg/1024px-Barack_Obama_and_Donald_Trump.jpg
wget -O unknown/funeral.jpg https://upload.wikimedia.org/wikipedia/commons/thumb/c/c6/The_Funeral_of_President_George_H.W._Bush_%2831265101997%29.jpg/800px-The_Funeral_of_President_George_H.W._Bush_%2831265101997%29.jpg?20181206162026
and bam, it thinks Biden is Trump, but it correctly doesn't know Pence, and it finds both trump and obama in the funeral:
unknown/obama-biden.jpg,obama
unknown/obama-biden.jpg,trump
unknown/funeral.jpg,unknown_person
unknown/funeral.jpg,unknown_person
unknown/funeral.jpg,unknown_person
unknown/funeral.jpg,unknown_person
unknown/funeral.jpg,unknown_person
unknown/funeral.jpg,unknown_person
unknown/funeral.jpg,trump
unknown/funeral.jpg,obama
unknown/trump-pence.jpg,trump
unknown/trump-pence.jpg,unknown_person
unknown/trump-obama.jpg,obama
unknown/trump-obama.jpg,trump
deepface face recognition
https://github.com/serengil/deepface
pipx doesn't work: https://github.com/serengil/deepface/issues/1323 so:
virtualenv -p python3 .venv
. .venv/bin/activate
pip install deepface==0.0.93 tf_keras==2.17.0
Then:
deepface find known/trump.jpg unknown/
says:
identity hash target_x target_y target_w ... source_y source_w source_h threshold distance
0 unknown/trump-pence.jpg 0b37307c8e96b191fc25850b3f753da0a18cdd60 286 88 131 ... 181 249 249 0.68 0.432509 [1 rows x 12 columns]
and:
deepface find known/obama.jpg unknown/
says:
identity hash target_x target_y target_w target_h source_x source_y source_w source_h threshold distance
0 unknown/obama-biden.jpg 52db8b94ea067ba1a0b11183ebd164275c289dae 146 101 123 123 214 59 183 183 0.68 0.293305
1 unknown/obama-biden.jpg 52db8b94ea067ba1a0b11183ebd164275c289dae 419 146 106 106 214 59 183 183 0.68 0.579007
so it is quite wrong, as it missed trump in trump-obama.jpg and found obama twice in obama-biden.jpg. And neither was found in funeral.jpg.
Bibliography: