def runFull(src, cache=False, cleanUp=True, limit=6000, optimizer=True): if cleanUp: if os.path.exists('/tmp/videos'): shutil.rmtree('/tmp/videos') manager = FullStorageManager(CustomTagger(FixedCameraBGFGSegmenter().segment, batch_size=30), CropSplitter(), '/tmp/videos') manager.put(src, 'test', args={'encoding': XVID, 'size': -1, 'sample': 1.0, 'offset': 0, 'limit': limit, 'batch_size': 30, 'num_processes': 4, 'background_scale': 1}, hwang=False) if cache: manager.cache('test', Condition(label='foreground'), hwang=False) clips = manager.get('test', Condition(label='foreground')) region = Box(200, 550, 350, 750) pipelines = [] d = DeepLensOptimizer() for c in clips: pipeline = c[KeyPoints()][ActivityMetric('one', region)][ Filter('one', [-0.25, -0.25, 1, -0.25, -0.25], 1.5, delay=10)] if optimizer: pipeline = d.optimize(pipeline) pipelines.append(pipeline) result = counts(pipelines, ['one'], stats=True) logrecord('full', ({'size': limit, 'cache': cache, 'optimizer': optimizer, 'file': src, 'folder_size': get_size('/tmp/videos')}), 'get', str(result), 's') if cache: manager.uncache('test', Condition(label='foreground'))
def test_tagger(): # logging.basicConfig(level=logging.DEBUG) urllib.request.urlretrieve( "https://www.dropbox.com/s/6adl0d91l5lokbu/cut4.mp4?dl=1", "/tmp/test.mp4") if os.path.exists('/tmp/videos'): shutil.rmtree('/tmp/videos') manager = FullStorageManager( CustomTagger(FixedCameraBGFGSegmenter().segment, batch_size=20), CropSplitter(), '/tmp/videos', '/tmp/videos', dsn='dbname=postgres user=postgres password=postgres host=127.0.0.1') start = time.time() output = manager.put('/tmp/test.mp4', 'test', parallel=False, args={ 'encoding': XVID, 'size': -1, 'sample': 1.0, 'offset': 0, 'limit': 1000, 'batch_size': 20, 'num_processes': 8, 'background_scale': 1 }) end = time.time() print("Without background resizing:", end - start) res = manager.get('test', Condition(label='foreground')) shape1 = next(next(res))['data'].shape assert shape1[2] == 3 manager = FullStorageManager( CustomTagger(FixedCameraBGFGSegmenter().segment, batch_size=20), CropSplitter(), '/tmp/videos', '/tmp/videos', dsn='dbname=postgres user=postgres password=postgres host=127.0.0.1') start = time.time() output = manager.put('/tmp/test.mp4', 'test', parallel=False, args={ 'encoding': XVID, 'size': -1, 'sample': 1.0, 'offset': 0, 'limit': 1000, 'batch_size': 20, 'num_processes': 8, 'background_scale': 0.2 }) end = time.time() print("With background resizing:", end - start) res = manager.get('test', Condition(label='foreground')) shape2 = next(next(res))['data'].shape assert shape2[2] == 3
def fullQuality(index=0): if os.path.exists('videos'): shutil.rmtree('videos') manager = FullStorageManager( CustomTagger(FixedCameraBGFGSegmenter(blur=11, movement_threshold=11).segment, batch_size=200), CropSplitter(), 'videos') now = datetime.datetime.now() filename = FOLDER + 'crash{0}.mp4'.format(index) manager.put(filename, 'test', args={ 'encoding': XVID, 'size': -1, 'sample': 1.0, 'offset': 0, 'limit': -1, 'batch_size': 200, 'num_processes': 4, 'background_scale': 1 }, hwang=False) #no queries to the background, lowest quality for speed to be within 5% manager.set_quality('test', Condition(label='background'), qscale=52, rscale=0.1) manager.set_quality('test', Condition(label='foreground'), qscale=44, rscale=1) put_result = (datetime.datetime.now() - now).total_seconds() logrecord('fullq', ({ 'folder_size': get_size('videos') }), 'put', str(put_result), 's') now = datetime.datetime.now() clips = manager.get('test', Condition(label='foreground')) for c in clips: pipeline_get(c[MotionVectors()][Speed()]) result = (datetime.datetime.now() - now).total_seconds() logrecord('fullq', ({ 'folder_size': get_size('videos') }), 'get', str(result), 's')
def runFull(src, tot=-1, sel=0.1): cleanUp() manager = FullStorageManager( CustomTagger(FixedCameraBGFGSegmenter().segment, batch_size=100), CropSplitter(), 'videos') now = timer() manager.put(src, 'test', args={ 'encoding': XVID, 'size': -1, 'sample': 1.0, 'offset': 0, 'limit': tot, 'batch_size': 100, 'num_processes': 12 }) put_time = timer() - now print("Put time for simple:", put_time) region = Box(515, 200, 700, 600) sel = sel / 2 clips = manager.get('test', Condition(label='foreground')) pipelines = [] for c in clips: pipelines.append(c[KeyPoints()][ActivityMetric('one', region)][Filter( 'one', [-0.25, -0.25, 1, -0.25, -0.25], 1.5, delay=10)]) result = counts(pipelines, ['one'], stats=True) logrecord('full', ({'file': src}), 'get', str(result), 's')
def get(): left = Box(1600, 1600, 1700, 1800) middle = Box(1825, 1600, 1975, 1800) right = Box(2050, 1600, 2175, 1800) # left = Box(1600 / 3, 1600 / 3, 1700 / 3, 1800 / 3) # middle = Box(1825 / 3, 1600 / 3, 1975 / 3, 1800 / 3) # right = Box(2050 / 3, 1600 / 3, 2175 / 3, 1800 / 3) # left = Box(1600 / 2, 1600 / 2, 1700 / 2, 1800 / 2) # middle = Box(1825 / 2, 1600 / 2, 1975 / 2, 1800 / 2) # right = Box(2050 / 2, 1600 / 2, 2175 / 2, 1800 / 2) clips = manager.get('test', Condition(label='foreground', custom_filter=None)) pipelines = [] now = timer() frame_count = 0 for c in clips: for frame in c: frame_count += 1 # pipelines.append(c[GoodKeyPoints()][ActivityMetric('left', left)][ # ActivityMetric('middle', middle)][ActivityMetric('right', right)][ # Filter('left', [1], 1, delay=25)][ # Filter('middle', [1], 1, delay=25)][ # Filter('right', [1], 1, delay=25)]) # result = counts(pipelines, ['left', 'middle', 'right'], stats=True) result = timer() - now print(frame_count) logrecord('full', ({'size': tot, 'batch_size': batch_size, 'file': src, 'folder_size': get_size(local_folder)}), 'get', str(result), 's')
def runFullPutMany(src_list, batch_size=30, cleanUp=False): if cleanUp: if os.path.exists('./videos_full'): shutil.rmtree('./videos_full') manager = FullStorageManager(None, CropSplitter(), 'videos_full') now = timer() targets = [os.path.basename(src) for src in src_list] logs = manager.put_many(src_list, targets, log=True, args={ 'encoding': XVID, 'size': -1, 'sample': 1.0, 'offset': 0, 'limit': -1, 'batch_size': batch_size, 'num_processes': os.cpu_count() }) put_time = timer() - now logrecord('full', ({ 'file': src_list }), 'put', str({'elapsed': put_time}), 's') for i, log in enumerate(logs): logrecord('fullMany', i, 'put', str({'elapsed': log}), 's') # Don't call get() for now for src in src_list: clips = manager.get(os.path.basename(src), Condition()) pipelines = [] for c in clips: pipelines.append(c[KeyPoints()]) result = counts(pipelines, ['one'], stats=True) logrecord('full', ({'file': src}), 'get', str(result), 's')
def runFullSequential(src, cleanUp=False): if cleanUp: if os.path.exists('./videos_full'): shutil.rmtree('./videos_full') manager = FullStorageManager(None, CropSplitter(), 'videos_full') now = timer() manager.put(src, os.path.basename(src), parallel=False, args={ 'encoding': XVID, 'size': -1, 'sample': 1.0, 'offset': 0, 'limit': -1, 'batch_size': 30, 'num_processes': os.cpu_count() }) put_time = timer() - now logrecord('full', ({'file': src}), 'put', str({'elapsed': put_time}), 's') clips = manager.get(os.path.basename(src), Condition()) pipelines = [] for c in clips: pipelines.append(c[KeyPoints()]) result = counts(pipelines, ['one'], stats=True) logrecord('full', ({'file': src}), 'get', str(result), 's')
def test_put(src, cleanUp=False): if cleanUp: if os.path.exists('./videos'): shutil.rmtree('./videos') manager = FullStorageManager(None, CropSplitter(), 'videos') start = time.time() manager.put(src, os.path.basename(src), parallel=True, args={ 'encoding': XVID, 'size': -1, 'sample': 1.0, 'offset': 0, 'limit': -1, 'batch_size': 50, 'num_processes': 6 }) print("Put time:", time.time() - start) clips = manager.get(os.path.basename(src), Condition(label='person')) pipelines = [] for c in clips: pipelines.append(c[KeyPoints()]) result = counts(pipelines, ['one'], stats=True) logrecord('full', ({'file': src}), 'get', str(result), 's')
def doexperiments_ks(budget=1000000000): for i in range(0, 300, 50): cleanUp() manager = FullStorageManager( CustomTagger(FixedCameraBGFGSegmenter().segment, batch_size=100), CropSplitter(), 'videos') manager.put('tcam.mp4', 'test', args={ 'encoding': XVID, 'size': -1, 'sample': 1.0, 'offset': 0, 'limit': 2000, 'batch_size': 100, 'num_processes': 4, 'background_scale': 1 }) clips = manager.get('test', Condition(label='foreground')) region = spatial_selectivity(buffer=i) d = DeepLensOptimizer() pipelines = [] for c in clips: pipeline = c[KeyPoints()][ActivityMetric('one', region)][Filter( 'one', [-0.25, -0.25, 1, -0.25, -0.25], 1.5, delay=10)] pipeline = d.optimize(pipeline) pipelines.append(pipeline) result, time1 = counts(pipelines, ['one'], stats=True) d.cacheKnapsack(manager, budget) clips = manager.get('test', Condition(label='foreground')) pipelines = [] for c in clips: pipeline = c[KeyPoints()][ActivityMetric('one', region)][Filter( 'one', [-0.25, -0.25, 1, -0.25, -0.25], 1.5, delay=10)] pipeline = d.optimize(pipeline) pipelines.append(pipeline) result, time2 = counts(pipelines, ['one'], stats=True) print(i, budget, time1['elapsed'], time2['elapsed'])
def runFullOpt(src, tot=1000, sel=0.1): cleanUp() manager = FullStorageManager( CustomTagger(FixedCameraBGFGSegmenter().segment, batch_size=20), CropSplitter(), 'videos') now = timer() manager.put(src, 'test', args={ 'encoding': XVID, 'size': -1, 'sample': 1.0, 'offset': 0, 'limit': tot, 'batch_size': 20, 'num_processes': 4, 'background_scale': 1 }) put_time = timer() - now print("Put time for full opt:", put_time) left = Box(1600, 1600, 1700, 1800) middle = Box(1825, 1600, 1975, 1800) right = Box(2050, 1600, 2175, 1800) sel = sel / 2 clips = manager.get('test', Condition(label='foreground', custom_filter=None)) pipelines = [] d = DeepLensOptimizer() for c in clips: pipeline = c[GoodKeyPoints()][ActivityMetric( 'left', left)][ActivityMetric('middle', middle)][ActivityMetric( 'right', right)][Filter('left', [1, 1, 1], 3, delay=25)][Filter('middle', [1, 1, 1], 3, delay=25)][Filter('right', [1, 1, 1], 3, delay=25)] pipeline = d.optimize(pipeline) pipelines.append(pipeline) result = counts(pipelines, ['left', 'middle', 'right'], stats=True) logrecord('fullopt', ({ 'size': tot, 'sel': sel, 'file': src }), 'get', str(result), 's')
def get(): clips = manager.get('test', Condition(label='foreground', custom_filter=None)) now = timer() frame_count = 0 for c in clips: for frame in c: frame_count += 1 result = timer() - now logrecord('full', ({ 'file': src, 'frames': frame_count }), 'get', str(result), 's')
def runFull(src, tot=1000, sel=0.1): cleanUp() manager = FullStorageManager( CustomTagger(FixedCameraBGFGSegmenter().segment, batch_size=100), CropSplitter(), 'videos', './videos', dsn='dbname=header user=postgres password=secret host=127.0.0.1') now = timer() manager.put(src, 'test', args={ 'encoding': XVID, 'size': -1, 'sample': 1.0, 'offset': 0, 'limit': tot, 'batch_size': 100, 'num_processes': 4, 'background_scale': 1 }) put_time = timer() - now print("Put time for full:", put_time) region = Box(200, 550, 350, 750) sel = sel / 2 clips = manager.get( 'test', Condition(label='foreground', custom_filter=time_filter(tot // 2 - int(tot * sel), tot // 2 + int(tot * sel)))) pipelines = [] for c in clips: pipelines.append(c[KeyPoints()][ActivityMetric('one', region)][Filter( 'one', [-0.25, -0.25, 1, -0.25, -0.25], 1.5, delay=10)]) result = counts(pipelines, ['one'], stats=True) logrecord('full', ({ 'size': tot, 'sel': sel, 'file': src }), 'get', str(result), 's')
def runFull(src, tot=1000, sel=0.1): cleanUp() manager = FullStorageManager(CustomTagger(FixedCameraBGFGSegmenter(movement_threshold=21,blur=7,movement_prob=0.10).segment, batch_size=100), CropSplitter(), 'videos') manager.put(src, 'test', args={'encoding': XVID, 'size': -1, 'sample': 1.0, 'offset': 0, 'limit': tot, 'batch_size': 100}) region = Box(500, 350, 750, 450) sel = sel/2 clips = manager.get('test', Condition(label='foreground', custom_filter=time_filter(tot//2-int(tot*sel),tot//2+int(tot*sel)))) pipelines = [] for c in clips: pipelines.append(c[KeyPoints(blur=3)][ActivityMetric('one', region)][Filter('one', [-0.5,1,-0.5], 0.25, delay=40)]) result = counts(pipelines, ['one'], stats=True) logrecord('full',({'size': tot, 'sel': sel, 'file': src}), 'get', str(result), 's')
def runFullOpt(src, tot=1000, sel=0.1): cleanUp() manager = FullStorageManager(CustomTagger(FixedCameraBGFGSegmenter().segment, batch_size=100), CropSplitter(), 'videos') manager.put(src, 'test', args={'encoding': XVID, 'size': -1, 'sample': 1.0, 'offset': 0, 'limit': tot, 'batch_size': 100}) region = Box(200, 550, 350, 750) sel = sel/2 clips = manager.get('test', Condition(label='foreground',custom_filter=time_filter(tot//2-int(tot*sel),tot//2+int(tot*sel)))) pipelines = [] d = DeepLensOptimizer() for c in clips: pipeline = c[KeyPoints()][ActivityMetric('one', region)][Filter('one', [-0.25,-0.25,1,-0.25,-0.25],1.5, delay=10)] pipeline = d.optimize(pipeline) pipelines.append(pipeline) result = counts(pipelines, ['one'], stats=True)[1]['elapsed'] logrecord('fullopt',({'size': tot, 'sel': sel, 'file': src}), 'get', str(result), 's')
from deeplens.full_manager.condition import Condition from deeplens.full_manager.full_video_processing import CropSplitter from deeplens.tracking.background import FixedCameraBGFGSegmenter from deeplens.dataflow.map import * from deeplens.full_manager.full_manager import * manager = FullStorageManager( CustomTagger(FixedCameraBGFGSegmenter().segment, batch_size=20), CropSplitter(), 'videos') res = manager.get('test', Condition(label='foreground', crop=Box(0, 0, 1000, 1000))) print([video for video in res[0]][0]['data'].shape) print(len(res)) play(res[0])
def runFull(src, tot=1000, batch_size=20): cleanUp() folder = '/tmp/videos' def tagger(vstream, batch_size): count = 0 for frame in vstream: count += 1 if count >= batch_size: break if count == 0: raise StopIteration("Iterator is closed") return {'label': 'foreground', 'bb': Box(1600, 1600, 2175, 1800)} manager = FullStorageManager(CustomTagger(tagger, batch_size=batch_size), NullSplitter(), folder) now = timer() manager.put(src, 'test', args={ 'encoding': XVID, 'size': -1, 'sample': 1.0, 'offset': 0, 'limit': tot, 'batch_size': batch_size, 'num_processes': 4, 'background_scale': 1 }) put_time = timer() - now print("Put time for full:", put_time) print("Batch size:", batch_size, "Folder size:", get_size(folder)) left = Box(1600, 1600, 1700, 1800) middle = Box(1825, 1600, 1975, 1800) right = Box(2050, 1600, 2175, 1800) # left = Box(1600 / 3, 1600 / 3, 1700 / 3, 1800 / 3) # middle = Box(1825 / 3, 1600 / 3, 1975 / 3, 1800 / 3) # right = Box(2050 / 3, 1600 / 3, 2175 / 3, 1800 / 3) # left = Box(1600 / 2, 1600 / 2, 1700 / 2, 1800 / 2) # middle = Box(1825 / 2, 1600 / 2, 1975 / 2, 1800 / 2) # right = Box(2050 / 2, 1600 / 2, 2175 / 2, 1800 / 2) clips = manager.get('test', Condition(label='foreground', custom_filter=None)) pipelines = [] now = timer() frame_count = 0 for c in clips: for frame in c: frame_count += 1 # pipelines.append(c[GoodKeyPoints()][ActivityMetric('left', left)][ # ActivityMetric('middle', middle)][ActivityMetric('right', right)][ # Filter('left', [1], 1, delay=25)][ # Filter('middle', [1], 1, delay=25)][ # Filter('right', [1], 1, delay=25)]) # result = counts(pipelines, ['left', 'middle', 'right'], stats=True) result = timer() - now print(frame_count) logrecord('full', ({ 'size': tot, 'batch_size': batch_size, 'file': src, 'folder_size': get_size(folder) }), 'get', str(result), 's')
def runFull(src, tot=1000, batch_size=20): # cleanUp() folder = '/bulk/videos' manager = FullStorageManager( CustomTagger(FixedCameraBGFGSegmenter().segment, batch_size=batch_size), CropSplitter(), folder) # now = timer() # manager.put(src, 'test', # args={'encoding': 'X264', 'size': -1, 'sample': 1.0, 'offset': 0, 'limit': tot, 'batch_size': batch_size, # 'num_processes': 4, 'background_scale': 1}) # put_time = timer() - now # print("Put time for full:", put_time) print("Batch size:", batch_size, "Folder size:", get_size(folder)) left = Box(1600, 1600, 1700, 1800) middle = Box(1825, 1600, 1975, 1800) right = Box(2050, 1600, 2175, 1800) # left = Box(1600 / 3, 1600 / 3, 1700 / 3, 1800 / 3) # middle = Box(1825 / 3, 1600 / 3, 1975 / 3, 1800 / 3) # right = Box(2050 / 3, 1600 / 3, 2175 / 3, 1800 / 3) # left = Box(1600 / 2, 1600 / 2, 1700 / 2, 1800 / 2) # middle = Box(1825 / 2, 1600 / 2, 1975 / 2, 1800 / 2) # right = Box(2050 / 2, 1600 / 2, 2175 / 2, 1800 / 2) clips = manager.get('test', Condition(label='foreground', custom_filter=None), large=True) pipelines = [] total_counts = {} total_frames = 0 total_time = 0 for c in tqdm(clips): this_result = count(c[GoodKeyPoints()][ActivityMetric( 'left', left)][ActivityMetric('middle', middle)][ActivityMetric( 'right', right)][Filter('left', [1], 1, delay=25)][Filter('middle', [1], 1, delay=25)][Filter('right', [1], 1, delay=25)], ['left', 'middle', 'right'], stats=True) print(this_result) total_counts = { k: total_counts.get(k, 0) + this_result[0].get(k, 0) for k in set(total_counts) | set(this_result[0]) } total_frames += this_result[1]['frames'] total_time += this_result[1]['elapsed'] result = total_counts, {'frames': total_frames, 'elapsed': total_time} logrecord('full', ({ 'size': tot, 'batch_size': batch_size, 'file': src, 'folder_size': get_size(folder) }), 'get', str(result), 's')
def _cacheClip(self, manager, clip): videos = manager.list() for video in videos: cache_condition = self.clip_filter(clip) manager.cache(video, Condition(custom_filter=cache_condition))