0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Keras+Tensorflow で猫の毛色分類(サビ猫は雑誌の表紙を飾ったのか?)2

Last updated at Posted at 2018-09-04

もくじ

  1. 学習用画像を集める
  2. 猫のご尊顔を検出(ココ)
  3. 学習
  4. 猫雑誌の表紙画像を集めて判定

猫のご尊顏を検出

「1」で集めた画像を何も考えずKerasに突っ込んだら、画像の水増ししても accuracy 0.5 ほど。
ぐぬぬ...
   「そーだ!画像から猫だけ検出しよう!猫でない画像も弾かれるはず!」
 そこでとても有名なこの記事、素晴らしいです。

配布いただいているモデルと検出のプログラムを使い、猫のご尊顏が検出された領域をクリップして保存した。

  • imagefilename:入力画像ファイル
  • cascadefilename:上記でお配りいただいているOpenCVで読める学習済モデル.xmlファイル(cascade.xml)

検出条件は全てデフォルト、保存領域は検出結果より縦横10%づつ増量した(次の画像水増しで動かすため)。
下記のコードは、ご尊顏検出とクリップ・保存部分のみ。
呼び出しやらディレクトリ名やらなんやらよろしく追加改造する必要あり。

    import sys
    import cv2 as cv

    def detect(imagefilename, cascadefilename) :
       srcimg = cv.imread(imagefilename)
       filename, ext = os.path.splitext(os.path.basename(imagefilename))
       cascade = cv.CascadeClassifier(cascadefilename)
       objects = cascade.detectMultiScale(srcimg)
       i = 0
       for (x, y, w, h) in objects:
           nx = int(x - round(w / 10))
           if (nx < 0):
               nx = 0
           ny = int(y - round(h / 10))
           if (ny < 0):
               ny = 0
           nw = int(w + 2*(round(w / 10)))
           nh = int(h + 2*(round(h / 10)))
           detectedimg = srcimg[ny:(ny+nh),nx:(nx+nw)]
           newfilename = filename + '_' + str(i) + ext
           cv.imwrite(newfilename, detectedimg)
           i+=1

こんな感じになった(黒)。
スクリーンショット 2018-08-30 9.57.13.jpg
草やら靴やら虚無やら、誤検出が1/4くらい。
明らかに猫でないものはFinder上でアイコンを見ながらざっと削除した。
毛色14種類、元画像はそれぞれが600枚ほど、ご尊顏検出結果がほぼ半分、誤検出を捨てたら200〜250枚づつくらい。
次はいよいよ学習。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?