anno points

在图片上点击后显示点和坐标的一种方法

Demo

python anno_point.py -i xxx.png
python anno_point.py -d ./im/
# press 'Esc' to exit.

Code

import cv2
import copy
import argparse
import numpy as np
from pathlib import Path

def parse_args():
    parser = argparse.ArgumentParser(
        "anno point, press 'Esc' to exit."
    )
    parser.add_argument(
        "-d", "--dir", required=False, default=None, help="Path to dir with PNG images"
    )
    parser.add_argument(
        "-i",
        "--imgs",
        nargs="+",
        required=False,
        default=[],
        help="Paths to PNG images",
    )
    return parser.parse_args()

def anno_im(im):
    x_list, y_list = [], []

    def on_EVENT_LBUTTONDOWN(event, x, y, flags, param):
        if event == cv2.EVENT_LBUTTONDOWN:
            x_list.append(copy.deepcopy(x)), y_list.append(copy.deepcopy(y))
            cv2.circle(im, (x, y), 1, (255, 0, 0), thickness=-1)
            cv2.putText(
                im,
                "{}, {}".format(x, y),
                (x, y),
                cv2.FONT_HERSHEY_PLAIN,
                1.0,
                (0, 0, 0),
                thickness=1,
            )
            cv2.imshow("image", im)

    cv2.namedWindow("image")
    cv2.setMouseCallback("image", on_EVENT_LBUTTONDOWN)
    cv2.imshow("image", im)
    while True:
        try:
            if cv2.waitKey(100) == 27:
                break
        except Exception:
            cv2.destroyWindow("image")
            break
    return x_list, y_list

def postprocess(x_list, y_list):
    # add your code here
    return None

def get_files(dir_path, extensions):
    all_files = []
    for ext in extensions:
        all_files.extend(Path(dir_path).glob(ext))
    return all_files

if __name__ == "__main__":
    args = parse_args()
    images_list = (args.imgs if args.dir is None else [str(img_path) for img_path in get_files(args.dir, ("*.png", "*.jpg","*.jpeg"))])
    for im_path in images_list:
        x_list, y_list = anno_im(cv2.imread(im_path, cv2.IMREAD_UNCHANGED))
        postprocess(x_list, y_list)

multiprocessing preprocess

Code

import multiprocessing as mp

def son_process(start_id, stop_id):
    # add your code here
    return None

if __name__ == '__main__':
    num_cores = int(mp.cpu_count())
    print("num cores: {}".format(num_cores))
    pool = mp.Pool(num_cores)
    params = []
    total_num = 46839
    for k in range(0, total_num - total_num // num_cores, total_num // num_cores):
        params.append((k, k + total_num // num_cores))
    results = [pool.apply_async(son_process, args=(t[0], t[1])) for t in params]
    results = [p.get() for p in results]
    son_process(params[-1][1], total_num)

Pytorch Dataloader

from torch.utils.data import Dataset

def XXX_dataloader(batch_size):
    train_data = XXX_Dataset(test=False)
    test_data = XXX_Dataset(test=True)
    train_queue = torch.utils.data.DataLoader(train_data, batch_size=batch_size, shuffle=True, num_workers=24)
    test_queue = torch.utils.data.DataLoader(test_data, batch_size=batch_size, num_workers=24)
    return train_queue, test_queue

class XXX_Dataset(Dataset):

    def __init__(self, test=False):

        self.test = test
        if test:
            # add your code here
            self.dataset_size = 1204
        else:
            # add your code here
            self.dataset_size = 46839

    def __len__(self):
        return self.dataset_size

    def __getitem__(self, idx):
        # add your code here
        return x, y

Time and Tide wait for no man.