A Facial Recognition utility in a dozen of LOC

A Facial Recognition utility in a dozen of LOC (Lines Of Code)

CV (Computer Vision)

I have been soak myself in open sourced libraries, such as OpenCV. I gradually came to realize concepts such as Machine Learning , Deep Learning are not purely academic standing water. As a matter of fact, those elusive topics and certain pramatic use cases could coalesce in a amount of interesting products. For instance, in past couple of months, there were a hype of guess-ages-by-photo, below is one screenshot.

What a seducive one! Initially been attracted by such funky features, after second thoughts, I found at the heart of it is two cohensive parts, the first one is how to locate human faces from background and whole picture, consequently to have a ballpark age for the recongnized the faces. You may guess how difficult to codify a program to implement the 1st feature. Actually no need chunks of code, at here purely a dozen of lines of code are necessiated (actually only 10 lines of code, excluding space line and comments). I'd like to piggyback on such tiny utility to elaborate advanced topics of Computer Visions.

Faces recognition

Actually face recognition is not new to us, this feature can be wildly found in the feature of auto focus in DC (Digital Camera) and many main stream smart phone built-in cameras. Just like below photo. You can get a sense of how commonplace of face recognition , which is becoming a widely used technology around us.

Therotically speaking, face recognition is also called face detection, it's a type of technogloy/program to electorincally identify human frontal faces in digital images, such as photos, camera or suvillance. Further more, face detection is kind of objects detection in computer vision area. Which will locate object (e.g. human face) and get the size.

My '10 LOC program'

First of all, let's have some visual and concrete feeling of this program, below screenshot is the source code.

The whole program source code can be found at this github repository https://github.com/CloudsDocker/pyFacialRecognition . Please feel free to fork , check it out and have a try. I'll walk through this program one line by one line at this blog.

"You serious? This is all the problem, just aforementioned 10 lines?" Let's first take a look at the actul run output.

Here is the origional image

Below is the result of execution of this tiny utility

Please be advised the red rectangle around faces.


Souce Code

Prerequite

First of first, as you know, this program is composed by python,therefore, make sure you work station or laptop equiped with python, vesrion is irrelavant for this program.

