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 create_detector_dataset(object_names, labels): class_distribution = defaultdict(int) rboxes = defaultdict(list) rboxes_set = defaultdict(set) frames = {} class_names = {k: i for i, k in enumerate(labels.union(object_names))} i_class_names = {i: k for k, i in class_names.items()} for r in Region.objects.all().filter(object_name__in=object_names): frames[r.frame_id] = r.frame if r.pk not in rboxes_set[r.frame_id]: rboxes[r.frame_id].append( (class_names[r.object_name], r.x, r.y, r.x + r.w, r.y + r.h)) rboxes_set[r.frame_id].add(r.pk) class_distribution[r.object_name] += 1 for dl in Label.objects.filter(name__in=labels): lname = dl.name for l in RegionLabel.all().objects.filter(label=dl): frames[l.frame_id] = l.frame if l.region: r = l.region if r.pk not in rboxes_set[r.frame_id]: rboxes[l.frame_id].append( (class_names[lname], r.x, r.y, r.x + r.w, r.y + r.h)) rboxes_set[r.frame_id].add(r.pk) class_distribution[lname] += 1 return class_distribution, class_names, rboxes, rboxes_set, frames, i_class_names
def import_region_labels(self): region_labels = [] for rl in self.json.get('region_label_list', []): drl = RegionLabel() drl.frame_id = self.frame_to_pk[rl['frame']] drl.region_id = self.region_to_pk[rl['region']] drl.video_id = self.video.pk if 'event' in rl: drl.event_id = self.event_to_pk[rl['event']] drl.frame_index = rl['frame_index'] drl.segment_index = rl['segment_index'] drl.label_id = self.label_to_pk[rl['label']] region_labels.append(drl) RegionLabel.objects.bulk_create(region_labels, 1000)