import cv2 import struct import sys import os sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..') import util video_path = util.download_video() if len(sys.argv) <= 1 else sys.argv[1] print('Performing classification on video {}'.format(video_path)) video_name = os.path.splitext(os.path.basename(video_path))[0] with Database() as db: [input_table], _ = db.ingest_videos([(video_name, video_path)], force=True) descriptor = NetDescriptor.from_file(db, 'nets/resnet.toml') batch_size = 48 frame = db.sources.FrameColumn() caffe_input = db.ops.CaffeInput(frame=frame, net_descriptor=descriptor.as_proto(), batch_size=batch_size, device=DeviceType.GPU) caffe_output = db.ops.Caffe(caffe_frame=caffe_input, net_descriptor=descriptor.as_proto(), batch_size=batch_size, batch=batch_size, device=DeviceType.GPU) output = db.sinks.Column(columns={'softmax': caffe_output}) job = Job(
def detect_faces(db, input_frame_columns, output_samplings, output_names, width=960, prototxt_path=None, model_weights_path=None, templates_path=None, return_profiling=False): if prototxt_path is None: prototxt_path = download_temp_file( 'https://storage.googleapis.com/scanner-data/nets/caffe_facenet/facenet_deploy.prototxt' ) if model_weights_path is None: model_weights_path = download_temp_file( 'https://storage.googleapis.com/scanner-data/nets/caffe_facenet/facenet_deploy.caffemodel' ) if templates_path is None: templates_path = download_temp_file( 'https://storage.googleapis.com/scanner-data/nets/caffe_facenet/facenet_templates.bin' ) descriptor = NetDescriptor(db) descriptor.model_path = prototxt_path descriptor.model_weights_path = model_weights_path descriptor.input_layer_names = ['data'] descriptor.output_layer_names = ['score_final'] descriptor.mean_colors = [119.29959869, 110.54627228, 101.8384321] facenet_args = db.protobufs.FacenetArgs() facenet_args.templates_path = templates_path facenet_args.threshold = 0.5 caffe_args = facenet_args.caffe_args caffe_args.net_descriptor.CopyFrom(descriptor.as_proto()) if db.has_gpu(): device = DeviceType.GPU pipeline_instances = -1 else: device = DeviceType.CPU pipeline_instances = 1 if type(output_names) is not list: output_names = [ '{}_{}'.format(output_names, i) for i in range(len(input_frame_columns)) ] else: assert (len(output_names) == len(input_frame_columns)) if type(output_samplings) is not list: output_samplings = [ output_samplings for _ in range(len(input_frame_columns)) ] else: assert (len(output_samplings) == len(input_frame_columns)) outputs = [] scales = [1.0, 0.5, 0.25, 0.125] batch_sizes = [int((2**i)) for i in range(len(scales))] profilers = {} for scale, batch in zip(scales, batch_sizes): facenet_args.scale = scale caffe_args.batch_size = batch frame = db.ops.FrameInput() #resized = db.ops.Resize( # frame = frame, # width = width, height = 0, # min = True, preserve_aspect = True, frame_info = db.ops.InfoFromFrame(frame=frame) facenet_input = db.ops.FacenetInput(frame=frame, args=facenet_args, device=device) facenet = db.ops.Facenet(facenet_input=facenet_input, args=facenet_args, device=device) facenet_output = db.ops.FacenetOutput(facenet_output=facenet, original_frame_info=frame_info, args=facenet_args) sampled_output = facenet_output.sample() output = db.ops.Output(columns=[sampled_output]) jobs = [] for output_name, frame_column, output_sampling in zip( output_names, input_frame_columns, output_samplings): job = Job( op_args={ frame: frame_column, sampled_output: output_sampling, output: '{}_{}'.format(output_name, scale) }) jobs.append(job) bulk_job = BulkJob(output=output, jobs=jobs) output = db.run(bulk_job, force=True, work_packet_size=batch * 4, io_packet_size=batch * 20, pipeline_instances_per_node=pipeline_instances) profilers['scale_{}'.format(scale)] = output[0].profiler() outputs.append(output) # Register nms bbox op and kernel db.register_op('BBoxNMS', [], ['bboxes'], variadic_inputs=True) kernel_path = script_dir + '/bbox_nms_kernel.py' db.register_python_kernel('BBoxNMS', DeviceType.CPU, kernel_path) # scale = max(width / float(max_width), 1.0) scale = 1.0 bbox_inputs = [db.ops.Input() for _ in outputs] nmsed_bboxes = db.ops.BBoxNMS(*bbox_inputs, scale=scale) output = db.ops.Output(columns=[nmsed_bboxes]) jobs = [] for i in range(len(input_frame_columns)): op_args = {} for bi, cols in enumerate(outputs): op_args[bbox_inputs[bi]] = cols[i].column('bboxes') op_args[output] = output_names[i] jobs.append(Job(op_args=op_args)) bulk_job = BulkJob(output=output, jobs=jobs) return db.run(bulk_job, force=True)
from scannerpy import Database, DeviceType, Job from scannerpy.stdlib import NetDescriptor, parsers, bboxes import math import os import subprocess import cv2 import sys import os.path sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..') import util util.download_video() with Database() as db: descriptor = NetDescriptor.from_file(db, 'nets/cpm2.toml') cpm2_args = db.protobufs.CPM2Args() cpm2_args.scale = 368.0 / 720.0 caffe_args = cpm2_args.caffe_args caffe_args.net_descriptor.CopyFrom(descriptor.as_proto()) caffe_args.batch_size = 1 video_path = util.download_video() if not db.has_table('example'): print('Ingesting video into Scanner ...') db.ingest_videos([('example', video_path)], force=True) input_table = db.table('example') frame = input_table.as_op().all(item_size=50) frame_info = db.ops.InfoFromFrame(frame=frame) cpm2_input = db.ops.CPM2Input(frame=frame,
import os.path sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../..') import util import numpy as np util.download_video() with Database() as db: video_path = util.download_video() if True or not db.has_table('example'): print('Ingesting video into Scanner ...') db.ingest_videos([('example', video_path)], force=True) input_table = db.table('example') descriptor = NetDescriptor.from_file(db, 'nets/faster_rcnn_coco.toml') caffe_args = db.protobufs.CaffeArgs() caffe_args.net_descriptor.CopyFrom(descriptor.as_proto()) caffe_args.batch_size = 1 frame = db.ops.FrameInput() caffe_frame = db.ops.CaffeInput(frame=frame, args=caffe_args, device=DeviceType.GPU) cls_prob, rois, fc7 = db.ops.FasterRCNN(caffe_input=caffe_frame, args=caffe_args, device=DeviceType.GPU) bboxes, feature = db.ops.FasterRCNNOutput(cls_prob=cls_prob, rois=rois, fc7=fc7, args=caffe_args,
import os import subprocess import cv2 import sys import os.path sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..') import util util.download_video() with Database() as db: # TODO(wcrichto): comment the demo. Make the Scanner philosophy more clear. # Add some figures to the wiki perhaps explaining the high level descriptor = NetDescriptor.from_file(db, 'nets/caffe_facenet.toml') facenet_args = db.protobufs.FacenetArgs() facenet_args.threshold = 0.5 caffe_args = facenet_args.caffe_args caffe_args.net_descriptor.CopyFrom(descriptor.as_proto()) caffe_args.batch_size = 2 print('Ingesting video into Scanner ...') [input_table], _ = db.ingest_videos([('example', util.download_video())], force=True) base_batch = 4 base_size = 1280*720 # TODO(apoms): determine automatically from video current_size = 1280*720 current_batch = math.floor(base_size / float(current_size) * base_batch) print('Running face detector...')