In addition, this utility is built upon OpenCV (http://opencv.org/downloads.html), therefore please install this component as well. Just as its name suggested, it is an open source framework focus on computer vision related deep learning. This is one Intel lab built by Rusian, which is a very active community.

Particulary, if you are Mac users, it's recommended to use brew to setup OpenCV. Below is sample commands(The 1st line of following command may raise some errors, in that case please contact me via the link at the rear of this blog):

brew tap homebrew/science
brew install opencv

Upon completion of above scripts, you can execute following scripts to verify whether it's installed success or not, e.g. it means all fine if no exception/errors raised

>>> import cv2

Souce Code Dissection

Let's dissect file recognizeFace_loose_en.py as one example

import cv2,sys
  • To import library of OpenCV and python built-in system library, which is used to parse input arguments.
inputImageFile=sys.argv[1]
  • To read the 1st argument, which to be the file name of the image to be parsed, e.g. test.jpg
faceClassifier=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  • To load HAAR Casscade Classifier, the human face recognition cascade categorizer which shipped with OpenCV. Which will do the actual computation, logic to recognize and size human faces from any given images.

Expansion of computer vision knowledge

We stop here not reading further code, avoiding perplex you, I'll walk through certain CV topics pertaining to this blog. As for more deep concepts, please feel free to contact me or goole by yourself.

Classifier

In arena of computer vision and machine learning, a variaty of classifiers been and being built, to assemle special domain knowledge to recognize corresponding objects. For example, there are particular classifier to recognize cars, there are plane classifier, and classifiers to recognize smile, eyes, etc. For our case, we need a specific classifier help us to detect and locate human faces.

Conceps of objects recognize

Generally speaking辛藻,, to recognize one object (such as human faces) means finding and identifying objects in an image or video sequence. However, it's neccessitate tons of sample/specimen to train machine to learn, for instance, it's likely thousands of hundreds of digital images/video will be prepared as learning material, while all of specimen should be categorized to two mutax type, positive or negative. e.g. phots containss human face and ones without human face. When machine read one photo, it was told this is either a positive one or negative one, then machine could gradually analysys and induce some common facets and persist to files for future usages, e.g. when given a new photo, the machine can classify it whether it's a positive or negative. That's why it's called classifier.

Cascade

Your feeling is right, just as it's name suggrested, cascade implies propagating something. In this case, it's specifically means Cascade classifier. Intuitively the next question is why cascade is required? Let me try to articulate the underlying logic, as you know, at the heart of digital images, which is the raw material of computer vision, are pixel。For one CV process, it need to scan each pixel per pixel, while in contemporary world, size of image tend to incresing more than we expected, e.g. normall one photo taken by smart phone tend to contains millions of pixels. At the meanwhile, to fine tune and get a more accuate result of one object recognition, it tend to lots of classifiers to work from different point of views of the underlying photo. Therefore these two factors interwhirled together, the final number would be astronomical. Therefore, one innovative solution is cascade, in a nutshell, all classifiers will be splited to multiple layers, one photo will be examined by classifiers on 1st layer at the very begining, if failed, the whole CV can retain negative immediately, with fewest efforts and time cost, while majority of other classifiers won't be executed in actual. This should significantely accelerate the whole process of CV. This is similar to FF(Fail Fast) in other areas,severed for sake of running efficiency.

objImage=cv2.imread(inputImageFile)
  • To create one OpenCV image object by loading the input digital file via OpenCV
cvtImage=cv2.cvtColor(objImage,cv2.COLOR_BGR2GRAY)
  • Firstly, convert the digital colorful image to grayscale one, which easy the task to scan and analyse the image. Actually this is quite common in image analys area. e.g. this could eliminate those noisy pixel from the picture.
foundFaces=faceClassifier.detectMultiScale(cvtImage,scaleFactor=1.3,minNeighbors=9,minSize=(50,50),flags = cv2.cv.CV_HAAR_SCALE_IMAGE)
  • Call method detectMultiScale to recongnize object, i.e. human face in this case. The parameters overview as below:
  • scaleFactor: For a photo, particualy from selpie, some faces are shows bigger than rest of others, due to the distance between each faces and lens. Therefore this parameter is used to config the factor, please be advised this double should greater than 1.0
  • minNeighbors: Because it need to gradually scan the photo by a certain window, i.e. a rectangle. So this parameter is telling how many other object in the vacinity to be detected, before making final decision that it's positive or negative.
  • minSize:For aforementioend window, this parameter is setting the size of this rectangle.
print(" Found {} human faces in this image".format(len(foundFaces)))
  • To print how many faces detected, be reminded returned value is a list, each item is the actual position of every faces. Therefore, using len to print total number of ojects found.
for (x,y,w,h) in foundFaces:
    cv2.rectangle(objImage,(x,y),(x+w,y+h),(0,0,255),2)
  • Traverese all faces detected, please be noted returning object is consist of 4 parts, i.e. the horizontal and vertial position, width and height.
  • Consequently to draw a rectangle by an off-the-shelf method from OpenCV. Be advised (0,0,255) represents color of the rectangel. It use R/G/B mode, e.g. black is (0,0,0),white is (255,255,255)匠襟,etc. Well versed web programmer should be familiar with it.
cv2.imshow('Detected human faces highlighted. Press any key to exit. ', objImage)
cv2.waitKey(0)
  • To display this image via opencv provided method imshow, together with the rectangles we draw previously
  • The last one is one user hint, remind you can quit the applicaiton by press any key on the image display window

In summary

We've skimmed source codes and related knowledge. This is just one of bunch of use cases of this framework, hope this can bring some insights. ,such as hack of CAPTCHA, newly open sourced project form Yahoo, NSFW, Not Suitable for Work (NSFW)惭等,to detect images with pornagraphy, etc.

Finally阅爽,please be reminded all related source are open sourced at github repository https://github.com/CloudsDocker/pyFacialRecognition ,please fork and sync to your local disk, check it out and paly it.

git clone https://github.com/CloudsDocker/pyFacialRecognition.git
cd pyFacialRecognition
./run.sh

Any comments/suggestions, feel free to contact me

Contact me:

Reference

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市蛤售,隨后出現(xiàn)的幾起案子丁鹉,更是在濱河造成了極大的恐慌妒潭,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,576評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件揣钦,死亡現(xiàn)場離奇詭異雳灾,居然都是意外死亡,警方通過查閱死者的電腦和手機冯凹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評論 3 399
  • 文/潘曉璐 我一進店門谎亩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人宇姚,你說我怎么就攤上這事匈庭。” “怎么了浑劳?”我有些...
    開封第一講書人閱讀 168,017評論 0 360
  • 文/不壞的土叔 我叫張陵阱持,是天一觀的道長。 經(jīng)常有香客問我魔熏,道長衷咽,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,626評論 1 296
  • 正文 為了忘掉前任蒜绽,我火速辦了婚禮镶骗,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘躲雅。我一直安慰自己鼎姊,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,625評論 6 397
  • 文/花漫 我一把揭開白布相赁。 她就那樣靜靜地躺著相寇,像睡著了一般。 火紅的嫁衣襯著肌膚如雪噪生。 梳的紋絲不亂的頭發(fā)上裆赵,一...
    開封第一講書人閱讀 52,255評論 1 308
  • 那天,我揣著相機與錄音跺嗽,去河邊找鬼战授。 笑死,一個胖子當(dāng)著我的面吹牛桨嫁,可吹牛的內(nèi)容都是我干的植兰。 我是一名探鬼主播,決...
    沈念sama閱讀 40,825評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼璃吧,長吁一口氣:“原來是場噩夢啊……” “哼楣导!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起畜挨,我...
    開封第一講書人閱讀 39,729評論 0 276
  • 序言:老撾萬榮一對情侶失蹤筒繁,失蹤者是張志新(化名)和其女友劉穎噩凹,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體毡咏,經(jīng)...
    沈念sama閱讀 46,271評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡驮宴,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,363評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了呕缭。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片堵泽。...
    茶點故事閱讀 40,498評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖恢总,靈堂內(nèi)的尸體忽然破棺而出迎罗,到底是詐尸還是另有隱情,我是刑警寧澤片仿,帶...
    沈念sama閱讀 36,183評論 5 350
  • 正文 年R本政府宣布纹安,位于F島的核電站,受9級特大地震影響滋戳,放射性物質(zhì)發(fā)生泄漏钻蔑。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,867評論 3 333
  • 文/蒙蒙 一奸鸯、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧可帽,春花似錦娄涩、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,338評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至努隙,卻和暖如春球恤,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背荸镊。 一陣腳步聲響...
    開封第一講書人閱讀 33,458評論 1 272
  • 我被黑心中介騙來泰國打工咽斧, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人躬存。 一個月前我還...
    沈念sama閱讀 48,906評論 3 376
  • 正文 我出身青樓张惹,卻偏偏與公主長得像,于是被迫代替她去往敵國和親岭洲。 傳聞我的和親對象是個殘疾皇子宛逗,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,507評論 2 359

推薦閱讀更多精彩內(nèi)容