【Deep Learning】Keras-yolo3でアノテーション補助

前回まででアノテーションツールなどの紹介を行いました。しかし、アノテーション作業自体は地味で
時間がとてもかかります。そこで予めモデルを使って大まかな予測を行い、アノテーションをある程度行ってしまい、
アノテーションのズレなどを補正するような作業を行えば、素直に一からアノテーションするよりも大幅に時間短縮になります。

以下、コードを参考までに

import sys
sys.path.append('../')
import matplotlib.pyplot as plt

import os, glob

from src import kerasYolo3
from src.util import *

target_dir = '../../学習データ/追加データ/整理/image'

model_path = '../model_data/種類モデル/type-model.h5'
classes_path = '../class/種類クラス/class.txt' 

class_list = get_class_list(classes_path)
print(class_list)

model = kerasYolo3.ImageDetector(model_path, classes_path)

resultNum=0
error_list=[]

for type_path in glob.glob(os.path.join(target_dir, '*')):
    if not os.path.isdir(type_path):
        continue
        
    print(os.path.basename(type_path))
    
    annotation_path = type_path.replace('image', 'annotation')
    
    if not os.path.isdir(annotation_path):
        os.makedirs(annotation_path)
        print("folder made:",annotation_path)

    for veriety_path in glob.glob(os.path.join(type_path, '*')):
        annotation_path = veriety_path.replace('image', 'annotation')

        if not os.path.isdir(annotation_path):
            os.makedirs(annotation_path)
            print("folder made:",annotation_path)

        print("  "+os.path.basename(veriety_path))

        for img in glob.glob(os.path.join(veriety_path, '*')):
            try:
                r_img, results = model.detect_image(img)
                annotation_path = img.replace('image', 'annotation').replace('.jpg', '.txt').replace('.png', '.txt').replace('.jpeg', '.txt').replace('.gif', '.txt')

                with open(annotation_path, 'w', encoding='UTF-8') as f:

                    for result in results:
                        result = [str(r) for r in result]
                        f.write(",".join(result[2:]) + "," + str(class_list.index(result[0])) + "\n")
                        resultNum += 1
            except Exception as e:
                error_list.append(img + '\n' + str(e.__traceback__))

            print("    "+img)

    print("Done:",resultNum)
    print("Error:",len(error_list))
    print("Error list below")
    print(error_list)

KerasYoloクラスに関しては自作になります。次回時間あれば詳しく説明します。
簡単にyoloをパッケージ化した物になります。