Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from PIL import Image | |
| import os | |
| import subprocess | |
| import glob | |
| def get_latest_file(directory): | |
| # 获取所有的文件和文件夹 | |
| all_items = glob.glob(os.path.join(directory, '*')) | |
| # 分离文件和文件夹 | |
| folders = [item for item in all_items if os.path.isdir(item)] | |
| files = [item for item in all_items if os.path.isfile(item)] | |
| # 先检查文件夹,如果有文件夹,则返回最新的文件夹 | |
| if folders: | |
| latest_folder = max(folders, key=os.path.getmtime) | |
| return latest_folder | |
| # 如果没有文件夹,那么检查文件,并返回最新的文件 | |
| elif files: | |
| latest_file = max(files, key=os.path.getmtime) | |
| return latest_file | |
| # 如果没有文件和文件夹,那么返回None | |
| else: | |
| return None | |
| def save_image(image): | |
| # 指定目录 | |
| directory = '/Users/lixiangfang/Desktop/Surf code/Document-Dewarping-with-Control-Points/Source/testdata' | |
| # 检查目录是否存在,不存在则创建 | |
| if not os.path.exists(directory): | |
| os.makedirs(directory) | |
| # 保存图片到指定目录 | |
| image.save(os.path.join(directory, 'new_image_1.jpg')) | |
| def process_image(image): | |
| # 使用PIL.Image将numpy数组转换回图像 | |
| pil_image = Image.fromarray(image) | |
| # 将图像保存到指定目录 | |
| save_image(pil_image) | |
| program_path = '/Users/lixiangfang/Desktop/Surf code/Document-Dewarping-with-Control-Points/Source/test.py' | |
| subprocess.run(['python3', program_path, '--data_path_test=./testdata']) | |
| directory_image = '/Users/lixiangfang/Desktop/Surf code/Document-Dewarping-with-Control-Points/Source/flat' # 你的目录路径 | |
| latest_file = get_latest_file(get_latest_file(directory_image)) | |
| image_path = f'{latest_file}/144/test/new_image_1.jpg' | |
| new_image = Image.open(image_path) | |
| return new_image | |
| iface = gr.Interface(fn=process_image, | |
| inputs="image", | |
| outputs="image", | |
| title="Document dewarping platform", | |
| description="This page recognises wrinkled documents and processes them into flat ones, you can upload your local images and download the processed files.", | |
| allow_flagging="never" | |
| ) | |
| iface.launch() | |