def gallery_edit(id): if request.method == 'POST': gallery = Gallery(eid = id).from_form(request.form) gallery.save() else: gallery = Gallery().get(id) return menu.render('form.html', form = gallery.as_form())
def getIdentity(self, descriptor): if descriptor is None: return -1 distance = [] for gallId in self.database: dists = [] for gallEmbedding in self.database[gallId].descriptors: dists.append(np.linalg.norm(descriptor - gallEmbedding)) distance.append( [gallId, np.amin(dists)] ) if not distance: return self.addNewGallery(Gallery([descriptor], maxDescriptors=self.maxDescriptors)) bestId, minDistance = min(distance, key=lambda v: v[1]) if self.threshold is not None: if minDistance < self.threshold: self.addToGallery(bestId, descriptor) return bestId else: return self.addNewGallery(Gallery([descriptor], maxDescriptors=self.maxDescriptors)) else: return bestId
def post(self): """ """ identity = self.get_argument('id') code = self.get_argument('code') name = self.get_argument('name') param = None session = x.data.orm.createSession() if session.query(Gallery).filter(Gallery.id == identity).count() == 0: param = Gallery(id=str(uuid.uuid4())) # 添加到数据库 session.add(param) # 写数据库,但并不提交 session.flush() else: param = session.query(Gallery).filter(Gallery.id == identity).one() # 设置对象信息 param.code = code param.name = name param.modifiedDate = datetime.datetime.now() session.add(param) session.commit() self.write('提交成功!<br/>') self.write(name + "<br />") self.write('<a href="list">返回列表</a>')
def load_from_json(logger): # Check if json file actually exists otherwise return empty array if os.path.isfile(json_file_path): with open(json_file_path, "r", encoding="utf-8") as save_file: result_dicts = json.load(save_file) result_galleries = [] # Convert dictionary back to gallery object... for dict in result_dicts: gallery = Gallery(logger) gallery.initialize_from_dictionary(dict) result_galleries.append(gallery) return result_galleries return []
def buildPersonIdentifier(args): detector = choosePersonDetector(args) bodyEmbeddingGenerator = chooseBodyEmbeddingGenerator(args) if detector is not Detector.mtcnn: checkPath(args.bodyDetectorWeights, "Body Detector weights file", True) if detector is Detector.yolo: checkPath(args.bodyDetectorConfig, "Body detector configuration file", True) checkPath(args.embeddingGeneratorWeights, "Embedding generator weights file", True) if args.embeddingGeneratorClasses is None: args.embeddingGeneratorClasses = 1041 if bodyEmbeddingGenerator is EmbeddingGenerator.alignedReId else 751 gallery = None if args.gallery is not None: checkPath(args.bodyDetectorWeights, "Gallery weights file", True) checkPath(args.ids, "Ids set file", True) ids = getIds(args.ids) gallery = loadEmbeddings(args.gallery, ids, args.galleryLocation, args.oneIndexed) gallery = { id: Gallery(embeddings) for id, embeddings in gallery.items() } if args.galleryLocation is None: raise ValueError( "No query location specified. It should be specified with --galleryLocation option." ) return PersonReidentifierService( detector=detector, detectionThreshold=getDetectionThreshold(args), embeddingGenerator=bodyEmbeddingGenerator, detectorWeights=args.bodyDetectorWeights, detectorConf=args.bodyDetectorConfig, embeddingGeneratorWeights=args.embeddingGeneratorWeights, embeddingGeneratorNumClasses=args.embeddingGeneratorClasses, gallery=gallery, databaseThreshold=args.openWorldThreshold, maxDescriptors=args.maxDescriptors)
def get(self, id=''): print 'id:' + id param = Gallery(id=id, name='', code='') if id == '': self.render("gallery/gallery-form.html", title="Form | Gallery", header="form", footer="footer text", param=param) else: session = x.data.orm.createSession() param = session.query(Gallery).filter_by(id=id).first() self.render("gallery/gallery-form.html", title="Form | Gallery", header="form", param=param)
def create_gallery_from_request(request, logger, check_gallery_name=True): # Extract the relevant information from the request name = request.form.get("gallery-name", '') gallery_images = [] # Note that when the user has not selected any images the file name will simply be empty... result = request.files.getlist("gallery-images") for data in result: if data.filename != "": gallery_images.append(data) # Check if the name is valid if not GalleryHelper.is_valid_gallery_name(name): raise Exception( "Invalid gallery name " + name + ". It should only contain numeric, alphanumeric, '_' , '-' or whitespaces" ) # Check if this gallery already exists if check_gallery_name and GalleryHelper.does_gallery_with_name_already_exist( name): raise Exception("The gallery name " + "'" + name + "'" + " does already exist") # Check if image names are valid for image in gallery_images: # Check file extension if not GalleryHelper.is_valid_image_file_extension(image.filename): raise Exception( "The image name " + "'" + image.filename + "'" + " has an invalid file extension. Only .png or .jpeg images are supported." ) # Next check also the image file name if not GalleryHelper.is_valid_image_name(image.filename): raise Exception( "The image name " + "'" + image.filename + "'" + " contains invalid Characters. It should only contain numeric, alphanumeric, '_' , '-' or whitespaces" ) # We need to convert the result from the dropdown "Yes", "No" to a boolean is_favourite_str = request.form.get("gallery-favourite", "No") is_favourite = GalleryHelper.get_is_favourite_from_str( is_favourite_str) new_gallery = Gallery( logger=logger, name=name, tags=StringHelper.parse_tags_from_text( request.form.get("gallery-tags", ''), logger), is_favourite=is_favourite, # See: https://pythonise.com/series/learning-flask/the-flask-request-object -> Multiple files section images=[], description=request.form.get("gallery-description", "")) # Create a folder for the gallery name gallery_path = base_path + name + "/" if not os.path.isdir(gallery_path): os.makedirs(gallery_path) if len(gallery_images) > 0: new_gallery.set_file_paths(absolute_base_path, gallery_images) # Save to disk GalleryHelper.save_images_to_disk(new_gallery, gallery_images, logger) return new_gallery
def telegramWebHook(): update = Update.de_json(request.get_json(force=True)) text = None if getattr(update.message, 'document'): gallery = Gallery().search(tgid = update.message.chat.id) if gallery: newfile = bot.getFile(update.message.document.file_id) file_name = update.message.document.file_id newfile.download(file_name) writed = False if os.path.exists(file_name): writed = write_file(file_name, read_file(file_name, storage = 'local', append_path = False), acl = 'public-read', mime_type = update.message.document.mime_type) thumbnail(file_name) os.remove(file_name) write_file('%s.json' % file_name, update.to_json()) if writed: file_id = File(gallery_eid = gallery.eid, file_id = update.message.document.file_id) file_id.save() sendLink = getattr(gallery, 'sendLink', None) if sendLink == 'True': text = 'File URL: %s' % url_for('image', file_id = file_id.eid, _external = True, disable_web_page_preview = True) else: text = 'Failed to download file' else: text = 'Gallery does not exist, please create first' pass if getattr(update.message, 'text'): args = update.message.text.split(' ', 2) if args[0] == '/register': text = 'Username:'******'Complete register: https://telegram.me/ACSGalleryBot?start=%s' % update.message.from_user.id else: text = 'User added to gallery' # set gallery permission at this point because i have chat id elif args[0] == '/start': if len(args) > 1 and int(args[1]) == int(update.message.chat.id): text = 'Username:'******'force_reply' : True }) else: text = update.to_json() elif getattr(update.message, 'reply_to_message'): if update.message.reply_to_message.text == 'Username:'******'Password:'******'force_reply' : True }) return 'ok' elif update.message.reply_to_message.text == 'Password:'******'User succesfuly registered' elif args[0] == '/create': if hasattr(update.message.chat, 'title'): gallery = Gallery().search(tgid = update.message.chat.id) if not gallery: gallery = Gallery(tgid = update.message.chat.id, title = update.message.chat.title).save() text = 'Gallery URL: %s' % url_for('gallery', id = gallery.eid, _external = True, _scheme = 'https') else: text = 'Bot only works in groups' elif args[0] == '/remove': gallery = Gallery().search(tgid = update.message.chat.id) if gallery: gallery.delete() text = 'Gallery deleted' else: text = 'Gallery is not registered' # TODO: Confirm elif args[0] == '/config': args.pop(0) gallery = Gallery.search(tgid = update.message.chat.id) if gallery: if len(args) == 0: text = g.config(update.message.chat.id) elif len(args) == 1: text = 'get one' text = g.config(update.message.chat.id, args[0]) else: text = g.config(update.message.chat.id, args[0], args[1]) else: text = 'Gallery is not registered' #else: # text = update.to_json() if text: bot.sendMessage(update.message.chat.id, text, disable_web_page_preview=True) return ""
def run(self): args = self.update.message.text.split(' ', 2) if args[0] == '/register': self.text = 'Username:'******'Complete register: https://telegram.me/ACSGalleryBot?start=%s' % self.user_id else: self.text = 'User added to gallery' # set gallery permission at this point because i have chat id elif args[0] == '/start': if len(args) > 1 and int(args[1]) == int(self.chat_id): self.text = 'Username:'******'reply_markup' : { 'force_reply' : True } } elif getattr(self.update.message, 'reply_to_message'): if self.update.message.reply_to_message.text == 'Username:'******'username', self.update.message.text) user.save() self.text = 'Password:'******'reply_markup' : { 'force_reply' : True } } return 'ok' elif self.update.message.reply_to_message.text == 'Password:'******'password', self.update.message.text) user.save() self.text = 'User succesfuly registered' elif args[0] == '/create': if hasattr(self.update.message.chat, 'title'): gallery = Gallery().search(tgid = self.chat_id) if not gallery: gallery = Gallery(tgid = self.chat_id, title = self.update.message.chat.title).save() self.text = 'Gallery URL: %s' % url_for('gallery', id = gallery.eid.value, _external = True, _scheme = 'https') else: self.text = 'Bot only works in groups' elif args[0] == '/remove': gallery = Gallery().search(tgid = self.chat_id) if gallery: gallery.delete() self.text = 'Gallery deleted' else: self.text = 'Gallery is not registered' # TODO: Confirm elif args[0] == '/settings': args.pop(0) gallery = Gallery().search(tgid = self.chat_id) if gallery: if len(args) == 0: self.text = gallery.as_dict() elif len(args) == 1: value = gallery.as_dict() if getattr(gallery, args[0]): self.text = getattr(gallery, args[0]).value else: self.text = 'Setting %s not found' % args[0] else: value = ' '.join(args[1:]) gallery.setattr(args[0], value) gallery.save() self.text = 'Setting %s set to %s' % (args[0], value) else: self.text = 'Gallery is not registered'