def crop_xml(args, xml, sub_set_crop_path, instance_size=511): """ Dataset curation Parameters ---------- xml: str , xml sub_set_crop_path: str, xml crop path instance_size: int, instance_size """ cv2 = try_import_cv2() xmltree = ET.parse(xml) objects = xmltree.findall('object') frame_crop_base_path = os.path.join(sub_set_crop_path, xml.split('/')[-1].split('.')[0]) if not os.path.isdir(frame_crop_base_path): makedirs(frame_crop_base_path) img_path = xml.replace('xml', 'JPEG').replace('Annotations', 'Data') im = cv2.imread(img_path) avg_chans = np.mean(im, axis=(0, 1)) for id, object_iter in enumerate(objects): bndbox = object_iter.find('bndbox') bbox = [int(bndbox.find('xmin').text), int(bndbox.find('ymin').text), int(bndbox.find('xmax').text), int(bndbox.find('ymax').text)] z, x = crop_like_SiamFC(im, bbox, instance_size=instance_size, padding=avg_chans) cv2.imwrite(os.path.join(args.download_dir, frame_crop_base_path, '{:06d}.{:02d}.z.jpg'.format(0, id)), z) cv2.imwrite(os.path.join(args.download_dir, frame_crop_base_path, '{:06d}.{:02d}.x.jpg'.format(0, id)), x)
def crop_video(args, sub_set, video, crop_path, ann_base_path): """ Dataset curation Parameters ---------- sub_set: str , sub_set video: str, video number crop_path: str, crop_path ann_base_path: str, Annotations base path """ cv2 = try_import_cv2() video_crop_base_path = os.path.join(crop_path, sub_set, video) if not os.path.isdir(video_crop_base_path): makedirs(video_crop_base_path) sub_set_base_path = os.path.join(ann_base_path, sub_set) xmls = sorted(glob.glob(os.path.join(sub_set_base_path, video, '*.xml'))) for xml in xmls: xmltree = ET.parse(xml) objects = xmltree.findall('object') objs = [] filename = xmltree.findall('filename')[0].text im = cv2.imread(xml.replace('xml', 'JPEG').replace('Annotations', 'Data')) avg_chans = np.mean(im, axis=(0, 1)) for object_iter in objects: trackid = int(object_iter.find('trackid').text) bndbox = object_iter.find('bndbox') bbox = [int(bndbox.find('xmin').text), int(bndbox.find('ymin').text), int(bndbox.find('xmax').text), int(bndbox.find('ymax').text)] z, x = crop_like_SiamFC(im, bbox, instance_size=args.instance_size, padding=avg_chans) cv2.imwrite(os.path.join(args.download_dir, video_crop_base_path, '{:06d}.{:02d}.z.jpg'.format(int(filename), trackid)), z) cv2.imwrite(os.path.join(args.download_dir, video_crop_base_path, '{:06d}.{:02d}.x.jpg'.format(int(filename), trackid)), x)
def crop_img(img, anns, set_crop_base_path, set_img_base_path, instance_size=511): """ Dataset curation Parameters ---------- img: dic, img anns: str, video number set_crop_base_path: str, crop result path set_img_base_path: str, ori image path """ frame_crop_base_path = os.path.join(set_crop_base_path, img['file_name'].split('/')[-1].split('.')[0]) if not os.path.isdir(frame_crop_base_path): makedirs(frame_crop_base_path) cv2 = try_import_cv2() im = cv2.imread('{}/{}'.format(set_img_base_path, img['file_name'])) avg_chans = np.mean(im, axis=(0, 1)) for trackid, ann in enumerate(anns): rect = ann['bbox'] bbox = [rect[0], rect[1], rect[0] + rect[2], rect[1] + rect[3]] if rect[2] <= 0 or rect[3] <= 0: continue z, x = crop_like_SiamFC(im, bbox, instance_size=instance_size, padding=avg_chans) cv2.imwrite(os.path.join(args.download_dir, frame_crop_base_path, '{:06d}.{:02d}.z.jpg'.format(0, trackid)), z) cv2.imwrite(os.path.join(args.download_dir, frame_crop_base_path, '{:06d}.{:02d}.x.jpg'.format(0, trackid)), x)