def create_annotation(form, object_name, labels, frame): annotation = Region() annotation.object_name = object_name if form.cleaned_data['high_level']: annotation.full_frame = True annotation.x = 0 annotation.y = 0 annotation.h = 0 annotation.w = 0 else: annotation.full_frame = False annotation.x = form.cleaned_data['x'] annotation.y = form.cleaned_data['y'] annotation.h = form.cleaned_data['h'] annotation.w = form.cleaned_data['w'] annotation.metadata_text = form.cleaned_data['metadata_text'] annotation.metadata_json = form.cleaned_data['metadata_json'] annotation.frame = frame annotation.video = frame.video annotation.region_type = Region.ANNOTATION annotation.save() for l in labels: if l.strip(): dl = AppliedLabel() dl.video = annotation.video dl.frame = annotation.frame dl.region = annotation dl.label_name = l.strip() dl.source = dl.UI dl.save()
def create_annotation(form, object_name, labels, frame): annotation = Region() annotation.object_name = object_name if form.cleaned_data['high_level']: annotation.full_frame = True annotation.x = 0 annotation.y = 0 annotation.h = 0 annotation.w = 0 else: annotation.full_frame = False annotation.x = form.cleaned_data['x'] annotation.y = form.cleaned_data['y'] annotation.h = form.cleaned_data['h'] annotation.w = form.cleaned_data['w'] annotation.text = form.cleaned_data['text'] annotation.metadata = form.cleaned_data['metadata'] annotation.frame = frame annotation.video = frame.video annotation.region_type = Region.ANNOTATION annotation.save() for lname in labels: if lname.strip(): dl, _ = Label.objects.get_or_create(name=lname, set="UI") rl = RegionLabel() rl.video = annotation.video rl.frame = annotation.frame rl.region = annotation rl.label = dl rl.save()
def import_legacy_annotation(a, video_obj, frame, vdn_dataset=None): da = Region() da.region_type = Region.ANNOTATION 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 vdn_dataset: da.vdn_dataset = vdn_dataset if a['label'].strip(): da.object_name = a['label'] da.frame = frame da.full_frame = a['full_frame'] da.metadata_text = a['metadata_text'] da.metadata_json = a['metadata_json'] da.save() if a['label'].strip(): dl = AppliedLabel() dl.region = da dl.label_name = a['label'] dl.video = da.video dl.frame = da.frame dl.save() return da
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 import_region(a,video_obj,frame,detection_to_pk,vdn_dataset=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.region_type = a['region_type'] da.confidence = a['confidence'] da.object_name = a['object_name'] da.full_frame = a['full_frame'] if vdn_dataset: da.vdn_dataset = vdn_dataset if 'label' in a and a['label'].strip(): da.label = a['label'] if vdn_dataset: label_object, created = VLabel.objects.get_or_create(label_name=a['label'], source=VLabel.VDN, video=video_obj, vdn_dataset=vdn_dataset) else: label_object, created = VLabel.objects.get_or_create(label_name=a['label'], source=VLabel.UI, video=video_obj) da.label_parent = label_object da.frame = frame da.save() if da.region_type == Region.DETECTION: detection_to_pk[a['id']]=da.pk 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 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 create_annotation(form, object_name, labels, frame): annotation = Region() annotation.object_name = object_name if form.cleaned_data['high_level']: annotation.full_frame = True annotation.x = 0 annotation.y = 0 annotation.h = 0 annotation.w = 0 else: annotation.full_frame = False annotation.x = form.cleaned_data['x'] annotation.y = form.cleaned_data['y'] annotation.h = form.cleaned_data['h'] annotation.w = form.cleaned_data['w'] annotation.text = form.cleaned_data['text'] annotation.metadata = form.cleaned_data['metadata'] annotation.frame = frame annotation.video = frame.video annotation.region_type = Region.ANNOTATION annotation.save()
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 import_legacy_detection(d, video_obj, frame, vdn_dataset=None): dd = Region() dd.region_type = Region.DETECTION dd.video = video_obj dd.x = d['x'] dd.y = d['y'] dd.h = d['h'] dd.w = d['w'] dd.frame = frame dd.confidence = d['confidence'] dd.object_name = d['object_name'] dd.metadata_json = d['metadata'] if vdn_dataset: dd.vdn_dataset = dd.video.vdn_dataset dd.vdn_key = d['id'] dd.save() return dd
def create_region(a, video_obj, vdn_dataset): 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.region_type = a['region_type'] da.confidence = a['confidence'] da.object_name = a['object_name'] da.full_frame = a['full_frame'] da.parent_frame_index = a['parent_frame_index'] if vdn_dataset: da.vdn_dataset = vdn_dataset return da
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_region(a, video_obj, frame, detection_to_pk, vdn_dataset=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.region_type = a['region_type'] da.confidence = a['confidence'] da.object_name = a['object_name'] da.full_frame = a['full_frame'] if vdn_dataset: da.vdn_dataset = vdn_dataset da.frame = frame da.save() if da.region_type == Region.DETECTION: detection_to_pk[a['id']] = da.pk 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
def create_region(a, video_obj, vdn_dataset, old_task_to_new=None): """ # TODO: old_task_to_new """ 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'] 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_legacy_annotation(a,video_obj,frame,vdn_dataset=None): da = Region() da.region_type = Region.ANNOTATION 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 vdn_dataset: da.vdn_dataset = vdn_dataset if a['label'].strip(): da.label = a['label'] if vdn_dataset: label_object, created = VLabel.objects.get_or_create(label_name=a['label'], source=VLabel.VDN, video=video_obj, vdn_dataset=vdn_dataset) else: label_object, created = VLabel.objects.get_or_create(label_name=a['label'], source=VLabel.UI, video=video_obj) da.label_parent = label_object da.frame = frame da.full_frame = a['full_frame'] da.metadata_text = a['metadata_text'] da.metadata_json = a['metadata_json'] da.save() return da