Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
import tempfile
|
| 5 |
+
import shutil
|
| 6 |
+
import hashlib
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
TMP_DIR = os.path.join(tempfile.gettempdir(), 'gradio')
|
| 10 |
+
os.makedirs(TMP_DIR, exist_ok=True)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def move_uploaded_files(files, temp_dir):
|
| 14 |
+
saved_files = []
|
| 15 |
+
for file in files:
|
| 16 |
+
target_file_path = os.path.join(temp_dir, os.path.basename(file.name))
|
| 17 |
+
saved_files.append(target_file_path)
|
| 18 |
+
shutil.move(file.name, target_file_path)
|
| 19 |
+
return saved_files
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def process_images(files):
|
| 23 |
+
files_str = ','.join([f.name for f in files])
|
| 24 |
+
hashdir = hashlib.md5(files_str.encode('utf8')).hexdigest()
|
| 25 |
+
hashdir_path = os.path.join(TMP_DIR, hashdir)
|
| 26 |
+
os.makedirs(hashdir_path, exist_ok=True)
|
| 27 |
+
saved_files = move_uploaded_files(files, hashdir_path)
|
| 28 |
+
saved_files_str = ' '.join(saved_files)
|
| 29 |
+
return 'Before: {}\nAfter: {}'.format(files_str, saved_files_str), saved_files[0]
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
with gr.Blocks() as demo:
|
| 33 |
+
gr.Markdown("Image and Video Processor")
|
| 34 |
+
# Gradio interface
|
| 35 |
+
inputs = gr.File(file_count="multiple", file_types=["image"], label="Upload Images")
|
| 36 |
+
btn = gr.Button(value="Run")
|
| 37 |
+
# outputs = [gr.Image(type="numpy", label="Processed Images", interactive=False), gr.Video(label="Output Video")]
|
| 38 |
+
outputs = [gr.Textbox(label="Log"), gr.Image(type="filepath", label="Processed Images", interactive=False)]
|
| 39 |
+
btn.click(fn=process_images, inputs=inputs, outputs=outputs)
|
| 40 |
+
|
| 41 |
+
demo.launch()
|