text_plain_output_2.png text_plain_output_1.png
from google.colab import drive

from tqdm import tqdm

import glob

import json

import numpy as np

import os

import os
def is_in_ipython():
    """Is the code running in the ipython environment (jupyter including)"""
    program_name = os.path.basename(os.getenv('_', ''))
    if 'jupyter-notebook' in program_name or 'ipython' in program_name or 'jupyter' in program_name or ('JPY_PARENT_PID' in os.environ):
        return True
    else:
        return False
def is_in_colab():
    if not is_in_ipython():
        return False
    try:
        from google import colab
        return True
    except:
        return False
def is_in_kaggle_kernal():
    if 'kaggle' in os.environ['PYTHONPATH']:
        return True
    else:
        return False
if is_in_colab():
    from google.colab import drive
    drive.mount('/content/gdrive')
os.environ['TRIDENT_BACKEND'] = 'pytorch'
kaggle_kernal = None
if is_in_kaggle_kernal():
    os.environ['TRIDENT_HOME'] = './trident'
elif is_in_colab():
    os.environ['TRIDENT_HOME'] = '/content/gdrive/My Drive/trident'

class_names = ['未配戴口罩', '正確配戴口罩', '未正確配戴口罩', '配戴無防護能力口罩']
palette = [(0, 255, 255), (255, 255, 128), (255, 0, 255), (0, 255, 0)]
imgs = glob.glob('../input/maskedfacesdetection/imgs/*.*g')
path_dict = {}
for im_path in imgs:
    folder, filename = os.path.split(os.path.normpath(im_path))
    folder, file, ext = split_path(filename)
    path_dict[file] = im_path

annos = OrderedDict()
jsons = glob.glob('../input/maskedfacesdetection/tags/*.json')
images = []
boxes = []
for j in tqdm(range(len(jsons))):
    j_item = jsons[j]
    try:
        folder, file, ext = split_path(j_item)
        box_collection = []
        label_collection = []
        if file in path_dict and os.path.exists(path_dict[file]):
            img_path = path_dict[file]
            f = open(j_item, 'r', encoding='utf-8-sig')
            im = image2array(img_path)
            tagging = json.load(f)
            w = im.shape[1]
            h = im.shape[0]
            if len(tagging['outputs']) > 0 and len(tagging['outputs']['object']) > 0:
                folder, file, ext = split_path(img_path)
                annos[file + ext] = []
                for i in range(len(tagging['outputs']['object'])):
                    tag = tagging['outputs']['object'][i]
                    label = int(tag['name']) + 1
                    x1 = max(tag['bndbox']['xmin'], 0)
                    y1 = max(tag['bndbox']['ymin'], 0)
                    x2 = min(tag['bndbox']['xmax'], w)
                    y2 = min(tag['bndbox']['ymax'], h)
                    bbox = [x1, y1, x2, y2, label]
                    annos[file + ext].append(np.array([x1, y1, x2, y2, label]))
                    box_collection.append(bbox)
            box_collection = np.array(box_collection, dtype=np.float32)
            boxes.append(box_collection)
            images.append(img_path)
    except:
pickle_it('annos1.pkl', annos)

print(boxes[:20])
print('')
print(images[:20])