Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import zipfile | |
| import os | |
| from PIL import Image | |
| # Hàm giải nén thư mục ảnh và lấy đường dẫn | |
| def extract_images(zip_file): | |
| # Tạo thư mục lưu ảnh | |
| extract_folder = "./uploaded_images/" | |
| os.makedirs(extract_folder, exist_ok=True) | |
| # Giải nén zip file | |
| with zipfile.ZipFile(zip_file.name, 'r') as zip_ref: | |
| zip_ref.extractall(extract_folder) | |
| # Lấy danh sách các ảnh đã giải nén | |
| image_paths = [os.path.join(extract_folder, file) for file in os.listdir(extract_folder) if file.endswith(('.png', '.jpg', '.jpeg'))] | |
| # Mở và trả về ảnh | |
| images = [Image.open(path) for path in image_paths] | |
| # Trả về ảnh và đường dẫn của ảnh | |
| return images, image_paths | |
| # Tạo giao diện Gradio | |
| with gr.Blocks(title="AIC - Image Folder Uploader", theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# 🖼️ AIC - Image Folder Uploader") | |
| gr.Markdown("Ứng dụng để upload thư mục ảnh (dưới dạng file zip) và hiển thị ảnh") | |
| with gr.Row(): | |
| input_zip = gr.File( | |
| label="📁 Upload thư mục ảnh (dưới dạng tệp zip)", | |
| type="filepath" # Sử dụng 'filepath' thay vì 'file' | |
| ) | |
| output_images = gr.Gallery( | |
| label="🎨 Ảnh đã upload", | |
| height=300 | |
| ) | |
| output_paths = gr.Textbox(label="Đường dẫn ảnh đã upload", interactive=False) | |
| # Kết nối sự kiện cho việc upload và giải nén thư mục | |
| input_zip.change( | |
| fn=extract_images, | |
| inputs=input_zip, | |
| outputs=[output_images, output_paths] | |
| ) | |
| # Khởi chạy ứng dụng | |
| if __name__ == "__main__": | |
| demo.launch() | |