def main(movie_path): total_start = time.time() print('Detecting shots in movie {}'.format(movie_path)) movie_name = os.path.basename(movie_path) # Use GPU kernels if we have a GPU db = Database() print('Loading movie into Scanner database...') s = time.time() if db.has_gpu(): device = DeviceType.GPU else: device = DeviceType.CPU ############ ############ ############ ############ # 0. Ingest the video into the database ############ ############ ############ ############ [movie_table], _ = db.ingest_videos([(movie_name, movie_path)], force=True) print('Time: {:.1f}s'.format(time.time() - s)) print('Number of frames in movie: {:d}'.format(movie_table.num_rows())) s = time.time() ############ ############ ############ ############ # 1. Run Histogram over the entire video in Scanner ############ ############ ############ ############ print('Computing a color histogram for each frame...') frame = db.sources.FrameColumn() histogram = db.ops.Histogram(frame=frame, device=device) output = db.sinks.Column(columns={'histogram': histogram}) job = Job(op_args={ frame: movie_table.column('frame'), output: movie_name + '_hist' }) [hists_table] = db.run(output=output, jobs=[job], force=True) print('\nTime: {:.1f}s, {:.1f} fps'.format( time.time() - s, movie_table.num_rows() / (time.time() - s))) s = time.time() ############ ############ ############ ############ # 2. Load histograms and compute shot boundaries # in python ############ ############ ############ ############ print('Computing shot boundaries...') # Read histograms from disk hists = [ h for h in hists_table.column('histogram').load(parsers.histograms) ] boundaries = compute_shot_boundaries(hists) print('Found {:d} shots.'.format(len(boundaries))) print('Time: {:.1f}s'.format(time.time() - s)) s = time.time() ############ ############ ############ ############ # 3. Create montage in Scanner ############ ############ ############ ############ print('Creating shot montage...') row_length = min(16, len(boundaries)) rows_per_item = 1 target_width = 256 # Compute partial row montages that we will stack together # at the end frame = db.sources.FrameColumn() gather_frame = frame.sample() sliced_frame = gather_frame.slice() montage = db.ops.Montage(frame=sliced_frame, num_frames=row_length * rows_per_item, target_width=target_width, frames_per_row=row_length, device=device) sampled_montage = montage.sample() output = db.sinks.Column( columns={'montage': sampled_montage.unslice().lossless()}) item_size = row_length * rows_per_item starts_remainder = len(boundaries) % item_size evenly_divisible = (starts_remainder == 0) if not evenly_divisible: boundaries = boundaries[0:len(boundaries) - starts_remainder] job = Job( op_args={ frame: movie_table.column('frame'), gather_frame: db.sampler.gather(boundaries), sliced_frame: db.partitioner.all(item_size), sampled_montage: [ db.sampler.gather([item_size - 1]) for _ in range(len(boundaries) / item_size) ], output: 'montage_image' }) [montage_table] = db.run(output=output, jobs=[job], force=True) # Stack all partial montages together montage_img = np.zeros((1, target_width * row_length, 3), dtype=np.uint8) for img in montage_table.column('montage').load(): img = np.flip(img, 2) montage_img = np.vstack((montage_img, img)) print('') print('Time: {:.1f}s'.format(time.time() - s)) ############ ############ ############ ############ # 4. Write montage to disk ############ ############ ############ ############ cv2.imwrite('shots.jpg', montage_img) print('Successfully generated shots.jpg') print('Total time: {:.2f} s'.format(time.time() - total_start))
openreid_model_path = sys.argv[2] print("OpenReId Model Path", openreid_model_path) db = Database() if not db.has_table(movie_name): print("Ingesting video into Scanner ...") db.ingest_videos([(movie_name, movie_path)], force=True) input_table = db.table(movie_name) sampler = db.streams.All sampler_args = {} if db.has_gpu(): print("Using GPUs") device = DeviceType.GPU pipeline_instances = -1 else: print("Using CPUs") device = DeviceType.CPU pipeline_instances = 1 frame = db.sources.FrameColumn() # This is for the demo only. In practice, you should perform the human detection # Then, provide the frame and the bounding boxes for each person. # We want to extract the Open-ReID feature for each bounding boxes reid_features = db.ops.ExtractReIDFeature(frame=frame, model_path=openreid_model_path,
[input_table], failed = db.ingest_videos([('example', movie_path)], force=True) stride = 1 frame = db.sources.FrameColumn() strided_frame = db.streams.Stride(frame, stride) model_name = 'ssd_mobilenet_v1_coco_2017_11_17' model_url = MODEL_TEMPLATE_URL.format(model_name) # Call the newly created object detect op objdet_frame = db.ops.ObjDetect( frame=strided_frame, dnn_url=model_url, device=DeviceType.GPU if db.has_gpu() else DeviceType.CPU, batch=2) output_op = db.sinks.Column(columns={'bundled_data': objdet_frame}) job = Job( op_args={ frame: db.table('example').column('frame'), output_op: 'example_obj_detect', }) [out_table] = db.run(output=output_op, jobs=[job], force=True, pipeline_instances_per_node=1) out_table.profiler().write_trace('obj.trace')