LoginSignup
1
0

More than 3 years have passed since last update.

自作の顔検出APIを作成しIBM Cloudで公開する

Last updated at Posted at 2019-09-18

OpenCVを使うと深層学習ベースの顔検出を実装することが可能です。本記事ではこちらの記事にあるコードをベースに、画像から人物の顔を検出し、性別、年齢を特定するAPIを作成し、IBM Cloudに公開する手順を紹介します。

1. コードおよびモデルの取得

以下からコードを取得します。

$ git clone https://github.com/schiyoda/FaceDetection.git

性別および年齢推定のモデルは前述の記事の中に下図の通りリンクがありますので、コードと同じフォルダにダウンロードします。

image.png

2. ローカルで実行

以下のコマンドで実行可能です。

$ python run_server.py 
 * Flask starting server...
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

顔検出は画像ファイルあるいは画像のURLをPOSTすることで実行可能です。

  • 画像ファイルをPOST
$ curl -X POST -F img_file=@pexels-photo-1470110.jpeg http://0.0.0.0:5000/detectFace
  • 画像のURLをPOST
curl -X POST -F url=https://images.pexels.com/photos/1470110/pexels-photo-1470110.jpeg http://0.0.0.0:5000/detectFace

いずれの方法でも以下のような応答が返ってきます。

{
  "faces": [
    {
      "age": {
        "confidence": "0.996", 
        "max": "12", 
        "min": "8"
      }, 
      "face_location": {
        "height": 505, 
        "left": 1789, 
        "top": 1023, 
        "width": 415
      }, 
      "gender": {
        "confidence": "1.000", 
        "gender": "Male"
      }
    }, 
    {
      "age": {
        "confidence": "0.995", 
        "max": "2", 
        "min": "0"
      }, 
      "face_location": {
        "height": 444, 
        "left": 1014, 
        "top": 1057, 
        "width": 380
      }, 
      "gender": {
        "confidence": "1.000", 
        "gender": "Male"
      }
    }, 
    {
      "age": {
        "confidence": "0.999", 
        "max": "32", 
        "min": "25"
      }, 
      "face_location": {
        "height": 526, 
        "left": 1402, 
        "top": 463, 
        "width": 413
      }, 
      "gender": {
        "confidence": "0.943", 
        "gender": "Female"
      }
    }
  ]
}

コードには含まれませんが、OpenCVで可視化すると以下のような結果が得られます。

image.png

3. IBM Cloudにデプロイ

IBM Cloudの Cloud Foundry アプリとしてデプロイするには以下のようにします。

$ ibmcloud login
$ ibmcloud target --cf
$ ibmcloud app push アプリ名

デプロイ後は以下のように実行可能です。

curl -X POST -F img_file=@sample1.jpg http://アプリ名.mybluemix.net/detectFace 
{"faces":[{"age":{"confidence":"0.983","max":"12","min":"8"},"face_location":{"height":195,"left":677,"top":384,"width":160},"gender":{"confidence":"1.000","gender":"Male"}},{"age":{"confidence":"0.701","max":"6","min":"4"},"face_location":{"height":171,"left":383,"top":400,"width":146},"gender":{"confidence":"1.000","gender":"Male"}},{"age":{"confidence":"0.987","max":"32","min":"25"},"face_location":{"height":199,"left":530,"top":177,"width":158},"gender":{"confidence":"0.992","gender":"Female"}}]}

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0