def add_annotation(anno): check_input_manager( 'annotation', anno, [ 'video_id', 'frame_num', 'entity_id', 'username', 'status', 'bbox']) if 'job_id' not in anno: anno['job_id'] = 'null' key_query = "(job_id" value_query = "(%s" % anno['job_id'] for k, v in anno.items(): if k in ('username', 'status', 'bbox', 'entity_desc'): key_query += "," + k value_query += ",'%s'" % anno[k] elif k in ('video_id', 'frame_num', 'entity_id', 'cat_id'): key_query += "," + k value_query += ",%s" % anno[k] key_query += ")" value_query += ")" query = "INSERT INTO annotation %s VALUES %s" % (key_query, value_query) annotation_id = run_single_query(query) print("annotation %s is added!" % annotation_id) return annotation_id
def add_job(job): check_input_manager('job', job, [ 'username', 'entity_id', 'video_id', 'bbox', 'start_frame', 'end_frame' ]) if 'status' not in job: job['status'] = 'new' key_query = "(username" value_query = "('%s'" % job['username'] for k, v in job.items(): if k in ("job_name", "status", "bbox"): key_query += "," + k value_query += ",'%s'" % job[k] elif k in ("video_id", "entity_id", "start_frame", "end_frame"): key_query += "," + k value_query += ",%s" % job[k] key_query += ")" value_query += ")" query = "INSERT INTO job %s VALUES %s" % (key_query, value_query) print("Job %s is added! Entity: {}, Start: {}, End: {}".format( job['entity_id'], job['start_frame'], job['end_frame'])) return run_single_query(query)
def del_entity(entity): if 'entity_id' in entity: query = "DELETE FROM entity WHERE entity_id='%s'" % entity['entity_id'] print("entity %s is deleted!" % entity['entity_id']) elif 'video_id' in entity: query = "DELETE FROM entity WHERE video_id='%s'" % entity['video_id'] print("entities in video %s are deleted!" % entity['video_id']) return run_single_query(query)
def del_user(user): check_input_manager('user', user, ['username']) remove_status = remove_bucket(user['username']) if remove_status is None: print("Remove user failed!") return query = "DELETE FROM user WHERE username='******'" % user['username'] print("User %s is deleted!" % user['username']) return run_single_query(query)
def del_category(cat): if 'cat_id' in cat: query = "DELETE FROM category WHERE cat_id='%s'" % cat['cat_id'] print("cat_id %s is deleted!" % cat['cat_id']) elif 'name' in cat: query = "DELETE FROM category WHERE name='%s'" % cat['name'] print("name %s is deleted!" % cat['name']) elif 'sup_cat_name' in cat: query = "DELETE FROM category WHERE sup_cat_name='%s'" % cat[ 'sup_cat_name'] print("categories under %s are deleted!" % cat['sup_cat_name']) return run_single_query(query)
def add_video(video, file_full_location): check_input_manager('video', video, ['username', 'video_name', 'format']) key_query = "(username" value_query = "('%s'" % video['username'] for k, v in video.items(): if k in ("video_name", "format", "status"): key_query += "," + k value_query += ",'%s'" % video[k] elif k in ("fps", "num_frames"): key_query += "," + k value_query += ",%s" % video[k] key_query += ")" value_query += ")" query = "INSERT INTO video %s VALUES %s" % (key_query, value_query) print("Video %s is added!" % video['video_name']) video_num = run_single_query(query) video_id = 'video-' + str(video_num) bucket_name = 'videos' if make_bucket(bucket_name) is None: print("Creating bucket failed!") video = {'video_id': video_id} del_video(video) return if not uploader(bucket_name, video_id, file_full_location): print("Upload video failed!") video = {'video_id': video_id} del_video(video) return num_frames, fps, v_width, v_height = save_frames(video_id, file_full_location) if num_frames > 0: video = { 'video_id': video_num, 'fps': fps, 'num_frames': num_frames, 'width': v_width, 'height': v_height } update_video(video) else: print("Upload frames failed!") video = {'video_id': video_num} del_video(video) return return video_id
def add_server(server): check_input_manager('server', server, ['endpoint', 'status']) key_query = "(endpoint" value_query = "('%s'" % server['endpoint'] for k, v in server.items(): if k in ('status', 'desc'): key_query += "," + k value_query += ",'%s'" % server[k] key_query += ")" value_query += ")" query = "INSERT INTO function %s VALUES %s" % (key_query, value_query) print("server %s is added!" % server['endpoint']) return run_single_query(query)
def add_category(cat): if 'name' not in cat: print("Invalid category information to add") return None key_query = "(name" value_query = "('%s'" % cat['name'] for k, v in cat.items(): if k == "sup_cat_name": key_query += "," + k value_query += ",'%s'" % cat[k] key_query += ")" value_query += ")" query = "INSERT INTO category %s VALUES %s" % (key_query, value_query) cat_id = run_single_query(query) print("category %s is added!" % cat_id) return cat_id
def del_annotation(anno): if 'annotation_id' in anno: query = "DELETE FROM annotation WHERE annotation_id='%s'" % ( anno['annotation_id']) print("annotation_id %s is deleted!" % anno['annotation_id']) else: check_input_manager('annotation', anno, ['video_id']) query = "DELETE FROM annotation WHERE video_id='%s'" % ( anno['video_id']) if 'job_id' in anno: query += " AND job_id='%s'" % anno['job_id'] elif 'entity_id' in anno: query += " AND entity_id='%s'" % anno['entity_id'] elif 'frame_num' in anno: query += " AND frame_num='%s'" % anno['frame_num'] elif 'cat_id' in anno: query += " AND cat_id='%s'" % anno['cat_id'] print("annotations from video %s are deleted!" % anno['video_id']) return run_single_query(query)
def add_user(user): check_input_manager('user', user, ['username', 'password', 'role']) check_user = make_bucket(user['username']) if check_user is None: print("Create bucket failed!") return elif not check_user: print("Username existed!") return key_query = "(username" value_query = "('%s'" % user['username'] for k, v in user.items(): if k in ("password", "role"): key_query += "," + k value_query += ",'%s'" % user[k] key_query += ")" value_query += ")" query = "INSERT INTO user %s VALUES %s" % (key_query, value_query) print("User %s is added!" % user['username']) return run_single_query(query)
def del_server(server): check_input_manager('server', server, ['server_id']) query = "DELETE FROM function WHERE server_id=%s" % \ (server['server_id']) print("server %s is deleted!" % server['server_id']) return run_single_query(query)
def add_entity(video_id): query = "INSERT INTO entity (video_id) VALUES (%s)" % video_id entity_id = run_single_query(query) print("entity %s is added!" % entity_id) return entity_id
def del_job(job): check_input_manager('job', job, ['job_id']) query = "DELETE FROM job WHERE job_id=%s" % \ (str(job['job_id'])) print("Job %s is deleted!" % job['job_id']) return run_single_query(query)