def test_parse_video_fname(video): """Tests the extraction of camera and date information from video filenames.""" if video['format'] == 'arbitrary': with pytest.raises(Exception): camIdx, begin, end = parse_video_fname(video['name'], format=video['format']) return camIdx, begin, end = parse_video_fname(video['name'], format=video['format']) assert camIdx == video['cam'] assert begin == video['dt_begin'] assert end == video['dt_end']
def run(gt_file, videos, images, visualize_debug, output, fix_utc_2014, nb_bits=12): """ Converts bb_binary ground truth Cap'n Proto files to hdf5 files and extracts the corresponding rois from videos or images. """ def get_filenames(f): if f is None: return [] else: return [line.rstrip('\n') for line in f.readlines()] gen_factory = FrameGeneratorFactory(get_filenames(videos), get_filenames(images)) if os.path.exists(output): os.remove(output) distribution = DistributionCollection([('bits', Bernoulli(), nb_bits)]) dset = DistributionHDF5Dataset(output, distribution) camIdxs = [] periods = [] for fname in gt_file: fc = load_frame_container(fname) camIdx, start_dt, end_dt = parse_video_fname(fname) if fix_utc_2014 and start_dt.year == 2014: start_dt -= timedelta(hours=2) gt_frames = [] gen = gen_factory.get_generator(camIdx, start_dt) for frame, (video_frame, video_filename) in zip(fc.frames, gen): gt = {} np_frame = convert_frame_to_numpy(frame) rois, mask, positions = extract_gt_rois(np_frame, video_frame, start_dt) for name in np_frame.dtype.names: gt[name] = np_frame[name][mask] bits = [int_id_to_binary(id)[::-1] for id in gt["decodedId"]] gt["bits"] = 2 * np.array(bits, dtype=np.float) - 1 gt["tags"] = 2 * (rois / 255.).astype(np.float16) - 1 gt['filename'] = os.path.basename(video_filename) gt['camIdx'] = camIdx gt_frames.append(gt) print('.', end='', flush=True) print() gt_period = GTPeriod(camIdx, start_dt, end_dt, fname, gt_frames) periods.append( [int(gt_period.start.timestamp()), int(gt_period.end.timestamp())]) camIdxs.append(gt_period.camIdx) append_gt_to_hdf5(gt_period, dset) dset.attrs['periods'] = np.array(periods) dset.attrs['camIdxs'] = np.array(camIdxs) visualize_detection_tiles(dset, os.path.splitext(output)[0]) dset.close()
def __init__(self, video_dir, image_dir): self.videos = get_files(video_dir) self.image_dirs = get_subdirs(image_dir) self.videos_dict = {parse_video_fname(video)[:2]: video for video in self.videos} self.image_dirs_dict = {} for dir in self.image_dirs: first_frame = sorted(get_files(dir))[0] parsed = parse_image_fname(first_frame) self.image_dirs_dict[parsed] = dir
def save_first_frame(video_fname, output_dir, force): camIdx, start, _ = parse_video_fname(video_fname) outname = os.path.join(output_dir, get_fname(camIdx, start) + ".png") if os.path.exists(outname) and not force: return gen = raw_frames_generator(video_fname, format='guess_on_ext') frame = next(gen) assert frame.dtype == np.uint8 image_save(outname, frame)
def __init__(self, video_dir, image_dir): self.videos = get_files(video_dir) self.image_dirs = get_subdirs(image_dir) self.videos_dict = { parse_video_fname(video)[:2]: video for video in self.videos } self.image_dirs_dict = {} for dir in self.image_dirs: first_frame = sorted(get_files(dir))[0] parsed = parse_image_fname(first_frame) self.image_dirs_dict[parsed] = dir
def __init__(self, video_files, image_dirs): self.videos = video_files self.image_dirs = image_dirs self.sources = { parse_video_fname(video)[:2]: ('video', video) for video in self.videos } for dir in self.image_dirs: first_frame = sorted(get_files(dir))[0] parsed = parse_image_fname(first_frame) self.sources[parsed] = ('image', dir) self.sources = {(cam, drop_microseconds(ts)): val for (cam, ts), val in self.sources.items()}
def process_video(args): config = get_auto_config() logger.info('Initializing {} pipeline(s)'.format(args.num_threads)) plines = [Pipeline([Image, Timestamp], [PipelineResult], **config) for _ in range(args.num_threads)] logger.info('Loading bb_binary repository {}'.format(args.repo_output_path)) repo = Repository(args.repo_output_path) camId, _, _ = parse_video_fname(args.video_path) logger.info('Parsed camId = {}'.format(camId)) gen_processor = GeneratorProcessor(plines, lambda: BBBinaryRepoSink(repo, camId=camId)) logger.info('Processing video frames from {}'.format(args.video_path)) gen_processor(video_generator(args.video_path, args.timestamp_format, args.text_root_path))
def run(bb_gt_files, video_dir, image_dir, visualize_debug, force, output): """ Converts bb_binary ground truth Cap'n Proto files to hdf5 files and extracts the corresponding rois from videos or images. """ gen_factory = FrameGeneratorFactory(video_dir, image_dir) if force and os.path.exists(output): os.remove(output) dset = HDF5Dataset(output) camIdxs = [] periods = [] for fname in bb_gt_files: fc = load_frame_container(fname) camIdx, start_dt, end_dt = parse_video_fname(fname) basename = os.path.basename(fname) gt_frames = [] print(basename) gen = gen_factory.get_generator(camIdx, start_dt) first = True for frame, (video_frame, video_filename) in zip(fc.frames, gen): gt = {} np_frame = convert_frame_to_numpy(frame) rois, mask, positions = extract_gt_rois(np_frame, video_frame, start_dt) for name in np_frame.dtype.names: gt[name] = np_frame[name][mask] gt["bits"] = np.array( [int_id_to_binary(id)[::-1] for id in gt["decodedId"]]) gt["tags"] = rois gt['filename'] = os.path.basename(video_filename) gt_frames.append(gt) if first and visualize_debug: visualize_detections(gt, positions, video_frame) first = False print('.', end='') gt_period = GTPeriod(camIdx, start_dt, end_dt, fname, gt_frames) periods.append( [int(gt_period.start.timestamp()), int(gt_period.end.timestamp())]) camIdxs.append(gt_period.camIdx) append_gt_to_hdf5(gt_period, dset) dset.attrs['periods'] = np.array(periods) dset.attrs['camIdxs'] = np.array(camIdxs) dset.close()
def get_seperate_timestamps(path_video, ts_format, path_filelists): def get_flist_name(dt_utc): fmt = '%Y%m%d' dt = dt_utc.astimezone(get_timezone()) if ts_format == '2014': return dt.strftime(fmt) + '.txt' elif ts_format == '2015': return os.path.join(dt.strftime(fmt), 'images.txt') else: assert (False) def find_file(name, path): for root, dirs, files in os.walk(path): if name in [ os.path.join(os.path.basename(root), f) for f in files ]: return os.path.join(path, name) return None def next_day(dt): return dt + timedelta(days=1) fname_video = os.path.basename(path_video) cam, from_dt, to_dt = parse_video_fname(fname_video) # due to a bug in the data aquistion software we have to check in the # filename list of the next day as well timestamps = [from_dt, to_dt, next_day(from_dt), next_day(to_dt)] txt_files = set([get_flist_name(dt) for dt in timestamps]) txt_paths = [find_file(f, path_filelists) for f in txt_files] txt_paths = [path for path in txt_paths if path is not None] assert len(txt_paths) > 0 image_fnames = list( chain.from_iterable( [open(path, 'r').readlines() for path in txt_paths])) first_fname = fname_video.split('_TO_')[0] + '.jpeg\n' second_fname = fname_video.split('_TO_')[1].split('.mkv')[0] + '.jpeg\n' image_fnames.sort() fnames = image_fnames[image_fnames. index(first_fname):image_fnames.index(second_fname) + 1] return [ parse_image_fname(fn, format='beesbook')[1].timestamp() for fn in fnames ]
def run(bb_gt_files, video_dir, image_dir, visualize_debug, force, output): """ Converts bb_binary ground truth Cap'n Proto files to hdf5 files and extracts the corresponding rois from videos or images. """ gen_factory = FrameGeneratorFactory(video_dir, image_dir) if force and os.path.exists(output): os.remove(output) dset = HDF5Dataset(output) camIdxs = [] periods = [] for fname in bb_gt_files: fc = load_frame_container(fname) camIdx, start_dt, end_dt = parse_video_fname(fname) basename = os.path.basename(fname) gt_frames = [] print(basename) gen = gen_factory.get_generator(camIdx, start_dt) first = True for frame, (video_frame, video_filename) in zip(fc.frames, gen): gt = {} np_frame = convert_frame_to_numpy(frame) rois, mask, positions = extract_gt_rois(np_frame, video_frame, start_dt) for name in np_frame.dtype.names: gt[name] = np_frame[name][mask] gt["bits"] = np.array([int_id_to_binary(id)[::-1] for id in gt["decodedId"]]) gt["tags"] = rois gt['filename'] = os.path.basename(video_filename) gt_frames.append(gt) if first and visualize_debug: visualize_detections(gt, positions, video_frame) first = False print('.', end='') gt_period = GTPeriod(camIdx, start_dt, end_dt, fname, gt_frames) periods.append([int(gt_period.start.timestamp()), int(gt_period.end.timestamp())]) camIdxs.append(gt_period.camIdx) append_gt_to_hdf5(gt_period, dset) dset.attrs['periods'] = np.array(periods) dset.attrs['camIdxs'] = np.array(camIdxs) dset.close()
def process_video(video_path, repo_output_path, ts_format, text_root_path, rank): info = lambda msg: logger.info('Process {}: {}'.format(rank, msg)) import theano from pipeline import Pipeline from pipeline.cmdline import logger from pipeline.pipeline import GeneratorProcessor, get_auto_config from pipeline.io import BBBinaryRepoSink, video_generator from pipeline.objects import PipelineResult, Image, Timestamp from bb_binary import Repository, parse_video_fname repo_output_path = os.path.join(repo_output_path, 'process_{}'.format(rank)) info('Theano compile dir: {}'.format(theano.config.base_compiledir)) info('Output dir: {}'.format(repo_output_path)) config = get_auto_config() info('Initializing pipeline') pipeline = Pipeline([Image, Timestamp], [PipelineResult], **config) info('Loading bb_binary repository {}'.format(repo_output_path)) repo = Repository(repo_output_path) camId, _, _ = parse_video_fname(video_path) info('Parsed camId = {}'.format(camId)) gen_processor = GeneratorProcessor( pipeline, lambda: BBBinaryRepoSink(repo, camId=camId)) log_callback = lambda frame_idx: info('Processing frame {} from {}'.format( frame_idx, video_path)) ffmpeg_stderr_fd = open('process_{}_ffmpeg_stderr.log'.format(rank), 'w') info('Processing video frames from {}'.format(video_path)) gen_processor( video_generator(video_path, ts_format, text_root_path, log_callback, ffmpeg_stderr_fd))
def process_video(args): config = get_auto_config() logger.info('Initializing {} pipeline(s)'.format(args.num_threads)) plines = [ Pipeline([Image, Timestamp], [PipelineResult], **config) for _ in range(args.num_threads) ] logger.info('Loading bb_binary repository {}'.format( args.repo_output_path)) repo = Repository(args.repo_output_path) camId, _, _ = parse_video_fname(args.video_path) logger.info('Parsed camId = {}'.format(camId)) gen_processor = GeneratorProcessor( plines, lambda: BBBinaryRepoSink(repo, camId=camId)) logger.info('Processing video frames from {}'.format(args.video_path)) gen_processor( video_generator(args.video_path, args.timestamp_format, args.text_root_path))
def process_video(video_path, repo_output_path, ts_format, text_root_path, rank): info = lambda msg: logger.info(f"Process {rank}: {msg}") import theano from pipeline import Pipeline from pipeline.cmdline import logger from pipeline.pipeline import GeneratorProcessor, get_auto_config from pipeline.io import BBBinaryRepoSink, video_generator from pipeline.objects import PipelineResult, Image, Timestamp from bb_binary import Repository, parse_video_fname repo_output_path = os.path.join(repo_output_path, f"process_{rank}") info(f"Theano compile dir: {theano.config.base_compiledir}") info(f"Output dir: {repo_output_path}") config = get_auto_config() info("Initializing pipeline") pipeline = Pipeline([Image, Timestamp], [PipelineResult], **config) info(f"Loading bb_binary repository {repo_output_path}") repo = Repository(repo_output_path) camId, _, _ = parse_video_fname(video_path) info(f"Parsed camId = {camId}") gen_processor = GeneratorProcessor( pipeline, lambda: BBBinaryRepoSink(repo, camId=camId)) log_callback = lambda frame_idx: info( f"Processing frame {frame_idx} from {video_path}") ffmpeg_stderr_fd = open(f"process_{rank}_ffmpeg_stderr.log", "w") info(f"Processing video frames from {video_path}") gen_processor( video_generator(video_path, ts_format, text_root_path, log_callback, ffmpeg_stderr_fd))
def process_video(video_path, repo_output_path, ts_format, text_root_path, rank): info = lambda msg: logger.info('Process {}: {}'.format(rank, msg)) import theano from pipeline import Pipeline from pipeline.cmdline import logger from pipeline.pipeline import GeneratorProcessor, get_auto_config from pipeline.io import BBBinaryRepoSink, video_generator from pipeline.objects import PipelineResult, Image, Timestamp from bb_binary import Repository, parse_video_fname repo_output_path = os.path.join(repo_output_path, 'process_{}'.format(rank)) info('Theano compile dir: {}'.format(theano.config.base_compiledir)) info('Output dir: {}'.format(repo_output_path)) config = get_auto_config() info('Initializing pipeline') pipeline = Pipeline([Image, Timestamp], [PipelineResult], **config) info('Loading bb_binary repository {}'.format(repo_output_path)) repo = Repository(repo_output_path) camId, _, _ = parse_video_fname(video_path) info('Parsed camId = {}'.format(camId)) gen_processor = GeneratorProcessor(pipeline, lambda: BBBinaryRepoSink(repo, camId=camId)) log_callback = lambda frame_idx: info('Processing frame {} from {}'.format(frame_idx, video_path)) ffmpeg_stderr_fd = open('process_{}_ffmpeg_stderr.log'.format(rank), 'w') info('Processing video frames from {}'.format(video_path)) gen_processor(video_generator(video_path, ts_format, text_root_path, log_callback, ffmpeg_stderr_fd))
def test_parse_fname_videos(video): """Tests the extraction of camera, date and time information from filenames.""" camIdx, begin, end = parse_video_fname(video['name']) assert camIdx == video['cam'] assert begin == video['dt_begin'] assert end == video['dt_end']