<jupyter_start><jupyter_text>Modify to your own paths<jupyter_code>base_path = "/your/own/root_directory/"
imgs_path = list(sorted(os.listdir("/your/own/images/")))
annot_path = list(sorted(os.listdir("/your/own//annotations/")))
def generate_box(obj):
    xmin = int(obj.find('xmin').text)
    ymin = int(obj.find('ymin').text)
    xmax = int(obj.find('xmax').text)
    ymax = int(obj.find('ymax').text)
    return [xmin, ymin, xmax, ymax]

def generate_label(obj):
    if obj.find('name').text == "with_mask":
        return 1
    elif obj.find('name').text == "without_mask":
        return 2
    return 3

def generate_target(file): 
    with open(file) as f:
        data = f.read()
        soup = BeautifulSoup(data, 'xml')
        objects = soup.find_all('object')
        num_objs = len(objects)
        boxes = []
        labels = []
        for i in objects:
            boxes.append(generate_box(i))
            labels.append(generate_label(i))
        boxes = torch.as_tensor(boxes, dtype=torch.float32)
        labels = torch.as_tensor(labels, dtype=torch.int64)
        target = {}
        target["boxes"] = boxes
        target["labels"] = labels
        return target<jupyter_output><empty_output><jupyter_text>Plot 30 images with annotations<jupyter_code>N = 30
for ip,ap in zip(imgs_path[:N], annot_path[:N]):
  imp = base_path+"images/"+ip
  anp = base_path+"annotations/"+ap
  target = generate_target(anp)
  img = Image.open(imp).convert("RGB")
  fig,ax = plt.subplots(1)
  ax.imshow(img)
  for (box,label) in zip(target["boxes"],target["labels"]):
        xmin, ymin, xmax, ymax = box
        if label==1:
            rect = patches.Rectangle((xmin,ymin),(xmax-xmin),(ymax-ymin),linewidth=1,edgecolor='b',facecolor='none')
            print("with_mask")
        elif label==2:
            rect = patches.Rectangle((xmin,ymin),(xmax-xmin),(ymax-ymin),linewidth=1,edgecolor='g',facecolor='none')
            print("without_mask")
        else:
            rect = patches.Rectangle((xmin,ymin),(xmax-xmin),(ymax-ymin),linewidth=1,edgecolor='r',facecolor='none')
            print("mask_weared_incorrect")
        ax.add_patch(rect)
        ax.axis("off")
  print(ip)
  plt.show()<jupyter_output><empty_output><jupyter_text>Check data distribution<jupyter_code>result = {}
for ip,ap in zip(imgs_path, annot_path):
  anp = base_path+"annotations/"+ap
  target = generate_target(anp)
  label = str(list(set(target["labels"].cpu().numpy())))

  if label in result:
    result[label] +=1
  else:
    result[label] =1
print(result)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16122766122766122, 'fraction_numerical': 0.007381507381507382, 'mean_word_length': 3.439655172413793, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '16259', 'license': 'MIT', 'n_tokens_mistral': 922, 'n_tokens_neox': 899, 'n_words': 146, 'path': 'src/data_review.ipynb', 'repo_name': 'hwanseung2/Image_processing', 'size': '2574'}