Skip to content

On the previous post we had discussed on the notion of installing a python lib to be used to match faces from a known directory of images. This time, we will be working with identifying images as physical objects with a predefined known DB file rather than matching faces.

We have to install quite a few python libraries this time around in order to start matching objects in an given image with a database of predefined items.

pip3 install --user tensorflow
pip3 install --user numpy
pip3 install --user scipy
pip3 install --user opencv-python
pip3 install --user matplotlib
pip3 install --user h5py
pip3 install --user keras
pip3 install --user imageai
wget https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5

I've downloaded this particular 100MB file to the Download directory and pulled an arbitary banana image file as my test into a /var/tmp directory location.


#!/usr/bin/env python3
# Sample Image Prediction with AI and Tensorflow
# imgPrediction.py
################################################

from imageai.Prediction import ImagePrediction
import os

execution_path = os.getcwd()
prediction = ImagePrediction()
prediction.setModelTypeAsResNet()
prediction.setModelPath( execution_path + "/Downloads/resnet50_weights_tf_dim_ordering_tf_kernels.h5")
prediction.loadModel()


predictions, percentage_probabilities = prediction.predictImage("/var/tmp/Bananas.jpg", result_count=5)

for index in range(len(predictions)):
    print(predictions[index] , " : " , percentage_probabilities[index])
# results are as follows

 hobbit@Hobbit : ~ $ python3 imgPrediction.py
2020-02-03 22:30:42.018504: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2020-02-03 22:30:42.063861: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fad5b33bbf0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-02-03 22:30:42.063890: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
banana  :  98.45277667045593
lemon  :  0.7330755237489939
orange  :  0.5388087593019009
zucchini  :  0.07304142927750945
acorn_squash  :  0.052256841445341706

There is something with tensorflow where this CPU cant seem to run, apparently. Ignore those two warning messages and read the bottom 6 lines. Its comical that using this AI image library, it was able to identify this banana.jpg file with pretty high accuracy. again, i did the opposite with this software to match to see if this application would know a person's name or title by reading a jpg file with a facial image. After using one of the images from the previous post, it was able to identify a tie and a suit with 95% certainly but not the person's name nor their title. 

In term of facial recognition, image object AI when using a predefined DB has its own limitations when trying to ID a face. There doesnt seem to be a software, which can do both: facial recognition and ID-ing objects. 

You can find the step by step writeup with explainations from this author:
View at Medium.com

The best programming language used with Face Recognition and Image Object Recognition in my opinion is python3. There is literally a python library to use when comes to matching face images to other face images.

First, we will need to install one library then we will use git to clone a repository to play with the predefined image.

Using pip3 after installing your trusty python3 programming language. We can run the following commands.

pip3 install --user face_recognition

git clone https://github.com/bradtraversy/face_recognition_examples

As simple as cake, the above two lines of items would be needed to get started in working on using this Face Recognition application. When you run the face_recognition software it will take in two arguments. The zero location will be the application, the first location is the known images and lastly the second location will be the unknown facial images.


hobbit@Hobbit : ~/github/facerecog $ /Users/hobbit/Library/Python/3.7/bin/face_recognition /Users/hobbit/facerecog/img/known/ /Users/hobbit/facerecog/img/unknown/

WARNING: No faces found in ./img/known/Bananas.jpg. Ignoring file.
/Users/flo/Library/Python/3.7/lib/python/site-packages/PIL/Image.py:932: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images
"Palette images with Transparency expressed in bytes should be "
./img/unknown/obama2_0.jpg,Barack Obama
./img/unknown/gettyimages-459761948.jpg,no_persons_found
./img/unknown/d-trump.jpg,Donald Trump
./img/unknown/obama.jpeg,Barack Obama
./img/unknown/barack-obama-12782369-1-402.jpg,Barack Obama
./img/unknown/gates_lookalike.jpg,Bill Gates
./img/unknown/keanu-reeves-9454211-1-402.jpg,unknown_person
./img/unknown/sarahmullerEB358D55-EC16-36C7-7932-0C2F4E502D06.jpg,Steve Jobs
./img/unknown/xxx_d06_jordan_01_876569.jpg,Michael Jordan
./img/unknown/lead_720_405.jpg,Donald Trump
./img/unknown/stevejobs.png,Steve Jobs
./img/unknown/bill-gates-4.jpg,Bill Gates
./img/unknown/1523669923561.jpg,Donald Trump
./img/unknown/keanu-reeves-2000.jpg,unknown_person
./img/unknown/220px-Mark_Wahlberg_2017.jpg,unknown_person
./img/unknown/mark.jpg,unknown_person
./img/unknown/jordan-basketball.jpeg,Michael Jordan
./img/unknown/mark2.jpg,unknown_person
./img/unknown/bill-gates-750-563.jpg,Bill Gates
./img/unknown/steve-jobs---mini-biography.jpg,Steve Jobs


i tried inserting a image of a banana.jpg into this facial recognition application to see whether it would detect another banana01.jpg in the unknown directory. Interesting enough, this facial recognition is only able to detect faces. As you can see, this application detected an unknown image in the known directory which was not recognizable as a face. It then skipped over.

I felt as if facial recognition application will only work with facial images unlike where if one were to use an application to recognize a known physical object, one would then require other arsenals in the python lib. perhaps, Facial recognition is far different than object recognition as I digressed.