Photoshpで顔認識

なんだか暖かくなってきましたね。もうすぐさくらが咲きます。お花見のシーズン到来です。おべんとうもってでかけなきゃ!

まあ、それはおいておいて今回はPhotoshopでの顔認識について書いてみました。続きは読み飛ばして頂いても良いかと思います。

えーと、いつの頃からは忘れましたが、PhotoshopにはFaceDetection系のライブラリが実装されています。しかしメニュー見てもそれらしいのはないですよね。これは色域選択の機能の一つとして組み込まれていて、スキントーンを選択したときのオプション用のライブラリなのです。
ちょっとやってみましょう。
色域設定パネルで選択をスキントーンとします。顔を検出にチェックするとこんな具合に顔のトーンを中心に選択範囲を生成してくれます。
しかしながら、いくらしぼっても顔だけを選択とかにはなりません。まあ、色域指定ですからね…でもね、バックの同系色ぐらいは排除してほしいものです。そんなこんなで、ここでもfaceDetecterの出番となります。

psFaceRecognition1.png

この状態でクイックマスクを適用してみましょう。

psFaceRecognition2.png

こうなります。ずいぶん要らない部分が選択されてるのがわかりますね。クイックマスクを解除して選択された状態からスクリプトを走らせます。
このスクリプト、当該ドキュメントから顔を検出してその部分と既存の選択範囲を合成して新たな選択範囲を生成してくれます。

psFaceRecognition3.png

結果はこんな感じです。
要らない部分が排除されたのがおわかり頂けると思います。
で、以下が今回使用したスクリプト。エクスターナルオブジェクト必須です。テストする場合は過去のOpenCV系の記事に目を通してください。

var f = File(activeDocument.path+”/”+app.activeDocument.name);
var cascade = “/haarcascades/haarcascade_frontalface_alt2.xml”;
try {
     var extObj = new ExternalObject(“lib:/faceDetecter64-1.0.2.framework”);
     } catch(e){
          alert(e);
          }
var pos = extObj.trackFace(f.fsName, cascade,1.1, 3, 15);
extObj.unload();
if(pos.length>1){
     var qm = activeDocument.channels.add();
     activeDocument.selection.clear();
     var ch = activeDocument.channels.add();
     for(var sl,i=0;i<pos.length;i+=4){
          sl = [ [pos[i],pos[i+1]],
                    [pos[i]+pos[i+2],pos[i+1]],
                    [pos[i]+pos[i+2],pos[i+1]+pos[i+3]],
                    [pos[i],pos[i+1]+pos[i+3]]];
          activeDocument.selection.select(sl,SelectionType.REPLACE);
          activeDocument.selection.clear();
          }
     activeDocument.selection.load(ch);
     ch.remove();
     activeDocument.selection.invert();
     var gy = new SolidColor();
     gy.gray.gray = 100;
     activeDocument.activeChannels = [qm];
     activeDocument.selection.fill(gy,ColorBlendMode.NORMAL, 100, false);
     activeDocument.selection.load(qm);
     qm.remove();
     }<pos.length;i+=4){ sl="[" [pos[i],pos[i+1]],="" [pos[i]+pos[i+2],pos[i+1]],="" [pos[i]+pos[i+2],pos[i+1]+pos[i+3]],="" [pos[i],pos[i+1]+pos[i+3]]];="" activedocument.selection.select(sl,selectiontype.replace);="" activedocument.selection.clear();="" }="" activedocument.selection.load(ch);="" ch.remove();="" activedocument.selection.invert();="" var="" gy="new" solidcolor();="" gy.gray.gray="100;" activedocument.activechannels="[qm];" activedocument.selection.fill(gy,colorblendmode.normal,="" 100,="" false);="" activedocument.selection.load(qm);="" qm.remove();="" }

最後に今回の画像ソースは
http://www.sxc.hu/photo/1251759

です。スタンダードリストリクションですから商用にも使える無料素材です。

おまけ、アイランドガールの顔の横にはでっかい顔があります。二階調化すると…

psFaceRecognition4.png
psFR.png

設定かえてトライしても絶対拾うので何かなって思ったらこんなのが居ましたw

ten_a

Graphic Designer, Scripter and Coder. Adobe Community Professional.

シェアする

コメントを残す