<jupyter_start><jupyter_text>Create YOLOv3 training dataUse image having aspect ratio between [1.5, 3]<jupyter_code>np.random.seed(430)
rand_fns = list(fns)
np.random.shuffle(rand_fns)
rand_fns[:10]
with open("yolo_trn_anno.txt", "w") as text_file:
    for fn in tqdm(rand_fns[:-len(rand_fns)//10]):
        raw_fn = Path(fn).stem
        json_data = json.load(open(f"{path_json}/{raw_fn}.json", encoding="utf-8_sig"))
        img = cv2.imread(fn)[..., ::-1].astype(np.uint8)
        h, w, _ = img.shape

        if (w / h < 1.5) or (w / h > 3):
            continue

        rw, rh = 736 / w, 352 / h
        path = f"trn_data_yolov3/{raw_fn}.jpg"
        annotation = path
        resized_img = cv2.resize(img.copy(), (0,0), fx=rw, fy=rh)
        for c in json_data['chars']:
            y0, x0, y1, x1, y2, x2, y3, x3 = c['points']
            top, left = np.minimum(x0, x1), np.minimum(y0, y3)
            button, right = np.maximum(x2, x3), np.maximum(y1, y2)
            top = np.maximum(top, 0)
            left = np.maximum(left, 0)
            button = np.minimum(button, h)
            right = np.minimum(right, w)
            top = int(rh * top)
            left = int(rw * left)
            button = int(rh * button)
            right = int(rw * right)

            
            annotation += f"{left},{top},{right},{button},0"
            cv2.imwrite(f"trn_data_yolov3/{raw_fn}.jpg", resized_img[..., ::-1])
        text_file.write(annotation + "\n")
with open("yolo_val_anno.txt", "w") as text_file:
    for fn in tqdm(rand_fns[-len(rand_fns)//10:]):
        raw_fn = Path(fn).stem
        json_data = json.load(open(f"{path_json}/{raw_fn}.json", encoding="utf-8_sig"))
        img = cv2.imread(fn)[..., ::-1].astype(np.uint8)
        h, w, _ = img.shape

        if (w / h < 1.5) or (w / h > 3):
            continue

        rw, rh = 736 / w, 352 / h
        path = f"trn_data_yolov3/{raw_fn}.jpg"
        annotation = path
        resized_img = cv2.resize(img.copy(), (0,0), fx=rw, fy=rh)
        for c in json_data['chars']:
            y0, x0, y1, x1, y2, x2, y3, x3 = c['points']
            top, left = np.minimum(x0, x1), np.minimum(y0, y3)
            button, right = np.maximum(x2, x3), np.maximum(y1, y2)
            top = np.maximum(top, 0)
            left = np.maximum(left, 0)
            button = np.minimum(button, h)
            right = np.minimum(right, w)
            top = int(rh * top)
            left = int(rw * left)
            button = int(rh * button)
            right = int(rw * right)
            
            annotation += f" {left},{top},{right},{button},0"
            cv2.imwrite(path, resized_img[..., ::-1])
        text_file.write(annotation + "\n")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16843647015745147, 'fraction_numerical': 0.032954961552544855, 'mean_word_length': 1.9663409337676439, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '227089', 'license': 'MIT', 'n_tokens_mistral': 1079, 'n_tokens_neox': 1024, 'n_words': 216, 'path': 'generate_training_data_task2.ipynb', 'repo_name': 'awesometechno/keras-yolo3', 'size': '2731'}