def close(self): if self.opened: lock = FileLock(self.dbfile) lock.acquire() # Update database. if self.root != None: self.db[self.key] = self.root self.db.close() # Release lock. lock.release() # Set object to closed state. self.opened = False
def make_style_transfer(message): project_dir_path = "/tmp/TeleBotProject/files_folder/deerace/" user_dir_path = project_dir_path + str(message.from_user.id) + "/" try: if os.path.isdir(project_dir_path) == False: mode = 0o755 os.makedirs(project_dir_path, mode) if os.path.isdir(user_dir_path) == False: mode = 0o755 os.makedirs(user_dir_path, mode) # dicrectory for current user except OSError: print("Creation of the directory %s failed" % user_dir_path) else: print("Successfully created the directory %s" % user_dir_path) print(message.photo[-1]) try: cid = message.chat.id file_info = bot.get_file(message.photo[-1].file_id) downloaded_file = bot.download_file(file_info.file_path) is_content_photo = False downloaded_file_path = '' if os.path.isfile(user_dir_path + "style_path.txt") == False: downloaded_file_path = user_dir_path + "style_" + message.photo[ -1].file_id + ".jfif" else: downloaded_file_path = user_dir_path + "content_" + message.photo[ -1].file_id + ".jfif" is_content_photo = True print("downloaded_file_path = ", downloaded_file_path) with open(downloaded_file_path, 'wb') as new_file: new_file.write(downloaded_file) if is_content_photo == True: style_image_file_path = "" # path to style photo style_path_file_handler = open( user_dir_path + "style_path.txt", "r") style_image_file_path = style_path_file_handler.readline( ).strip() ############################################################## #### Better create with exception throw, but i'm so lazy :(#### if style_image_file_path == None or len( style_image_file_path) == 0: bot.reply_to( message, ":ERROR: Problem with STYLE photo.\n" + "Please use command /deleteStyleImage and upload STYLE photo again" ) return elif os.path.isfile(style_image_file_path) == False: bot.reply_to( message, ":ERROR: Can't find file with style image path.\n" + "Please use command /deleteStyleImage and upload STYLE photo again" ) return #### Better create with exception throw, but i'm so lazy :(#### ############################################################## bot.reply_to( message, "*CONTENT PHOTO GETTED*\n\n" + "I get CONTENT PHOTO, now wait, i make image and send it to you\n\n" + "*MAKE STEP->* Wait i make stylize and send photo to you ", parse_mode='Markdown') #bot.reply_to(message, "I get CONTENT PHOTO, now wait, i make image and send it to you") # for some reason the 'upload_photo' status isn't quite working (doesn't show at all) bot.send_chat_action(message.chat.id, 'typing') protected_file_path = os.path.join(project_dir_path, "lock_file.tmp") print("Protecting file: {}".format(protected_file_path)) file_lock_handlers = FileLock(protected_file_path) with file_lock_handlers: with open(protected_file_path, "a") as file_handler: #object_style_transfer_facade = StyleTransferFacade(style_image_path=user_dir_path + "style.jfif", # content_image_path=downloaded_file_path) object_style_transfer_facade = StyleTransferFacade( style_image_path=style_image_file_path, content_image_path=downloaded_file_path) object_style_transfer_facade.transfer_style( out_image_path=user_dir_path + "out_image.jpg") del object_style_transfer_facade bot.send_photo( message.chat.id, open(user_dir_path + "out_image.jpg", 'rb'), reply_markup=hideBoard, reply_to_message_id=message.message_id ) # send file and hide keyboard, after image is sent print("bot.send_photo") file_handler.flush() else: with open(user_dir_path + "style_path.txt", 'w') as style_path_file_handler: style_path_file_handler.write(downloaded_file_path) bot.reply_to( message, "*STYLE PHOTO GETTED*\n\n" + "I get STYLE photo, now i need CONTENT PHOTO\n\n" + "*MAKE STEP->*Please send me one more photo, witch i will stylize", parse_mode='Markdown') #bot.reply_to(message, "WTF is this?\nhttps://www.youtube.com/watch?v=78_W7-3oJD8") print("File uploaded path = ", downloaded_file_path) except Exception as e: bot.reply_to(message, e)
def __init__(self, filename, lock=0, cache=0, min_recordsize=MINRECORDSIZE, readonly=0, recover=0): """ Create an instance using filename as data file. If lock is true, a filelock will be enabled until the file is closed. cache controls whether to enable a cache or not (should only be used where OS level caches are not available). min_recordsize can be set to have all records be padded to at least this size (might reduce overhead due to reallocation of records that have become too small for updates). readonly can be set to true to have existing files opened in read-only mode. Writes will then cause an IOError, opening a non-existing file in read-only mode will too. Opening the storage file in recover mode will disable some of the checks normally done. This flag should only be used if a previous normal opening failed with a hint to run recovery. """#' self.readonly = readonly if _debug: log.call(SYSTEM_DEBUG) # Adjust min_recordsize if min_recordsize < MINRECORDSIZE: min_recordsize = MINRECORDSIZE if min_recordsize % BLOCKSIZE != 0: min_recordsize = (min_recordsize / BLOCKSIZE + 1) * BLOCKSIZE self.min_recordsize = min_recordsize # Lock the file if lock: self.filelock = filelock = FileLock.FileLock(filename) # This may raise a FileLock.Error if recover: try: filelock.lock() except FileLock.Error: filelock.remove_lock() filelock.lock() else: filelock.lock() # Open it: # first try for an existing file, then revert to creating a new one if readonly: mode = 'rb' else: mode = 'r+b' self.filename = filename try: # Existing file self.file = file = open(filename, mode) file.seek(0, 2) self.EOF = EOF = file.tell() except IOError, why: if readonly: raise IOError, why # New file: write header and state marker if _debug: log(SYSTEM_INFO, 'Creating a new storage file %s' % filename) self.file = file = open(filename, 'w+b') self.write_fileheader(file) self.mark(COLD) EOF = file.tell() if EOF % BLOCKSIZE != 0: # pad to block boundary file.write(' ' * (BLOCKSIZE - EOF % BLOCKSIZE)) EOF = file.tell() self.EOF = EOF self.is_new = 1
def make_photo_deerace(message): project_dir_path = "/tmp/TeleBotProject/files_folder/deerace/" user_dir_path = project_dir_path + str(message.from_user.id) + "/" try: if os.path.isdir(project_dir_path) == False: mode = 0o755 os.makedirs(project_dir_path, mode) if os.path.isdir(user_dir_path) == False: mode = 0o755 os.makedirs(user_dir_path, mode) # dicrectory for current user os.makedirs(user_dir_path + "deerace/datasets/", mode) # dicrectory for current user os.makedirs(user_dir_path + "deerace/results/", mode) # dicrectory for current user except OSError: print("Creation of the directory %s failed" % user_dir_path) else: print("Successfully created the directory %s" % user_dir_path) print(message.photo[-1]) try: cid = message.chat.id file_info = bot.get_file(message.photo[-1].file_id) downloaded_file = bot.download_file(file_info.file_path) photo_name = "" photo_name = "deerace_" + message.photo[-1].file_id + ".jfif" downloaded_file_path = '' downloaded_file_path = user_dir_path + photo_name print("downloaded_file_path = ", downloaded_file_path) with open(downloaded_file_path, 'wb') as new_file: new_file.write(downloaded_file) bot.reply_to( message, "I get photo for DeERace, wait a minute i processing them...") # for some reason the 'upload_photo' status isn't quite working (doesn't show at all) bot.send_chat_action(message.chat.id, 'typing') protected_file_path = os.path.join(project_dir_path, "lock_file.tmp") print("Protecting file: {}".format(protected_file_path)) file_lock_handlers = FileLock(protected_file_path) with file_lock_handlers: with open(protected_file_path, "a") as file_handler: deerace_object = DeERace( images_with_photo_path=downloaded_file_path, photo_name=photo_name, user_dir_path=user_dir_path) face_find_count, list_of_file_paths_with_faces = deerace_object.find_face_and_processing( ) if face_find_count == 0: bot.reply_to( message, "Wait wait wait. I don't see faces in this photo! \n" + "Please send me photo with faces") return else: bot.reply_to( message, "Ok i see " + str(face_find_count) + " faces.\n" + "When i prosecced all faces, i send result to you") for dict_from_list in list_of_file_paths_with_faces: for keys_from_dict in dict_from_list.keys(): bot.send_photo( message.chat.id, open( dict_from_list[keys_from_dict]['real'], 'rb'), caption="Real face", reply_markup=hideBoard, reply_to_message_id=message.message_id ) # send file and hide keyboard, after image is sent bot.send_photo( message.chat.id, open( dict_from_list[keys_from_dict]['fake'], 'rb'), caption="Fake face", reply_markup=hideBoard, reply_to_message_id=message.message_id ) # send file and hide keyboard, after image is sent print(dict_from_list[keys_from_dict]['real']) print(dict_from_list[keys_from_dict]['fake']) print() file_handler.flush() except Exception as e: bot.reply_to(message, e)