def add_collection(*, name, **_): with db.session_scope() as session: coll = models.Collection(name=name) session.add(coll) session.commit() coll = to_dict(coll) return {'collection': coll}
def set_active_collection(*, collection_id, **_): with db.session_scope() as session: start_date, end_date = db.collection_get_bounds( session, collection_id=collection_id) if start_date is not None and end_date is not None: end_date = end_date + datetime.timedelta(seconds=1) frames_query = models.FramesQuery(tbegin=start_date, tend=end_date, collection_id=collection_id) n_frames = 20 mode = 'subsample' ntotal, frames = db.get_frames(session, frames_query, mode, n_frames, after_id=None) search_results = {'ntotal': ntotal, 'frames': frames} search = { 'startDate': start_date, 'endDate': end_date, 'mode': mode, 'nFrames': n_frames, 'afterId': None, 'collectionId': collection_id } return {'searchResults': search_results, 'search': search}
def get_frame_appearances(*, frame_id, **_): with db.session_scope() as session: frame = session.query(models.Frame).get(frame_id) apps = frame.appearances app_dicts = [to_dict(a, rels=['appearance_labels']) for a in apps] frame_dict = to_dict(frame) return {'frame': frame_dict, 'appearances': app_dicts}
def add_creator(*, name, **_): with db.session_scope() as session: creator = models.Creator(name=name) session.add(creator) session.commit() creator_dict = to_dict(creator) return {'creator': creator_dict}
def update_box(*, appearance_id, box, **_): with db.session_scope() as session: app = session.query(models.Appearance).get(appearance_id) for k, v in box.items(): setattr(app, k, v) session.commit() app_dict = to_dict(app) return {'appearance': app_dict}
def delete_appearance_label(*, appearance_label_id, **_): with db.session_scope() as session: applabel = session.query( models.AppearanceLabel).get(appearance_label_id) app = applabel.appearance session.delete(applabel) session.commit() app_dicts = to_dict(app, rels=['appearance_labels']) return {'appearance': app_dicts}
def delete_appearance(*, appearance_id, **_): with db.session_scope() as session: app = session.query(models.Appearance).get(appearance_id) frame = app.frame session.delete(app) session.commit() app_dicts = [ to_dict(a, rels=['appearance_labels']) for a in frame.appearances ] frame_dict = to_dict(frame) return {'frame': frame_dict, 'appearances': app_dicts}
def change_frame(*, frame_id, collection_id, shift, **_): with db.session_scope() as session: if shift != 0: assert collection_id, 'Collection need to be defined when useing shift.' frame_id = db.navigate_frame(session, collection_id, frame_id, shift) frame = session.query(models.Frame).get(frame_id) apps = frame.appearances app_dicts = [to_dict(a, rels=['appearance_labels']) for a in apps] frame_dict = to_dict(frame) return {'frame': frame_dict, 'appearances': app_dicts}
def add_appearance_label(*, appearance_id, label_id, creator_id, **_): with db.session_scope() as session: creator = session.query(models.Creator).get(creator_id) label = session.query(models.Label).get(label_id) appearance = session.query(models.Appearance).get(appearance_id) app_label = models.AppearanceLabel(creator=creator, appearance=appearance, label=label) session.add(app_label) session.commit() app_dict = to_dict(appearance, rels=['appearance_labels']) return {'appearance': app_dict}
def update_search(*, search, **_): with db.session_scope() as session: frames_query = models.FramesQuery( tbegin=search['start_date'], tend=search['end_date'], collection_id=search.get('collection_id')) ntotal, frames = db.get_frames(session, frames_query=frames_query, n_frames=search.get('n_frames', 1), mode=search.get('mode'), after_id=search.get('after_id')) search_results = {'ntotal': ntotal, 'frames': frames} return {'searchResults': search_results, 'search': search}
def collection_add_frames(*, collection_id, frame_ids, search, full, **_): with db.session_scope() as session: if full: frames_query = models.FramesQuery( tbegin=search['start_date'], tend=search['end_date'], collection_id=search.get('collection_id')) n = db.collection_add_frames_via_query(session, collection_id, frames_query, nframes=None) return {'collectionId': collection_id, 'countDelta': n} else: n = db.collection_add_frames(session, collection_id, frame_ids) return {'collectionId': collection_id, 'countDelta': n}
def add_appearance(*, frame_id, appearance, label_ids, creator_id, **_): with db.session_scope() as session: frame = session.query(models.Frame).get(frame_id) creator = session.query(models.Creator).get(creator_id) labels = session.query(models.Label).filter( models.Label.id.in_(label_ids)).all() app = models.Appearance(frame=frame, creator=creator, **appearance) session.add(app) for label in labels: appLabel = models.AppearanceLabel(creator=creator, appearance=app, label=label) session.add(appLabel) session.commit() app_dict = to_dict(app, rels=['appearance_labels']) return {'appearance': app_dict}
def insert_label_appearances(*, label_appearances, creator_id): with db.session_scope() as session: creator = session.query(models.Creator).get(creator_id) for la in label_appearances: frame = session.query(models.Frame).get(la['frame_id']) label = session.query(models.Label).get(la['label_id']) app = models.Appearance(frame=frame, creator=creator, bbox_xmax=la['bbox_xmax'], bbox_xmin=la['bbox_xmin'], bbox_ymax=la['bbox_ymax'], bbox_ymin=la['bbox_ymin']) session.add(app) appLabel = models.AppearanceLabel(creator=creator, appearance=app, label=label) session.add(appLabel) session.commit()
def download_collection(*, collection_id=28, appearance_needed=True): with db.session_scope() as session: xs = [] f = session.query(models.Frame).options( joinedload('appearances') .joinedload('appearance_labels') .joinedload('label') ).filter(models.Frame.collections.any(id=collection_id)) \ .all() for frame in f: d = to_dict(frame, rels=['appearances', 'appearance_labels', 'label']) if appearance_needed: include = len(d['appearances']) > 0 else: include = True if include: xs.append(d) return {"collection": xs}
def collection_remove_frames(*, collection_id, frame_ids, **_): with db.session_scope() as session: n = db.collection_remove_frames(session, collection_id, frame_ids) return {'collectionId': collection_id, 'countDelta': -n}
def delete_collection(*, collection_id, **_): with db.session_scope() as session: coll = session.query(models.Collection).get(collection_id) session.delete(coll) session.commit() return {'collectionId': collection_id}