def __init__(self): self.__sessions = {} # 保存 sessions self.__batchs = {} # 保存批处理 .. self.__mis = {} # 保存正在进行的转换进程 .. self.__next_sid = 0 self.__lock = threading.Lock() global cfg if sys.platform.find('win32') == 0: self.__mis_root = "c:/store/imgs" # 存储转换后的图片 .. cmd_path = self.__mis_root.replace('/', '\\') os.system('mkdir ' + cmd_path) # cmd.exe 需要 else: if "imgpath" in cfg: self.__mis_root = cfg["imgpath"] else: home = os.getenv('HOME') self.__mis_root = home + '/store/imgs' os.system('mkdir -p ' + self.__mis_root) self.__users = {} dbname = 'labels.db' if 'dbfname' in cfg: dbname = cfg['dbfname'] self.__db = DB(self.__mis_root + '/' + dbname) handlers = [ (r'/', HelpHandler), (r'/login', LoginHandler), (r'/pic', SinglePicture), (r'/stream', CreateSessionHandler), (r'/stream/([0-9]+)', DelSessionHandler), (r'/stream/([0-9]+)/(.*)', SessionHandler), (r'/batch', CreateBatchHandler), (r'/batch/([0-9]+)', DelBatchHandler), (r'/batch/([0-9]+)/(.*)', BatchHandler), (r'/retrain', RetrainIndexHandler), (r'/retrain/media2image', RetrainMedia2ImageHandler), (r'/retrain/cf', RetrainShowingHandler), (r'/retrain/api/next', RetrainNextImageHandler), # 获取下一张图片的分类结果 .. (r'/retrain/api/confirm', RetrainImageCfHandler), # 确认分类结果,PUT (r'/retrain/api/cancel', RetrainImageCancelHandler), # 撤销 (r'/retrain/api/skip', RetrainImageSkipHandler), # 跳过 (r'/imgs/(.*)', NoCacheHandler, { 'path': self.__mis_root }), # 图片文件.. (r'/get_labels', GetClassifierLabelsHandler), # 返回所有的标签,json ] tornado.web.Application.__init__(self, handlers, cookie_secret="abcd")
def __init__(self): self.__sessions = {} # 保存 sessions self.__batchs = {} # 保存批处理 .. self.__mis = {} # 保存正在进行的转换进程 .. self.__next_sid = 0 self.__lock = threading.Lock() global cfg if sys.platform.find('win32') == 0: self.__mis_root = "c:/store/imgs" # 存储转换后的图片 .. cmd_path = self.__mis_root.replace('/', '\\') os.system('mkdir ' + cmd_path) # cmd.exe 需要 else: if "imgpath" in cfg: self.__mis_root = cfg["imgpath"] else: home = os.getenv('HOME') self.__mis_root = home + '/store/imgs' os.system('mkdir -p ' + self.__mis_root) self.__users = {} dbname = 'labels.db' if 'dbfname' in cfg: dbname = cfg['dbfname'] self.__db = DB(self.__mis_root + '/' + dbname) handlers = [ (r'/', HelpHandler), (r'/login', LoginHandler), (r'/pic', SinglePicture), (r'/stream', CreateSessionHandler), (r'/stream/([0-9]+)', DelSessionHandler), (r'/stream/([0-9]+)/(.*)', SessionHandler), (r'/batch', CreateBatchHandler), (r'/batch/([0-9]+)', DelBatchHandler), (r'/batch/([0-9]+)/(.*)', BatchHandler), (r'/retrain', RetrainIndexHandler), (r'/retrain/media2image', RetrainMedia2ImageHandler), (r'/retrain/cf', RetrainShowingHandler), (r'/retrain/api/next', RetrainNextImageHandler), # 获取下一张图片的分类结果 .. (r'/retrain/api/confirm', RetrainImageCfHandler), # 确认分类结果,PUT (r'/retrain/api/cancel', RetrainImageCancelHandler), # 撤销 (r'/retrain/api/skip', RetrainImageSkipHandler), # 跳过 (r'/imgs/(.*)', NoCacheHandler, {'path': self.__mis_root } ), # 图片文件.. (r'/get_labels', GetClassifierLabelsHandler), # 返回所有的标签,json ] tornado.web.Application.__init__(self, handlers, cookie_secret="abcd")
class Application(tornado.web.Application): def __init__(self): self.__sessions = {} # 保存 sessions self.__batchs = {} # 保存批处理 .. self.__mis = {} # 保存正在进行的转换进程 .. self.__next_sid = 0 self.__lock = threading.Lock() global cfg if sys.platform.find('win32') == 0: self.__mis_root = "c:/store/imgs" # 存储转换后的图片 .. cmd_path = self.__mis_root.replace('/', '\\') os.system('mkdir ' + cmd_path) # cmd.exe 需要 else: if "imgpath" in cfg: self.__mis_root = cfg["imgpath"] else: home = os.getenv('HOME') self.__mis_root = home + '/store/imgs' os.system('mkdir -p ' + self.__mis_root) self.__users = {} dbname = 'labels.db' if 'dbfname' in cfg: dbname = cfg['dbfname'] self.__db = DB(self.__mis_root + '/' + dbname) handlers = [ (r'/', HelpHandler), (r'/login', LoginHandler), (r'/pic', SinglePicture), (r'/stream', CreateSessionHandler), (r'/stream/([0-9]+)', DelSessionHandler), (r'/stream/([0-9]+)/(.*)', SessionHandler), (r'/batch', CreateBatchHandler), (r'/batch/([0-9]+)', DelBatchHandler), (r'/batch/([0-9]+)/(.*)', BatchHandler), (r'/retrain', RetrainIndexHandler), (r'/retrain/media2image', RetrainMedia2ImageHandler), (r'/retrain/cf', RetrainShowingHandler), (r'/retrain/api/next', RetrainNextImageHandler), # 获取下一张图片的分类结果 .. (r'/retrain/api/confirm', RetrainImageCfHandler), # 确认分类结果,PUT (r'/retrain/api/cancel', RetrainImageCancelHandler), # 撤销 (r'/retrain/api/skip', RetrainImageSkipHandler), # 跳过 (r'/imgs/(.*)', NoCacheHandler, {'path': self.__mis_root } ), # 图片文件.. (r'/get_labels', GetClassifierLabelsHandler), # 返回所有的标签,json ] tornado.web.Application.__init__(self, handlers, cookie_secret="abcd") def get_user(self, who): ''' 用于存储 user 相关的 ''' if who in self.__users: return self.__users[who] else: user = UserTask(who, self.__mis_root, self.__db) self.__users[who] = user return user def cnt_total(self): return self.__db.get_total() def cnt_labeled(self): return self.__db.get_labeled() def next_image(self, user): ut = self.get_user(user) return ut.next_image() def save_cf_result(self, key, label, user): ''' 保存标定结果 ''' ut = self.get_user(user) global cf title = cf.title(int(label)) ut.save_image_result(key, label, title) def skip(self, key, user): ut = self.get_user(user) ut.skip(key) def cancel_last(self, user): ''' 撤销最后的保存 ''' ut = self.get_user(user) ut.cancel() def next_sid(self): self.__next_sid += 1 return str(self.__next_sid) def get_session(self, sid): if sid in self.__sessions: return self.__sessions[sid] else: return None def cb(self, rc): #print rc pass def list_sessions(self): ss = [] for s in self.__sessions: ss.append(self.__sessions[s].descr()) return ss def create_session(self, url, interval, notify_url): global cb sid = self.next_sid() s = Session(cb, self, sid, url = url, interval = interval, notify_url = notify_url) self.__sessions[sid] = s return sid def destory_session(self, sid): if sid in self.__sessions: s = self.__sessions[sid] s.close() del(self.__sessions[sid]) return True else: return False def query_session(self, sid, begin, end): if sid not in self.__sessions: return None s = self.__sessions[sid] return s.results(begin, end) def is_running(self, sid): if sid not in self.__sessions: return False s = self.__sessions[sid] return s.is_running() def create_media2image(self, user, mid, fname): dst_path = self.__mis_root + '/' + mid try: os.mkdir(dst_path) except: pass mi = Media2Image(user, fname, (256, 256), 0.2, dst_path) self.__mis[mid] = mi return mi def get_media2image(self, mid): if mid in self.__mis: return self.__mis[mid] else: return None def destroy_media2image(self, mid): mi = self.get_media2image(mid) if mi is not None: mi.stop() del self.__mis[mid]
def get_database_record(fname): db = DB(fname) return db.labeled_all()
class Application(tornado.web.Application): def __init__(self): self.__sessions = {} # 保存 sessions self.__batchs = {} # 保存批处理 .. self.__mis = {} # 保存正在进行的转换进程 .. self.__next_sid = 0 self.__lock = threading.Lock() global cfg if sys.platform.find('win32') == 0: self.__mis_root = "c:/store/imgs" # 存储转换后的图片 .. cmd_path = self.__mis_root.replace('/', '\\') os.system('mkdir ' + cmd_path) # cmd.exe 需要 else: if "imgpath" in cfg: self.__mis_root = cfg["imgpath"] else: home = os.getenv('HOME') self.__mis_root = home + '/store/imgs' os.system('mkdir -p ' + self.__mis_root) self.__users = {} dbname = 'labels.db' if 'dbfname' in cfg: dbname = cfg['dbfname'] self.__db = DB(self.__mis_root + '/' + dbname) handlers = [ (r'/', HelpHandler), (r'/login', LoginHandler), (r'/pic', SinglePicture), (r'/stream', CreateSessionHandler), (r'/stream/([0-9]+)', DelSessionHandler), (r'/stream/([0-9]+)/(.*)', SessionHandler), (r'/batch', CreateBatchHandler), (r'/batch/([0-9]+)', DelBatchHandler), (r'/batch/([0-9]+)/(.*)', BatchHandler), (r'/retrain', RetrainIndexHandler), (r'/retrain/media2image', RetrainMedia2ImageHandler), (r'/retrain/cf', RetrainShowingHandler), (r'/retrain/api/next', RetrainNextImageHandler), # 获取下一张图片的分类结果 .. (r'/retrain/api/confirm', RetrainImageCfHandler), # 确认分类结果,PUT (r'/retrain/api/cancel', RetrainImageCancelHandler), # 撤销 (r'/retrain/api/skip', RetrainImageSkipHandler), # 跳过 (r'/imgs/(.*)', NoCacheHandler, { 'path': self.__mis_root }), # 图片文件.. (r'/get_labels', GetClassifierLabelsHandler), # 返回所有的标签,json ] tornado.web.Application.__init__(self, handlers, cookie_secret="abcd") def get_user(self, who): ''' 用于存储 user 相关的 ''' if who in self.__users: return self.__users[who] else: user = UserTask(who, self.__mis_root, self.__db) self.__users[who] = user return user def cnt_total(self): return self.__db.get_total() def cnt_labeled(self): return self.__db.get_labeled() def next_image(self, user): ut = self.get_user(user) return ut.next_image() def save_cf_result(self, key, label, user): ''' 保存标定结果 ''' ut = self.get_user(user) global cf title = cf.title(int(label)) ut.save_image_result(key, label, title) def skip(self, key, user): ut = self.get_user(user) ut.skip(key) def cancel_last(self, user): ''' 撤销最后的保存 ''' ut = self.get_user(user) ut.cancel() def next_sid(self): self.__next_sid += 1 return str(self.__next_sid) def get_session(self, sid): if sid in self.__sessions: return self.__sessions[sid] else: return None def cb(self, rc): #print rc pass def list_sessions(self): ss = [] for s in self.__sessions: ss.append(self.__sessions[s].descr()) return ss def create_session(self, url, interval, notify_url): global cb sid = self.next_sid() s = Session(cb, self, sid, url=url, interval=interval, notify_url=notify_url) self.__sessions[sid] = s return sid def destory_session(self, sid): if sid in self.__sessions: s = self.__sessions[sid] s.close() del (self.__sessions[sid]) return True else: return False def query_session(self, sid, begin, end): if sid not in self.__sessions: return None s = self.__sessions[sid] return s.results(begin, end) def is_running(self, sid): if sid not in self.__sessions: return False s = self.__sessions[sid] return s.is_running() def create_media2image(self, user, mid, fname): dst_path = self.__mis_root + '/' + mid try: os.mkdir(dst_path) except: pass mi = Media2Image(user, fname, (256, 256), 0.2, dst_path) self.__mis[mid] = mi return mi def get_media2image(self, mid): if mid in self.__mis: return self.__mis[mid] else: return None def destroy_media2image(self, mid): mi = self.get_media2image(mid) if mi is not None: mi.stop() del self.__mis[mid]