def from_database_and_key(cls, dataset, image_key): debug_utils.DebugUtils.tic() with app.app_context(): db_session = database_controller.get_db(app.config) rows = db_session.execute(select_image_cql, [dataset, image_key]) rows = list(rows) if len(rows) == 0: app.logger.warning( 'Did not found doc with the provided dataset and ID.') return None elif len(rows) == 1: r = rows[0] # get image now r_img = db_session.execute(select_image_img_cql, [image_key]) r_img = list(r_img) image = np.fromstring(r_img[0].img, dtype=np.uint8) image = cv2.imdecode(image, cv2.IMREAD_COLOR) # debug_utils.DebugUtils.toc("from_database_and_key") # image = image.reshape(r.height, r.width, 3) return cls(r.phash, r.dataset, image, r.name, r.annotation, r.category, r.partition, r.fold, r.last_modified) elif len(rows) > 1: app.logger.warning( 'Query error: the same image cannot appear twice.') return None
def list_datasets(): with app.app_context(): db_session = database_controller.get_db(app.config) rows = db_session.execute( "SELECT name, tags, annotation_labels, image_categories, last_modified FROM " + TABLE) return list(rows)
def list_images_phash_from_dataset(dataset_name): with app.app_context(): db_session = database_controller.get_db(app.config) cql = "SELECT phash FROM " + TABLE_IMAGES + " WHERE dataset=\'" + dataset_name + "\'" rows = db_session.execute(cql, timeout=60) imgs = list(rows) return imgs
def delete(self): with app.app_context(): db_session = database_controller.get_db(app.config) cql = "DELETE FROM " + TABLE + " WHERE name=\'" + self.dataset_name + "\'" res = db_session.execute(cql) return (True, "")
def delete_image(dataset_name, image_id): with app.app_context(): db_session = database_controller.get_db(app.config) cql = "DELETE FROM " + TABLE_IMAGES_IMG + " WHERE phash=\'" + image_id + "\'" res = db_session.execute(cql) cql = "DELETE FROM " + TABLE_IMAGES + " WHERE dataset=\'" + dataset_name + "\'" + " AND phash=\'" + image_id + "\'" res = db_session.execute(cql) return (True, "")
def delete_all_images(dataset_name): with app.app_context(): db_session = database_controller.get_db(app.config) # first, remove all imgs from images_img table phashs = ImageModel.list_images_phash_from_dataset(dataset_name) for p in phashs: cql = "DELETE FROM " + TABLE_IMAGES_IMG + " WHERE phash=\'" + p.phash + "\'" res = db_session.execute(cql) cql = "DELETE FROM " + TABLE_IMAGES + " WHERE dataset=\'" + dataset_name + "\'" res = db_session.execute(cql) return (True, "")
def get_all(dataset, ref_type): TABLE, field = RefCountModel.get_table_and_field_from_type(ref_type) with app.app_context(): db_session = database_controller.get_db(app.config) cql = "SELECT "+field+", ref_count FROM "+TABLE+" WHERE dataset=\'"+dataset+"\'" rows = db_session.execute(cql) values = [] for r in rows: if r.ref_count > 0: if ref_type == RefCountType.CATEGORY: values.append(r.category) elif ref_type == RefCountType.LABEL: values.append(r.label) return values
def __init__(self, username, password_hash, email, points=0, registration_date=None): self.username = username self.password_hash = password_hash self.email = email self.points = points if registration_date is None: self.registration_date = datetime.datetime.now() else: self.registration_date = registration_date with app.app_context(): self.db_session = database_controller.get_db(app.config)
def from_category(cls, dataset, category): TABLE = "category_ref_count" with app.app_context(): db_session = database_controller.get_db(app.config) cql = "SELECT dataset, " + \ "category, " + \ "ref_count FROM "+TABLE+" WHERE dataset=\'"+dataset+"\' AND category=\'"+category+"\'" rows = db_session.execute(cql) rows = list(rows) if len(rows) == 0: # not yet in database return cls(dataset, category, RefCountType.CATEGORY) elif len(rows) == 1: r = rows[0] return cls(dataset, category, RefCountType.CATEGORY, r.ref_count) elif len(rows) > 1: app.logger.warning('Query error: the same key cannot appear twice.') return None
def get_db(config): """ This functions connect to the Cassandra database and kept in the flask app """ KEYSPACE = app.config["KEYSPACE"] CLUSTER_IP = app.config["CLUSTER_IP"] global db_global with app.app_context(): if db_global is None: app.logger.info("database connection done again with ip: %s", CLUSTER_IP) cluster = Cluster([CLUSTER_IP], connect_timeout=1000) session = cluster.connect(KEYSPACE) session.default_timeout = 100 db_global = session return db_global
def from_label(cls, dataset, label): TABLE = "label_ref_count" with app.app_context(): db_session = database_controller.get_db(app.config) cql = "SELECT dataset, " + \ "label, " + \ "ref_count FROM "+TABLE+" WHERE dataset=\'"+dataset+"\' AND label=\'"+label+"\'" rows = db_session.execute(cql) rows = list(rows) if len(rows) == 0: # not yet in database return cls(dataset, label, RefCountType.LABEL) elif len(rows) == 1: r = rows[0] return cls(dataset, label, RefCountType.LABEL, r.ref_count) elif len(rows) > 1: app.logger.warning('Query error: the same key cannot appear twice.') return None
def __init__(self, dataset_name, tags=[], annotation_labels=[], image_categories=[], last_modified=None): self.dataset_name = dataset_name self.tags = tags if annotation_labels is None: self.annotation_labels = [] else: self.annotation_labels = annotation_labels if image_categories is None: self.image_categories = [] else: self.image_categories = image_categories self.last_modified = last_modified with app.app_context(): self.db_session = database_controller.get_db(app.config)
def __init__(self, phash, dataset_name, image, name="", bboxes=[], category="", partition=0, fold=0, last_modified=None): if dataset_name is None or dataset_name == "": raise Exception("An image needs needs a dataset name.") self.dataset_name = dataset_name self.name = name if image is None or image.shape[0] == 0: raise Exception("An image needs an image.") self.width = image.shape[1] self.height = image.shape[0] self.image = image if phash == "": # given the image, we compute the percepetual hash to use as key self.phash = self.compute_phash(image) else: self.phash = phash self.bboxes = bboxes if category == "": self.category = "default" else: self.category = category self.partition = partition self.fold = fold self.last_modified = last_modified with app.app_context(): db_session = database_controller.get_db(app.config)
def from_username(cls, username): with app.app_context(): db_session = database_controller.get_db(app.config) cql = "SELECT username, " + \ "password_hash, " + \ "email, " + \ "points, " + \ "registration_date FROM "+TABLE+" WHERE username=\'"+username+"\'" rows = db_session.execute(cql) rows = list(rows) if len(rows) == 0: return None elif len(rows) == 1: r = rows[0] return cls(r.username, r.password_hash, r.email, r.points, r.registration_date) elif len(rows) > 1: app.logger.warning( 'Query error: the same username cannot appear twice.') return None
def from_name(cls, dataset_name): with app.app_context(): db_session = database_controller.get_db(app.config) cql = "SELECT name, " + \ "tags, " + \ "annotation_labels, " + \ "image_categories, " + \ "last_modified FROM "+TABLE+" WHERE name=\'"+dataset_name+"\'" rows = db_session.execute(cql) rows = list(rows) if len(rows) == 0: app.logger.warning( 'Did not found doc with the provided dataset.') return None elif len(rows) == 1: r = rows[0] return cls(r.name, r.tags, r.annotation_labels, r.image_categories, r.last_modified) elif len(rows) > 1: app.logger.warning( 'Query error: the same dataset cannot appear twice.') return None
def list_images_from_dataset(dataset_name): debug_utils.DebugUtils.tic() with app.app_context(): db_session = database_controller.get_db(app.config) # cql = "SELECT phash, "+ \ # "width, " + \ # "height, " + \ # "name, " + \ # "annotation, "+ \ # "category, " + \ # "partition, " + \ # "fold, " + \ # "last_modified FROM "+TABLE_IMAGES+" WHERE dataset=\'"+dataset_name+"\'" rows = db_session.execute(select_image_from_dataset_cql, [dataset_name], timeout=100) imgs = list(rows) debug_utils.DebugUtils.toc( "list_images_from_dataset ({})".format(dataset_name)) return imgs
from annotator_supreme import app from annotator_supreme.controllers import database_controller from annotator_supreme.controllers import debug_utils from annotator_supreme.models.bbox_model import BBox from PIL import Image import hashlib import imagehash import time import cv2 import numpy as np import datetime TABLE_IMAGES = "images" TABLE_IMAGES_IMG = "images_img" with app.app_context(): db_session = database_controller.get_db(app.config) upsert_image_cql = db_session.prepare(" INSERT INTO "+TABLE_IMAGES+ \ " (phash, "+ \ "dataset, " + \ "width, " + \ "height, " + \ "name, " + \ "annotation, " + \ "category, " + \ "partition, " + \ "fold, " + \ "last_modified) " + \ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ")