Spaces:
Build error
Build error
import glob | |
import os | |
import io | |
import ffmpeg | |
import requests | |
from PIL import Image | |
import gradio as gr | |
import shutil | |
import concurrent.futures | |
def process_image(mask_data, image_path): | |
image = Image.open(image_path) | |
image_data = io.BytesIO() | |
image.save(image_data, format=image.format) | |
image_data = image_data.getvalue() | |
# Prepare form data | |
form_data = { | |
'ldmSteps': 25, | |
'ldmSampler': 'plms', | |
'zitsWireframe': True, | |
'hdStrategy': 'Original', | |
'hdStrategyCropMargin': 196, | |
'hdStrategyCropTrigerSize': 1280, | |
'hdStrategyResizeLimit': 2048, | |
'prompt': '', | |
'negativePrompt': '', | |
'croperX': -24, | |
'croperY': -23, | |
'croperHeight': 512, | |
'croperWidth': 512, | |
'useCroper': False, | |
'sdMaskBlur': 5, | |
'sdStrength': 0.75, | |
'sdSteps': 50, | |
'sdGuidanceScale': 7.5, | |
'sdSampler': 'pndm', | |
'sdSeed': 42, | |
'sdMatchHistograms': False, | |
'sdScale': 1, | |
'cv2Radius': 5, | |
'cv2Flag': 'INPAINT_NS', | |
'paintByExampleSteps': 50, | |
'paintByExampleGuidanceScale': 7.5, | |
'paintByExampleSeed': 42, | |
'paintByExampleMaskBlur': 5, | |
'paintByExampleMatchHistograms': False, | |
'sizeLimit': 1024, | |
} | |
files_data = { | |
'image': (os.path.basename(image_path), image_data), | |
'mask': ('mask.png', mask_data) | |
} | |
response = requests.post('https://ahmedghani-lama-cleaner-lama.hf.space/inpaint', data=form_data, files=files_data) | |
if response.headers['Content-Type'] == 'image/jpeg' or response.headers['Content-Type'] == 'image/png': | |
output_image_path = os.path.join('output_images', os.path.splitext(os.path.basename(image_path))[0] + '_inpainted' + os.path.splitext(image_path)[1]) | |
with open(output_image_path, 'wb') as output_image_file: | |
output_image_file.write(response.content) | |
else: | |
print(f"Error processing {image_path}: {response.text}") | |
def remove_watermark(sketch, images_path='frames', output_path='output_images'): | |
if os.path.exists('output_images'): | |
shutil.rmtree('output_images') | |
os.makedirs('output_images') | |
mask_data = io.BytesIO() | |
sketch["mask"].save(mask_data, format=sketch["mask"].format) | |
mask_data = mask_data.getvalue() | |
image_paths = glob.glob(f'{images_path}/*.*') | |
with concurrent.futures.ThreadPoolExecutor() as executor: | |
executor.map(lambda image_path: process_image(mask_data, image_path), image_paths) | |
return gr.Video.update(value=convert_frames_to_video('output_images'), visible=True), gr.Button.update(value='Done!') | |
def convert_video_to_frames(video): | |
print(f" input video is : {video}") | |
if os.path.exists('input_video.mp4'): | |
os.remove('input_video.mp4') | |
ffmpeg.input(video).output('input_video.mp4').run() | |
video_path = 'input_video.mp4' | |
if os.path.exists('frames'): | |
shutil.rmtree('frames') | |
os.makedirs('frames') | |
video_name = os.path.splitext(os.path.basename(video_path))[0] | |
ffmpeg.input(video_path).output(f'frames/{video_name}_%d.jpg', qscale=2).run() | |
return gr.Image.update(value=f"{os.getcwd()}/frames/{video_name}_1.jpg", interactive=True), gr.Button.update(interactive=True) | |
def convert_frames_to_video(frames_path): | |
if os.path.exists('output_video.mp4'): | |
os.remove('output_video.mp4') | |
( | |
ffmpeg | |
.input(f'{frames_path}/*.jpg', pattern_type='glob', framerate=25) | |
.output('output_video.mp4') | |
.run() | |
) | |
return gr.Video.update(value='output_video.mp4', visible=True, interactive=True), gr.Button.update(interactive=False) | |
css = """ | |
#remove_btn { | |
background: linear-gradient(#201d18, #2bbbc3); | |
font-weight: bold; | |
font-size: 18px; | |
color:white; | |
} | |
#remove_btn:hover { | |
background: linear-gradient(#2bbbc3, #201d18); | |
} | |
footer { | |
display: none !important; | |
} | |
""" | |
demo = gr.Blocks(css=css, title="Video Watermark Remover") | |
with demo: | |
gr.Markdown(""" | |
# <center>π₯ Video Watermark Remover</center> | |
""") | |
with gr.Row(): | |
with gr.Column(): | |
input_video = gr.Video(label="Upload a Video") | |
with gr.Column(): | |
mask = gr.Image(label="Create a mask for the image", tool="sketch", type="pil", interactive=False) | |
with gr.Row(): | |
with gr.Column(): | |
pass | |
with gr.Column(): | |
remove_btn = gr.Button("Remove Watermark", interactive=False, elem_id="remove_btn") | |
with gr.Column(): | |
pass | |
output_video = gr.Video(label="Output Video", interactive=False) | |
input_video.change(convert_video_to_frames, inputs=[input_video], outputs=[mask, remove_btn]) | |
remove_btn.click(remove_watermark, inputs=[mask], outputs=[output_video, remove_btn]) | |
#position:fixed;bottom:0;left:0;right:0; | |
gr.Markdown("""## <center style="margin:20px;">Developed by Muhammad Ahmed<img src="https://avatars.githubusercontent.com/u/63394104?v=4" style="height:50px;width:50px;border-radius:50%;margin:5px;"></img></center> | |
""") | |
demo.launch(show_api=False) |