undresser / app.py
alfabill's picture
Update app.py
2a125e6 verified
from run import process
import time
import cv2
from PIL import Image
import gradio as gr
TESTdevice = "cpu"
index = 1
def mainTest(inputpath, outpath):
watermark = deep_nude_process(inputpath)
watermark1 = cv2.cvtColor(watermark, cv2.COLOR_BGRA2RGBA)
return watermark1
def deep_nude_process(inputpath):
dress = cv2.imread(inputpath)
h = dress.shape[0]
w = dress.shape[1]
dress = cv2.resize(dress, (512, 512), interpolation=cv2.INTER_CUBIC)
watermark = process(dress)
watermark = cv2.resize(watermark, (w, h), interpolation=cv2.INTER_CUBIC)
return watermark
def inference(img):
global index
if img is None:
return None
bgra = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
inputpath = f"input_{index}.jpg"
cv2.imwrite(inputpath, bgra)
outputpath = f"out_{index}.jpg"
index += 1
print(time.strftime("START!!!!!!!!! %Y-%m-%d %H:%M:%S", time.localtime()))
output = mainTest(inputpath, outputpath)
print(time.strftime("Finish!!!!!!!!! %Y-%m-%d %H:%M:%S", time.localtime()))
return output
def load_image_from_file(file_path, new_height=None):
"""
Load an image from a file and optionally resize it while maintaining the aspect ratio.
"""
try:
img = Image.open(file_path)
if new_height is not None:
aspect_ratio = img.width / img.height
new_width = int(new_height * aspect_ratio)
img = img.resize((new_width, new_height), Image.LANCZOS)
return img
except FileNotFoundError:
print(f"File not found: {file_path}")
return None
except Exception as e:
print(f"Error loading image from file: {e}")
return None
title = "Undress AI"
description = "β›” Input photos of people, similar to the test picture at the bottom, and undress pictures will be produced. You may have to wait 30 seconds for a picture. πŸ”ž Do not upload personal photos πŸ”ž"
# Prepare examples
examples = [
["example9.webp"],
["example2.png"],
["example1.png"],
["example5.webp"],
["example6.webp"],
["example8.webp"],
]
# Custom CSS
css = """
#example_img {
max-height: 400px;
}
"""
# Create Gradio interface
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
gr.Markdown(f"# {title}")
gr.Markdown(description)
with gr.Row():
with gr.Column():
image_input = gr.Image(type="numpy", label="Input Image", height=340)
process_button = gr.Button("Run", variant="primary")
gr.Examples(
examples=examples,
inputs=image_input,
label="Example Images",
elem_id="example_img"
)
with gr.Column():
image_output = gr.Image(type="numpy", label="Output Image", height=340)
# Connect the button to the inference function
process_button.click(
fn=inference,
inputs=image_input,
outputs=image_output
)
# Launch the app
if __name__ == "__main__":
demo.queue(max_size=10)
demo.launch()