def tag2seg(tag_sequence, sent): """ construct segments according to the output of segmentor :param tag_sequence: segment tag sequence :return: """ # get bieos style sequence bieos = ot2bieos(tag_sequence) segments = [] beg, end = -1, -1 for (i, tag) in enumerate(bieos): if tag == 'O' or tag == 'S': beg, end = i, i + 1 aspect_tag = sent['tags'][i] segments.append(Segment(beg=beg, end=end, aspect=aspect_tag)) beg, end = -1, -1 elif tag == 'B': assert beg == -1 beg = i elif tag == 'E': end = i + 1 assert beg != -1 and beg < end aspect_tag = sent['tags'][i] segments.append(Segment(beg=beg, end=end, aspect=aspect_tag)) beg, end = -1, -1 return segments
def insert_collection(json_file, region, name_keys, replacement, split): results = [] err = [] with open(json_file) as j_file: j = json.load(j_file) if 'type' in j and j['type'] == 'FeatureCollection': feature_list = j['features'] for feat in feature_list: print "Has geometry? " + str(feat.get('geometry') is not None) names = [] for key in name_keys: n = feat.get('properties').get(key) print n if replacement and n in replacement: names.append(replacement[n]) elif split and n in split: names += list(split[n]) elif n: names.append(n.title()) print names if feat and feat.get('geometry'): path = Path(**feat) s = Segment(coordinates = path.geo_json, trail_names = names, length = len(path), region = region) try: s.save() results.append(s) except (errors.OperationError, errors.ValidationError): err.append(s) continue return results, err
def btn_delete_clicked(self): ''' When delete is clicked 1. Prompt the user for the name of the save/object 2. Delete the object from the segmentation_list 3. Delete the object from segments.json 4. Reorder the remaining objects 5. Delete segments.json if there are no remaining saves ''' # prompt the user for a save to delete and pass in the available segment_name's name = prompt_deleting([s.segment_name for s in self.segmentations]) import json with open(self.data_fname, mode='r') as f: segmentations = json.load(f) # data to be saved in segments.json data = [] # initialize removed_id to be an out-of-bounds value (will be updated in the loop) removed_id = len(segmentations) for segment in segmentations: segment_dict = json.loads(segment) seg = Segment(**segment_dict) if seg.segment_name != name: # reorder remaining Segments if deletion has already been performed if seg.id > removed_id: seg.id -= 1 # https://doc.qt.io/qtforpython/PySide2/QtWidgets/QListWidgetItem.html self.segmentation_list.item(seg.id).setText( "{} | {} | {}".format(seg.id, seg.segment_name, seg.type_class)) # save the new id in the backend self.segmentations[seg.id].id -= 1 entry = seg.json() data.append(entry) else: # remove the selected Segment from backend self.segmentations.pop(seg.id) # remove the selected Segment from frontend (https://doc.qt.io/archives/qt-4.8/qlistwidget.html#takeItem) self.segmentation_list.takeItem(seg.id) # save the removed_id to reorder subsequent Segments removed_id = seg.id # delete segments.json if no Segments remain if not data: os.remove(self.data_fname) # otherwise, save the remaining Segments else: with open(self.data_fname, mode='w') as f: f.write(json.dumps(data, indent=2))
def create_segment(self, s): ds = Segment() ds.video_id = self.video.pk ds.segment_index = s.get('segment_index', '-1') ds.start_time = s.get('start_time', 0) ds.end_time = s.get('end_time', 0) ds.metadata = s.get('metadata', "") if s.get('event', None): ds.event_id = self.event_to_pk[s['event']] ds.frame_count = s.get('frame_count', 0) ds.start_index = s.get('start_index', 0) return ds
def post_create_user(sender, instance, created, **kwargs): print "Post save emited for", instance users = User.objects.filter(username="******") if created and len(users) > 0: user_template = users[0] print "user_template", user_template user = instance image_types = ImageType.objects.filter(user=user_template) print "image_types", image_types print 'mkdir {0}{1}'.format(BASE_DIR+'/segment/static/uploads/',str(user_template.id),str(user.id)) os.popen('cp -a {0}{1} {0}{2}'.format(BASE_DIR+'/segment/static/uploads/',str(user_template.id),str(user.id))) for image_type in image_types: print "imagetype", image_type #folder_random_name = get_random_name() #print 'mkdir {0}{1}/{2}'.format(BASE_DIR+'/segment/static/uploads/',str(user.id),folder_random_name) #os.popen('mkdir {0}{1}/{2}'.format(BASE_DIR+'/segment/static/uploads/',str(user.id),folder_random_name)) #os.popen('mkdir {0}{1}/{2}/segments'.format(BASE_DIR+'/segment/static/uploads/',str(user.id),folder_random_name)) #os.popen('mkdir {0}{1}/{2}/segments'.format(BASE_DIR+'/segment/static/uploads/',str(user.id),folder_random_name)) it = ImageType() it.name=image_type.name it.user = user it.folder = image_type.folder it.save() it.users_shared.add(user) tags = Tag.objects.filter(image_type=image_type) for tag in tags: t = Tag() t.name = tag.name t.image_type = it t.save() images = Image.objects.filter(image_type=image_type) print "images", images for image in images: i = Image() i.name = image.name i.image_type = it i.filename = str(image.filename).replace('/'+str(user_template.id)+'/','/'+str(user.id)+'/') #print 'cp "{0}{1}" "{2}{3}"'.format(BASE_DIR+'/segment/static/',str(image.filename),BASE_DIR+'/segment/static/' ,str(i.filename)) #os.popen('cp "{0}{1}" "{2}{3}"'.format(BASE_DIR+'/segment/static/',str(image.filename),BASE_DIR+'/segment/static/' ,str(i.filename))) i.parent_segment = image.parent_segment i.save() segments = Segment.objects.filter(image=image) print "image id", image.id, "segments", len(segments), segments for segment in segments: s = Segment() s.x1 = segment.x1 s.y1 = segment.y1 s.x2 = segment.x2 s.y2 = segment.y2 s.image = i s.filename = str(segment.filename).replace('/'+str(user_template.id)+'/','/'+str(user.id)+'/') #print 'cp "{0}{1}" "{2}{3}"'.format(BASE_DIR+'/segment/static/',str(segment.filename),BASE_DIR+'/segment/static/',str(s.filename)) #os.popen('cp "{0}{1}" "{2}{3}"'.format(BASE_DIR+'/segment/static/',str(segment.filename),BASE_DIR+'/segment/static/',str(s.filename))) s.save() for tag in segment.tags.all(): tag_new = Tag.objects.filter(image_type=it,name=tag.name) s.tags.add(tag_new[0])
def create_segment(s, v): ds = Segment() ds.video_id = v.pk ds.segment_index = s.get('segment_index', '-1') ds.start_time = s.get('start_time', 0) ds.end_time = s.get('end_time', 0) ds.metadata = s.get('metadata', "") ds.frame_count = s.get('frame_count', 0) ds.start_index = s.get('start_index', 0) return ds
def point_in_polygon(polygon, point): line_left = Segment(Point(-999999999, point.y), point) line_right = Segment(point, Point(999999999, point.y)) count_left = 0 count_right = 0 for e in polygon.get_edges(): if edges_intersect(line_left, e): count_left += 1 if edges_intersect(line_right, e): count_right += 1 if count_left % 2 == 0 and count_right % 2 == 0: return False return True
def create_segment(s,v): ds = Segment() ds.video_id = v.pk ds.segment_index = s.get('segment_index','-1') ds.start_time = s.get('start_time',0) ds.end_time = s.get('end_time',0) ds.metadata = s.get('metadata',"") ds.frame_count = s.get('frame_count',0) ds.start_index = s.get('start_index',0) return ds
def break_segment(oid): seg_model = Segment.objects(id=oid).get() seg = Path(**seg_model.coordinates) points = [] for s in Segment.objects(coordinates__geo_intersects = seg_model.coordinates): p = Path(**s.coordinates) if p[0] in seg: points.append(p[0]) elif p[-1] in seg: points.append(p[-1]) new_models = [] start = 0 for i, point in enumerate(seg): if i == 0: continue if point in points or i == len(seg.list) - 1: new_p = seg.slice(start, i+1) new_m = Segment(coordinates= new_p.geo_json, trail_names=seg_model.trail_names, length = len(new_p), region = seg_model.region) new_models.append(new_m) new_m.save() start = i seg_model.delete() return new_models
def populateSegmentList(self): ''' On start, populate a list of segmentations that user previously did ''' try: import json with open(self.data_fname, 'r') as f: segmentations = json.load(f) for segment in segmentations: segment_dict = json.loads(segment) seg = Segment(**segment_dict) self.addSegmentationItem(seg) except FileNotFoundError as e: self.writeMessage("No segmentations detected")
def btn_save_clicked(self): ''' When save is clicked 1. Prompt the user for the name and type of the object 2. save the object ''' data = prompt_saving() if len(self.current_result_point_indices) == 0: self.writeMessage("There are no points to save") else: import json a = [] if not os.path.isfile(self.data_fname): self.largest_seg_id = self.largest_seg_id + 1 segment = Segment(id=self.largest_seg_id, data_file_name=self.current_data_file_name, segment_name=data["seg_name"], indices=self.current_result_point_indices, type_class=(data["type_class"], 1)) self.addSegmentationItem(segment) entry = segment.json() a.append(entry) with open(self.data_fname, mode='w') as f: f.write(json.dumps(a, indent=2)) else: with open(self.data_fname) as feedsjson: feeds = json.load(feedsjson) self.largest_seg_id = len(feeds) segment = Segment(id=self.largest_seg_id, data_file_name=self.current_data_file_name, segment_name=data["seg_name"], indices=self.current_result_point_indices, type_class=(data["type_class"], 1)) self.addSegmentationItem(segment) entry = segment.json() feeds.append(entry) with open(self.data_fname, mode='w') as f: f.write(json.dumps(feeds, indent=2)) self.lowerScene.clear() # added by Star: unselect points, resume their color and size to original ones self.resume_selected_points()
def btn_combine_clicked(self): data = prompt_saving() if len(self.multi_segments_point_indices) == 0: self.writeMessage( "No segments are selected to combine. Please apply first.") return # reuse the codes in btn_save_clicked. Can be refractored later. import json a = [] if not os.path.isfile(self.data_fname): self.largest_seg_id = self.largest_seg_id + 1 segment = Segment(id=self.largest_seg_id, data_file_name=self.current_data_file_name, segment_name=data["seg_name"], indices=self.multi_segments_point_indices, type_class=(data["type_class"], 1)) self.addSegmentationItem(segment) entry = segment.json() a.append(entry) with open(self.data_fname, mode='w') as f: f.write(json.dumps(a, indent=2)) else: with open(self.data_fname) as feedsjson: feeds = json.load(feedsjson) self.largest_seg_id = len(feeds) segment = Segment(id=self.largest_seg_id, data_file_name=self.current_data_file_name, segment_name=data["seg_name"], indices=self.multi_segments_point_indices, type_class=(data["type_class"], 1)) self.addSegmentationItem(segment) entry = segment.json() feeds.append(entry) with open(self.data_fname, mode='w') as f: f.write(json.dumps(feeds, indent=2))
# 'seed': 123, # 'time_slices': 5} # # train_patches = generate_ordered_patches(input_file, label_file, **input_params) # test_patches = generate_patches(input_file, label_file, n_patches=200, **input_params) train_patches = pickle.load(open('../Data/NNSegment/Annotation_patches_8slices_large.pkl', 'rb')) test_patches = train_patches[-50:] train_patches = train_patches[:-50] model_path = './temp_save/' if not os.path.exists(model_path): os.mkdir(model_path) model = Segment(input_shape=(256, 256, 20), unet_feat=32, fc_layers=[64, 32], n_classes=3, model_path=model_path) for epoch in range(25): inds = np.random.choice(np.arange(len(train_patches)), (200,), replace=False) train_subset = [train_patches[i] for i in inds] model.fit(train_subset, label_input='annotation', n_epochs=4, valid_patches=test_patches, valid_label_input='annotation') model.save(model.model_path + '/final.h5') sites = ['D4-Site_0', 'D4-Site_1', 'D4-Site_2', 'D4-Site_3', 'D5-Site_0', 'D5-Site_1', 'D5-Site_3', 'D5-Site_4']
def segment_generate(index: int) -> None: # noinspection PyTypeChecker segment = Segment.create_or_update({'name': 'segment_%s' % index})[0] print('\t%s: Upserted' % segment.name)
def main(): init_time = Bunch(\ start="08:59:00 02-07-15", end="19:00:00 02-07-15" ) init_time.set_in_place(time_utils.simplefmt_in_pdt_to_utc_epoch) entries = [] begin = increment_epoch_by_day(init_time.start, 0) end = increment_epoch_by_day(init_time.end, 3) print('begin_UTC_Epoch:', begin) print('end_UTC_Epoch:', end) entries += whereis.AutoDB().get_date_range(begin, end) print('Retrieved entries from database') # entries = geo_utils.calculate_bearing(entries) print('Calculated bearing for all data') series = [] for index in range(1, len(entries)): # for entry in entries: # When passed only an entry id, a Segment looks up the previous id and # uses that for it's starting entry. So if you pass an `id` for # `Entry_n`, it looks up `Entry_n-1` and uses that as it's starting # entry. The xy for the Segment is then calculated as xy for `Entry_n` # assuming `Entry_n-1` is the origin. # # Note also that this example runs extremely slowly since AutoDB # automatically creates and closes the database connection for every # query. So each segment means a new disk read. seg = Segment([entries[index-1], entries[index]], whereis.AutoDB()) series.append(seg) print('Created segments') # Assembling all the xy coordinates via dead-reckoning coords = [[0, 0]] for i in range(len(series)): x = series[i].x y = series[i].y coords.append([x+coords[i][0], y+coords[i][1]]) # coords = series.xy_coords print('Created xy coords') ylist, xlist = zip(*coords) xlist = [-n for n in xlist] fig, ax = plt.subplots() ax.plot(xlist, ylist, marker='o', markersize=0.25, markeredgewidth=0.05, linewidth=0.1, ) ax.set_aspect('equal') ax.set_xlim(*min_max(xlist, lambda x: x)) ax.set_ylim(*min_max(ylist, lambda y: y)) # Uncomment the below lines to hide the axis labels # ax.set_yticklabels([]) # ax.set_xticklabels([]) # ax.get_xaxis().set_ticks([]) # ax.get_yaxis().set_ticks([]) print('Writing image') fig.savefig('example00__dead_reckoning.png', bbox_inches='tight', dpi=2000)
db.session.commit() # Add tasks task1 = Task(creator_id=u1.id, assignee_id=u2.id, title="Fix this", status="In Progress", team=t1.id) task2 = Task(creator_id=u2.id, assignee_id=u1.id, title="Do stuff", status="Done", team=t2.id) # Add segments to teams s1 = Segment(team_id=t1.id, name="Design", lead=u1.id) s2 = Segment(team_id=t1.id, name="Engineering", lead=u1.id) s3 = Segment(team_id=t1.id, name="Product", lead=u2.id) # Add new objects to session, so they'll persist db.session.add(s1) db.session.add(s2) db.session.add(s3) db.session.add(task1) db.session.add(task2) # Commit--otherwise, this never gets saved! db.session.commit() # Add members to teams m1 = Member(role="Designer", user_id=u1.id, segment_id=s1.id)