new_faces = [] for video, framelist, facelist in zip(videos, frames, faces): frame_objs = Frame.objects.filter(video_id=video.id).filter( number__in=framelist).order_by('number') # Annotate frame objects with shot metadata frame_objs_with_metadata = [] shots = Shot.objects.filter(video_id=video.id).order_by('min_frame').all() shot_iter = iter(shots) cur_shot = next(shot_iter) for frame in frame_objs: while frame.number > cur_shot.max_frame: cur_shot = next(shot_iter) frame_objs_with_metadata.append((frame, cur_shot)) for frame_with_shot, bboxlist in zip(frame_objs_with_metadata, facelist.load()): for bbox in bboxlist: new_faces.append( Face(frame=frame_with_shot[0], shot=frame_with_shot[1], bbox_x1=bbox.x1, bbox_x2=bbox.x2, bbox_y1=bbox.y1, bbox_y2=bbox.y2, probability=bbox.score, labeler=LABELER)) Face.objects.bulk_create(new_faces) Notifier().notify('Done with face detection')
video_ids = sorted( list(all_videos.difference(labeled_videos).difference(bad_movie_ids))) videos = Video.objects.filter(id__in=video_ids).order_by('id') cfg = cluster_config(num_workers=100, worker=worker_config('n1-standard-32')) with make_cluster(cfg, no_delete=True) as db_wrapper: db = db_wrapper.db #if True: # db_wrapper = ScannerWrapper.create() # db = db_wrapper.db histograms = st.histograms.compute_flow_histograms( db, videos=[video.for_scannertools() for video in list(videos)], run_opts={ 'work_packet_size': 4, 'pipeline_instances_per_node': 2, 'io_packet_size': 2496, 'checkpoint_frequency': 1, 'tasks_in_queue_per_pu': 2 }) # Tag this video as being labeled new_videotags = [ VideoTag(video=video, tag=LABELED_TAG) for video in videos ] VideoTag.objects.bulk_create(new_videotags) Notifier().notify('Done with flow histograms')
from scannertools import shot_detection, kube from esper.scannerutil import ScannerWrapper, ScannerSQLTable from esper.prelude import Timer, Notifier, now, pcache, log from query.models import Video from esper.kube import make_cluster from threading import Thread, Condition from attr import attrs, attrib from pprint import pprint from storehouse import StorehouseException import traceback import subprocess as sp import pandas as pd from time import strftime notifier = Notifier() @attrs(frozen=True) class ScannerJobConfig: io_packet_size = attrib(type=int) work_packet_size = attrib(type=int) batch = attrib(type=int, default=1) pipelines_per_worker = attrib(type=int, default=-1) class TestFailure(Exception): pass def bench(name, args, run_pipeline, configs, force=False, no_delete=False):
# from esper.scannerutil import ScannerWrapper # if True: # db_wrapper = ScannerWrapper.create() db = db_wrapper.db job_config = ScannerJobConfig(io_packet_size=10000, work_packet_size=400, batch=400) hists = run_pipeline(db, videos, batch=job_config.batch, run_opts={ 'io_packet_size': job_config.io_packet_size, 'work_packet_size': job_config.work_packet_size, }) print('hists', len(hists)) hists, videos = unzip([(h, v) for (h, v) in zip(hists, videos) if v.num_frames < 800000]) boundaries = compute_shot_boundaries( db, videos=[v.for_scannertools() for v in videos], db_videos=videos, video_ids=[v.id for v in videos], histograms=hists) # print(len([v for (v, b) in zip(videos, boundaries) if b is None])) Notifier().notify('done')
labeler=LABELER, frame=frame) new_poses.append(new_pose) if pose_detected: new_frame_tags.append( Frame.tags.through(frame_id=frame.pk, tag_id=LABELED_TAG.pk)) Pose.objects.bulk_create(new_poses, batch_size=10000) Frame.tags.through.objects.bulk_create(new_frame_tags, batch_size=10000) print("{} pose objects created, {} frame tagged".format( len(new_poses), len(new_frame_tags))) # print("Tagging all the frames as being labeled") # for video, framelist in tqdm(zip(videos, frames), total=len(videos)): # frame_objs = Frame.objects.filter(video_id=video.id, number__in=framelist) # for frame in frame_objs: # if (video.id, frame.number) in frames_in_db_already: # continue # new_frame_tags.append( # Frame.tags.through(frame_id=frame.pk, tag_id=LABELED_TAG.pk)) print("Tagging this video as being labeled") videos_tagged_already = set( [vtag.video_id for vtag in VideoTag.objects.filter(tag=LABELED_TAG).all()]) new_videotags = [ VideoTag(video=video, tag=LABELED_TAG) for video in videos if video.id not in videos_tagged_already ] VideoTag.objects.bulk_create(new_videotags) Notifier().notify('Done with pose detection')
)) FaceGender.objects.bulk_create(new_facegenders, batch_size=100000) Tag all the frames as being labeled new_frame_tags = [] frame_objs = Frame.objects.filter(video_id=video.id, number__in=framelist) for frame in frame_objs: if (video.id, frame.number) in frames_in_db_already: continue new_frame_tags.append( Frame.tags.through(frame_id=frame.pk, tag_id=LABELED_TAG.pk)) Frame.tags.through.objects.bulk_create(new_frame_tags, batch_size=100000) # Get the videos that already have the tag videos_tagged_already = set([ vtag.video_id for vtag in VideoTag.objects.filter(tag=LABELED_TAG).all() ]) # Tag this video as being labeled new_videotags = [ VideoTag(video=video, tag=LABELED_TAG) for video in videos if video.id not in videos_tagged_already ] VideoTag.objects.bulk_create(new_videotags) print("Finished putting everything in the database") Notifier().notify('Done with gender detection')
# if (abs(obj.bbox_x1 - bbox.x1) < .000001 and # abs(obj.bbox_x2 - bbox.x2) < .000001 and # abs(obj.bbox_y1 - bbox.y1) < .000001 and # abs(obj.bbox_y2 - bbox.y2) < .000001 and # abs(obj.probability - bbox.score) < .000001): # face_obj = obj # break # if face_obj is None: # print("Couldn't find face {} in {}".format(bbox, face_objs)) # new_facelandmarks.append(FaceLandmarks( # face=face_obj, # landmarks=landmarks.tobytes(), # labeler=LABELER # )) #FaceLandmarks.objects.bulk_create(new_facelandmarks) ## Tag all the frames as being labeled #new_frame_tags = [] #for video, framelist in zip(videos, frames): # frame_objs = Frame.objects.filter(video_id=video.id).filter(number__in=framelist) # for frame in frame_objs: # new_frame_tags.append( # Frame.tags.through(frame_id=frame.pk, tag_id=LABELED_TAG.pk)) #Frame.tags.through.objects.bulk_create(new_frame_tags) ## Tag this video as being labeled #new_videotags = [VideoTag(video=video, tag=LABELED_TAG) for video in videos] #VideoTag.objects.bulk_create(new_videotags) Notifier().notify('Done with face landmark detection')
bin_size = 255.0 / len(saturation_channel) avg_saturation = np.sum([ count * j * bin_size for j, count in enumerate(saturation_channel) ]) / num_pixels frame_obj = Frame.objects.get(video_id=video.id, number=i) frame_obj.saturation = avg_saturation #new_frame_tags.append( # Frame.tags.through(frame_id=frame_obj.pk, tag_id=LABELED_TAG.pk)) frames_to_update.append(frame_obj) with transaction.atomic(): for frame in frames_to_update: frame.save() #Frame.tags.through.objects.bulk_create(new_frame_tags) # Get the videos that already have the tag videos_tagged_already = set([ vtag.video_id for vtag in VideoTag.objects.filter(tag=LABELED_TAG).all() ]) # Tag this video as being labeled new_videotags = [ VideoTag(video=video, tag=LABELED_TAG) for video in videos if video.id not in videos_tagged_already ] VideoTag.objects.bulk_create(new_videotags) Notifier().notify('Done with saturation computation')
frame_objs = Frame.objects.filter( video_id=video.id, number__in=framelist).order_by('number') for (frame_obj, brightness_val) in zip(frame_objs, brightness_list.load()): if (video.id, frame_obj.number) in frames_in_db_already: continue new_frame_tags.append( Frame.tags.through(frame_id=frame_obj.pk, tag_id=LABELED_TAG.pk)) frame_obj.brightness = brightness_val frames_to_update.append(frame_obj) with transaction.atomic(): for frame in frames_to_update: frame.save() Frame.tags.through.objects.bulk_create(new_frame_tags) # Get the videos that already have the tag videos_tagged_already = set([ vtag.video_id for vtag in VideoTag.objects.filter(tag=LABELED_TAG).all() ]) # Tag this video as being labeled new_videotags = [ VideoTag(video=video, tag=LABELED_TAG) for video in videos if video.id not in videos_tagged_already ] VideoTag.objects.bulk_create(new_videotags) Notifier().notify('Done with brightness computation')
db, videos=[video.for_scannertools() for video in videos], frames=frames, megabatch=1) # Labeler for this pipeline LABELER, _ = Labeler.objects.get_or_create(name='rmscontrast') LABELED_TAG, _ = Tag.objects.get_or_create(name='rmscontrast:labeled') # Tag all the frames as being labeled, and update frames with contrast val new_frame_tags = [] frames_to_update = [] for video, framelist, contrast_list in zip(videos, frames, contrast): frame_objs = Frame.objects.filter(video_id=video.id, number__in=framelist).order_by('number') for (frame_obj, contrast_val) in zip(frame_objs, contrast_list.load()): new_frame_tags.append( Frame.tags.through(frame_id=frame_obj.pk, tag_id=LABELED_TAG.pk)) frame_obj.contrast = contrast_val frames_to_update.append(frame_obj) with transaction.atomic(): for frame in frames_to_update: frame.save() Frame.tags.through.objects.bulk_create(new_frame_tags) # Tag this video as being labeled new_videotags = [VideoTag(video=video, tag=LABELED_TAG) for video in videos] VideoTag.objects.bulk_create(new_videotags) Notifier().notify('Done with contrast computation')
video_ids = sorted(list(set(video_ids).difference(set(shot_video_ids)))) videos = Video.objects.filter(id__in=video_ids).order_by('id') cfg = cluster_config(num_workers=100, worker=worker_config('n1-standard-32')) with make_cluster(cfg, no_delete=True) as db_wrapper: db = db_wrapper.db #if True: # db_wrapper = ScannerWrapper.create() # db = db_wrapper.db hsv_histograms = st.histograms.compute_hsv_histograms( db, videos=[video.for_scannertools() for video in list(videos)], run_opts={ 'work_packet_size': 4, 'io_packet_size': 2496, 'checkpoint_frequency': 1, 'tasks_in_queue_per_pu': 2, 'pipeline_instances_per_node': 2 }) # Tag this video as being labeled new_videotags = [ VideoTag(video=video, tag=LABELED_TAG) for video in videos ] VideoTag.objects.bulk_create(new_videotags) Notifier().notify('Done with HSV histograms')
video_ids = sorted( list(all_videos.difference(labeled_videos).difference(bad_movie_ids))) videos = Video.objects.filter(id__in=video_ids).order_by('id') cfg = cluster_config(num_workers=100, worker=worker_config('n1-standard-32')) with make_cluster(cfg, no_delete=True) as db_wrapper: db = db_wrapper.db #if True: # db_wrapper = ScannerWrapper.create() # db = db_wrapper.db histograms = st.histograms.compute_histograms( db, videos=[video.for_scannertools() for video in list(videos)], run_opts={ 'work_packet_size': 8, 'io_packet_size': 2496, 'checkpoint_frequency': 1, 'tasks_in_queue_per_pu': 2, 'pipeline_instances_per_node': 8 }) # Tag this video as being labeled new_videotags = [ VideoTag(video=video, tag=LABELED_TAG) for video in videos ] VideoTag.objects.bulk_create(new_videotags) Notifier().notify('Done with RGB histograms')
FaceFeatures.objects.bulk_create(new_features, batch_size=10000) #new_frame_tags = [] #frame_objs = Frame.objects.filter(video_id=video.id).filter(number__in=framelist) #for frame in frame_objs: # if (video.id, frame.number) in frames_in_db_already: # continue # new_frame_tags.append( # Frame.tags.through(frame_id=frame.pk, tag_id=LABELED_TAG.pk)) #if len(new_frame_tags) == 0: # continue #Frame.tags.through.objects.bulk_create(new_frame_tags, batch_size=10000) # Get the videos that already have the tag #videos_tagged_already = set([ # vtag.video_id # for vtag in VideoTag.objects.filter(tag=LABELED_TAG).all() #]) ## Tag this video as being labeled #new_videotags = [ # VideoTag(video=video, tag=LABELED_TAG) # for video in videos # if video.id not in videos_tagged_already #] #VideoTag.objects.bulk_create(new_videotags) print("Finished putting everything in the database") Notifier().notify('Done with face embeddings')
import scannerpy import scannertools as st import os from django.db.models import Q from query.models import Video import numpy as np from esper.prelude import Notifier # Load all Star Wars and Harry Potter films videos = Video.objects.filter(name__contains='godfather') db = scannerpy.Database() # Calculate at 2 fps frames = [ list(range(0, video.num_frames, int(round(video.fps) / 2))) for video in videos ] # Calculate motion flow flow = st.optical_flow.compute_flow( db, videos=[video.for_scannertools() for video in videos], frames=frames, megabatch=1) Notifier().notify('Done computing motion flow!')
db, videos=[video.for_scannertools() for video in videos], frames=frames, megabatch=1) # Labeler for this pipeline LABELER, _ = Labeler.objects.get_or_create(name='laplacianvariance') LABELED_TAG, _ = Tag.objects.get_or_create(name='laplacianvariance:labeled') # Tag all the frames as being labeled, and update frames with sharpness val new_frame_tags = [] frames_to_update = [] for video, framelist, sharpness_list in zip(videos, frames, sharpness): frame_objs = Frame.objects.filter(video_id=video.id, number__in=framelist).order_by('number') for (frame_obj, sharpness_val) in zip(frame_objs, sharpness_list.load()): new_frame_tags.append( Frame.tags.through(frame_id=frame_obj.pk, tag_id=LABELED_TAG.pk)) frame_obj.sharpness = sharpness_val frames_to_update.append(frame_obj) with transaction.atomic(): for frame in frames_to_update: frame.save() Frame.tags.through.objects.bulk_create(new_frame_tags) # Tag this video as being labeled new_videotags = [VideoTag(video=video, tag=LABELED_TAG) for video in videos] VideoTag.objects.bulk_create(new_videotags) Notifier().notify('Done with sharpness computation')