def _process_image(self,xml_file,img_file): if not os.path.exists(img_file): return None,None,None,None,None,None,None if self.image_preprocess is not None: img = wmli.imread(img_file) img = self.image_preprocess(img) image_data = wmli.encode_img(img) else: image_data = tf.gfile.FastGFile(img_file, 'rb').read() shape, _bboxes, _labels_text, _difficult, _truncated,_ = odu.read_voc_xml(xml_file, adjust=None) _labels = self.labels_text_to_labels(_labels_text) bboxes = [] labels_text = [] difficult = [] truncated = [] labels = [] for data in zip(_bboxes,_labels,_labels_text,_difficult,_truncated): if self.category_id_filter(data[1]): bboxes.append(data[0]) labels.append(data[1]) labels_text.append(data[2]) difficult.append(data[3]) truncated.append(data[4]) if len(labels) == 0: #print(f"Ignore {name}.") return None,None,None,None,None,None,None return image_data, shape, bboxes, labels, labels_text, difficult, truncated
def trans_dir(src_dir, out_dir): sub_dirs = wmlu.recurse_get_subdir_in_dir(src_dir) pattern = "img_{:05d}.jpg" wmlu.create_empty_dir(out_dir, remove_if_exists=False) for sd in sub_dirs: if sd[-1] == "/": sd = sd[:-1] rsd = osp.join(src_dir, sd) files = glob.glob(osp.join(rsd, "*.jpg")) if len(files) == 0: continue save_name = osp.join(out_dir, sd + f"_{data_type}_{len(files)}.np") save_dir_name = osp.dirname(save_name) wmlu.create_empty_dir(save_dir_name, remove_if_exists=False) all_frames = [] for i in range(len(files)): file_name = pattern.format(i + 1) file_path = osp.join(rsd, file_name) if not osp.exists(file_path): print(f"File {file_path} not exists, len={len(files)}") continue with open(file_path, "rb") as f: data = f.read() if args.new_short > 2: img = wmli.decode_img(data) img = wmli.resize_short_size(img, args.new_short) data = wmli.encode_img(img) all_frames.append(data) print(f"Save {save_name}") with open(save_name, "wb") as f: pickle.dump(all_frames, f)
def extract_frame(vid_item): """Generate optical flow using dense flow. Args: vid_item (list): Video item containing video full path, video (short) path, video id. Returns: bool: Whether generate optical flow successfully. """ full_path, subdir, save_path = vid_item out_full_path = save_path vr = wmli.VideoReader(full_path) print(f"{full_path} fps {vr.fps}.") sys.stdout.flush() # for i in range(len(vr)): all_frames = [] try: for i, vr_frame in enumerate(vr): if vr_frame is not None: if img_process_fn is not None: vr_frame = img_process_fn(vr_frame) w, h, _ = np.shape(vr_frame) if args.new_short == 0: if args.new_width == 0 or args.new_height == 0: # Keep original shape out_img = vr_frame else: out_img = mmcv.imresize(vr_frame, (args.new_width, args.new_height)) else: if min(h, w) == h: new_h = args.new_short new_w = int((new_h / h) * w) else: new_w = args.new_short new_h = int((new_w / w) * h) out_img = mmcv.imresize(vr_frame, (new_h, new_w)) all_frames.append(wmli.encode_img(out_img)) else: warnings.warn( 'Length inconsistent!' f'Early stop with {i + 1} out of {len(vr)} frames.') break out_full_path = out_full_path+f"_{len(all_frames)}.np" with open(out_full_path,"wb") as f: pickle.dump(all_frames,f) except Exception as e: print(f"Process {full_path} faild, {e}") print(f'{full_path} -> {out_full_path} done') sys.stdout.flush() return True
def create_tf_example(image, annotations): global src_file_index image_height = image['img_height'] image_width = image['img_width'] img_path = image['img_path'] if RECORD_IMG_SIZE is None: with tf.gfile.GFile(img_path, 'rb') as fid: encoded_jpg = fid.read() else: img = wmli.imread(img_path) img = wmli.resize_img(img, RECORD_IMG_SIZE, keep_aspect_ratio=True) encoded_jpg = wmli.encode_img(img) xmin = [] xmax = [] ymin = [] ymax = [] is_crowd = [] category_ids = [] for l, box in annotations: xmin.append(box[1]) xmax.append(box[3]) ymin.append(box[0]) ymax.append(box[2]) is_crowd.append(False) category_ids.append(l) if len(xmin) == 0: return None feature_dict = { 'image/height': dataset_util.int64_feature(image_height), 'image/width': dataset_util.int64_feature(image_width), 'image/filename': dataset_util.bytes_feature(img_path.encode('utf8')), 'image/encoded': dataset_util.bytes_feature(encoded_jpg), 'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmin), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmax), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymin), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymax), 'image/object/class/label': dataset_util.int64_list_feature(category_ids), 'image/object/is_crowd': dataset_util.int64_list_feature(is_crowd), } example = tf.train.Example(features=tf.train.Features( feature=feature_dict)) return example