def create_region(self, a): da = Region() da.video_id = self.video.pk da.x = a['x'] da.y = a['y'] da.h = a['h'] da.w = a['w'] da.vdn_key = a['id'] if 'text' in a: da.text = a['text'] elif 'metadata_text' in a: da.text = a['metadata_text'] if 'metadata' in a: da.metadata = a['metadata'] elif 'metadata_json' in a: da.metadata = a['metadata_json'] da.materialized = a.get('materialized', False) da.png = a.get('png', False) da.region_type = a['region_type'] da.confidence = a['confidence'] da.object_name = a['object_name'] da.full_frame = a['full_frame'] if a.get('event', None): da.event_id = self.event_to_pk[a['event']] if 'parent_frame_index' in a: da.frame_index = a['parent_frame_index'] else: da.frame_index = a['frame_index'] if 'parent_segment_index' in a: da.segment_index = a.get('parent_segment_index', -1) else: da.segment_index = a.get('segment_index', -1) return da
def create_region(a,video_obj,vdn_dataset,event_to_pk=None): da = Region() da.video = video_obj da.x = a['x'] da.y = a['y'] da.h = a['h'] da.w = a['w'] da.vdn_key = a['id'] if 'text' in a: da.text = a['text'] elif 'metadata_text' in a: da.text = a['metadata_text'] if 'metadata' in a: da.metadata = a['metadata'] elif 'metadata_json' in a: da.metadata = a['metadata_json'] da.materialized = a.get('materialized',False) da.png = a.get('png',False) da.region_type = a['region_type'] da.confidence = a['confidence'] da.object_name = a['object_name'] da.full_frame = a['full_frame'] if a.get('event',None): da.event_id = event_to_pk[a['event']] da.parent_frame_index = a['parent_frame_index'] da.parent_segment_index = a.get('parent_segment_index',-1) if vdn_dataset: da.vdn_dataset = vdn_dataset return da
def import_region_json(r, frame_index, video_id, event_id, segment_index=None, frame_id=None): dr = Region() dr.frame_index = frame_index dr.video_id = video_id dr.event_id = event_id dr.object_name = r['object_name'] dr.region_type = r.get('region_type', Region.ANNOTATION) dr.full_frame = r.get('full_frame', False) if segment_index: dr.segment_index = segment_index if frame_id: dr.frame_id = frame_id dr.x = r.get('x', 0) dr.y = r.get('y', 0) dr.w = r.get('w', 0) dr.h = r.get('h', 0) dr.confidence = r.get('confidence', 0.0) if r.get('text', None): dr.text = r['text'] else: dr.text = "" dr.metadata = r.get('metadata', None) return dr
def import_frame_regions_json(regions_json, video, event_id): """ Import regions from a JSON with frames identified by immuntable identifiers such as filename/path :param regions_json: :param video: :param event_id: :return: """ video_id = video.pk filename_to_pk = {} frame_index_to_pk = {} if video.dataset: # For dataset frames are identified by subdir/filename filename_to_pk = { df.original_path(): (df.pk, df.frame_index) for df in Frame.objects.filter(video_id=video_id) } else: # For videos frames are identified by frame index frame_index_to_pk = { df.frame_index: (df.pk, df.segment_index) for df in Frame.objects.filter(video_id=video_id) } regions = [] for k in regions_json: r = Region() if k['target'] == 'filename': fname = k['filename'] if not fname.startswith('/'): fname = '/{}'.format(fname) pk, findx = filename_to_pk[fname] r.frame_id = pk r.frame_index = findx elif k['target'] == 'index': pk, sindx = frame_index_to_pk[k['frame_index']] r.frame_id = pk r.frame_index = k['frame_index'] r.segment_index = sindx else: raise ValueError('invalid target: {}'.format(k['target'])) r.video_id = video_id r.event_id = event_id r.region_type = k['region_type'] r.materialized = k.get('materialized', False) r.full_frame = k.get('full_frame', False) r.x = k['x'] r.y = k['y'] r.w = k['w'] r.h = k['h'] r.metadata = k['metadata'] r.text = k['text'] Region.objects.bulk_create(regions, 1000)
def create_region(self, a): da = Region() da.video_id = self.video.pk da.x = a['x'] da.y = a['y'] da.h = a['h'] da.w = a['w'] da.text = a['text'] da.metadata = a['metadata'] da.png = a.get('png', False) da.region_type = a['region_type'] da.confidence = a['confidence'] da.object_name = a['object_name'] da.full_frame = a['full_frame'] da.event_id = self.event_to_pk[a['event']] da.frame_index = a['frame_index'] da.segment_index = a.get('segment_index', -1) return da
def import_regions_json(regions_json, video_id, event_id): fname_to_pk = { df.name: df.pk for df in Frame.objects.filter(video_id=video_id) } regions = [] for k in regions_json: if k['target'] == 'filename': r = Region() r.frame_id = fname_to_pk[k['filename']] r.video_id = video_id r.event_id = event_id r.x = k['x'] r.y = k['y'] r.w = k['w'] r.h = k['h'] r.metadata = k['metadata'] r.text = k['text'] else: raise ValueError Region.objects.bulk_create(regions, 1000) raise NotImplementedError
def create_region(a,video_obj,vdn_dataset,event_to_pk=None): da = Region() da.video = video_obj da.x = a['x'] da.y = a['y'] da.h = a['h'] da.w = a['w'] da.vdn_key = a['id'] da.metadata_text = a['metadata_text'] da.metadata_json = a['metadata_json'] da.materialized = a.get('materialized',False) da.png = a.get('png',False) da.region_type = a['region_type'] da.confidence = a['confidence'] da.object_name = a['object_name'] da.full_frame = a['full_frame'] if a.get('event',None): da.event_id = event_to_pk[a['event']] da.parent_frame_index = a['parent_frame_index'] da.parent_segment_index = a.get('parent_segment_index',-1) if vdn_dataset: da.vdn_dataset = vdn_dataset return da
def import_frame_json(f, frame_index, event_id, video_id): regions = [] df = Frame() df.video_id = video_id df.event_id = event_id df.frame_index = frame_index df.name = f['path'] for r in f.get('regions', []): dr = Region() dr.frame_index = frame_index dr.video_id = video_id dr.event_id = event_id dr.object_name = r['object_name'] dr.region_type = r.get('region_type', Region.ANNOTATION) dr.full_frame = r.get('full_frame', False) dr.x = r.get('x', 0) dr.y = r.get('y', 0) dr.w = r.get('w', 0) dr.h = r.get('h', 0) dr.confidence = r.get('confidence', 0.0) dr.text = r.get('text', None) dr.metadata = r.get('metadata', None) regions.append(dr) return df, regions