ProPainter / app.py
fffiloni's picture
Update app.py
261124f
raw
history blame
No virus
4 kB
import os
import subprocess
import gradio as gr
# execute a CLI command
def execute_command(command: str) -> None:
subprocess.run(command, check=True)
def infer(video_frames, masks_frames, project_name):
# Create the directory if it doesn't exist
my_video_directory = f"{project_name}"
if not os.path.exists(my_video_directory):
os.makedirs(my_video_directory)
# Assuming 'images' is a list of image file paths
for idx, image in enumerate(video_frames):
# Get the base file name (without path) from the original location
image_name = os.path.basename(image.name)
# Construct the destination path in the working directory
destination_path = os.path.join(my_video_directory, image_name)
# Copy the image from the original location to the working directory
shutil.copy(image.name, destination_path)
# Print the image name and its corresponding save path
print(f"Image {idx + 1}: {image_name} copied to {destination_path}")
# Create the directory if it doesn't exist
my_masks_directory = f"{project_name}_masks"
if not os.path.exists(my_masks_directory):
os.makedirs(my_masks_directory)
# Assuming 'images' is a list of image file paths
for idx, image in enumerate(video_frames):
# Get the base file name (without path) from the original location
image_name = os.path.basename(image.name)
# Construct the destination path in the working directory
destination_path = os.path.join(my_masks_directory, image_name)
# Copy the image from the original location to the working directory
shutil.copy(image.name, destination_path)
# Print the image name and its corresponding save path
print(f"Image {idx + 1}: {image_name} copied to {destination_path}")
video_frames_folder = "inputs/object_removal/bmx-trees"
masks_folder = "inputs/object_removal/bmx-trees_mask"
# Create the "results" folder if it doesn't exist
output_folder = "results"
if not os.path.exists(output_folder):
os.makedirs(output_folder)
#bmx_trees_folder = os.path.join(output_folder, "bmx-trees")
command = [
f"python",
f"inference_propainter.py",
f"--video={video_frames_folder}",
f"--mask={masks_folder}",
f"--output={output_folder}"
]
execute_command(command)
# Get the list of files in the "results" folder
result_files = os.listdir(output_folder)
# Print the content of the "results" folder
print(f"Contents of the {output_folder} folder:")
for item in result_files:
print(item)
# List the content of the "bmx-trees" folder within "results"
bmx_trees_folder = os.path.join(output_folder, "bmx-trees")
bmx_trees_content = [os.path.join(bmx_trees_folder, item) for item in os.listdir(bmx_trees_folder)]
print(f"Contents of the {bmx_trees_folder} folder:")
for item in bmx_trees_content:
print(item)
return "done", bmx_trees_content[0], bmx_trees_content[1]
css="""
#col-container{
margin: 0 auto;
max-width: 840px;
text-align: left;
}
"""
with gr.Blocks(css=css) as demo:
with gr.Column(elem_id="col-container"):
gr.HTML("""
<h2 style="text-align: center;">ProPainter</h2>
<p style="text-align: center;">
</p>
""")
with gr.Row():
with gr.Column():
video_frames = gr.Files(label="Video frames")
masks_frames = gr.Files(label="Masks frames")
submit_btn = gr.Button("Submit")
with gr.Column():
result = gr.Textbox(label="Result")
res_masked = gr.Video()
res_files = gr.Video()
submit_btn.click(fn=infer, inputs=[video_frames, masks_frames], outputs=[result, res_masked, res_files])
demo.queue(max_size=12).launch()