当前位置:网站首页>[logodetection dataset processing] (4) extract the logo area of each picture

[logodetection dataset processing] (4) extract the logo area of each picture

2022-06-10 14:22:00 ctrl A_ ctrl C_ ctrl V

Reference resources :【LogoDetection Dataset processing 】(3) Divide training sets into multiple folders by category

Previously, we have divided the training set into 50 A folder , Each folder has a category of pictures :

 Insert picture description here
In many cases , We need to extract the logo Area .

Original picture :
 Insert picture description here

Extracted logo Area :
 Insert picture description here
From the divided 50 And the picture extraction in the folder logo Area , obtain 50 Category logo We separate 50 In folders .

The code is as follows :

"""  Extract the... Of each picture logo Area . """""

import os
import json
import shutil
import cv2
from tqdm import tqdm


categories_list = [
    {
    
        "id": 1,
        "name": "bingdundun"
    },
    {
    
        "id": 2,
        "name": "sanyo"
    },
    {
    
        "id": 3,
        "name": "Eifini"
    },
    {
    
        "id": 4,
        "name": "PSALTER"
    },
    {
    
        "id": 5,
        "name": "Beaster"
    },
    {
    
        "id": 6,
        "name": "ON"
    },
    {
    
        "id": 7,
        "name": "BYREDO"
    },
    {
    
        "id": 8,
        "name": "Ubras"
    },
    {
    
        "id": 9,
        "name": "Eternelle"
    },
    {
    
        "id": 10,
        "name": "PerfectDiary"
    },
    {
    
        "id": 11,
        "name": "huaxizi"
    },
    {
    
        "id": 12,
        "name": "Clarins"
    },
    {
    
        "id": 13,
        "name": "Loccitane"
    },
    {
    
        "id": 14,
        "name": "Versace"
    },
    {
    
        "id": 15,
        "name": "Mizuno"
    },
    {
    
        "id": 16,
        "name": "Lining"
    },
    {
    
        "id": 17,
        "name": "DoubleStar"
    },
    {
    
        "id": 18,
        "name": "YONEX"
    },
    {
    
        "id": 19,
        "name": "ToryBurch"
    },
    {
    
        "id": 20,
        "name": "Gucci"
    },
    {
    
        "id": 21,
        "name": "LouisVuitton"
    },
    {
    
        "id": 22,
        "name": "CARTELO"
    },
    {
    
        "id": 23,
        "name": "JORDAN"
    },
    {
    
        "id": 24,
        "name": "KENZO"
    },
    {
    
        "id": 25,
        "name": "UNDEFEATED"
    },
    {
    
        "id": 26,
        "name": "BoyLondon"
    },
    {
    
        "id": 27,
        "name": "TREYO"
    },
    {
    
        "id": 28,
        "name": "carhartt"
    },
    {
    
        "id": 29,
        "name": "jierou"
    },
    {
    
        "id": 30,
        "name": "Blancpain"
    },
    {
    
        "id": 31,
        "name": "GXG"
    },
    {
    
        "id": 32,
        "name": "ledin"
    },
    {
    
        "id": 33,
        "name": "Diadora"
    },
    {
    
        "id": 34,
        "name": "TUCANO"
    },
    {
    
        "id": 35,
        "name": "Loewe"
    },
    {
    
        "id": 36,
        "name": "GraniteGear"
    },
    {
    
        "id": 37,
        "name": "DESCENTE"
    },
    {
    
        "id": 38,
        "name": "OSPREY"
    },
    {
    
        "id": 39,
        "name": "Swatch"
    },
    {
    
        "id": 40,
        "name": "erke"
    },
    {
    
        "id": 41,
        "name": "MassimoDutti"
    },
    {
    
        "id": 42,
        "name": "PINKO"
    },
    {
    
        "id": 43,
        "name": "PALLADIUM"
    },
    {
    
        "id": 44,
        "name": "origins"
    },
    {
    
        "id": 45,
        "name": "Trendiano"
    },
    {
    
        "id": 46,
        "name": "yiner"
    },
    {
    
        "id": 47,
        "name": "MonsterGuardians"
    },
    {
    
        "id": 48,
        "name": "fuerjia"
    },
    {
    
        "id": 49,
        "name": "IPSA"
    },
    {
    
        "id": 50,
        "name": "Schwarzkopf"
    }
]


#  Classified training set paths 
data_path = "TrainSet_50Classes"
annoations_path = "dataset/fewshotlogodetection_round1_train_202204/train/annotations/instances_train2017.json"
classdir_list = os.listdir(data_path)


#  Read annoations Of json file 
with open(annoations_path, 'r', encoding='utf-8') as f:
    annoations_dict = json.load(f)
images_list = annoations_dict["images"]
annotations_list = annoations_dict["annotations"]


#  Create outermost folder 
logoarea_dir = "LogoArea_50classes"
isExists_trainset_dir = os.path.exists(logoarea_dir)
if not isExists_trainset_dir:
    os.mkdir(logoarea_dir)


#  establish 50 Subfolders , as well as class_id A dictionary of folder names .
class_dict = {
    }
for i in range(len(categories_list)):
    dir_name = categories_list[i]["name"]
    dir_name_path = os.path.join(logoarea_dir, dir_name)
    class_dict[i + 1] = dir_name_path
    isExists = os.path.exists(dir_name_path)
    if isExists:
        shutil.rmtree(dir_name_path)
    os.makedirs(dir_name_path, exist_ok=True)


#  extract logo Area 
for classdir_name in tqdm(classdir_list,desc="process"):
    classdir_path = os.path.join(data_path, classdir_name)
    pic_list = os.listdir(classdir_path)
    logo_num = 0    #  Sequence number for picture naming 
    for pic_name in pic_list:
        pic_path = os.path.join(classdir_path, pic_name)
        image = cv2.imread(pic_path)
        for i in range(len(images_list)):
            if pic_name == images_list[i]["file_name"]:
                image_id = images_list[i]["id"]
                for j in range(len(annotations_list)):
                    if image_id == annotations_list[j]["image_id"]:
                        bbox = annotations_list[j]["bbox"]
                        x0, y0, w, h = bbox
                        logo_area = image[y0:y0 + h, x0:x0 + w]   #  extract logo Area 

                        #  Picture naming from  000  Start 
                        if logo_num < 10:
                            save_pic_name = classdir_name + "_00" + str(logo_num) + ".jpg"
                        elif logo_num < 100:
                            save_pic_name = classdir_name + "_0" + str(logo_num) + ".jpg"
                        else:
                            save_pic_name = classdir_name + "_" + str(logo_num) + ".jpg"
                        save_pic_path = os.path.join(logoarea_dir, classdir_name, save_pic_name)
                        cv2.imwrite(save_pic_path, logo_area)
                        logo_num += 1

Get one LogoArea_50classes The folder is as follows :

 Insert picture description here

原网站

版权声明
本文为[ctrl A_ ctrl C_ ctrl V]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101420132933.html