Spaces:
Running
Running
from run import process | |
import time | |
import subprocess | |
import os | |
import argparse | |
import cv2 | |
import sys | |
from PIL import Image | |
import torch | |
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 | |
bgra = cv2.cvtColor(img, cv2.COLOR_RGBA2BGRA) | |
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 | |
from PIL import Image | |
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. | |
Args: | |
file_path (str): The path to the image file. | |
new_height (int, optional): The new height for the image. If None, the image is not resized. | |
Returns: | |
Image: The loaded (and optionally resized) image. | |
""" | |
try: | |
img = Image.open(file_path) | |
if new_height is not None: | |
# Calculate new width to maintain aspect ratio | |
aspect_ratio = img.width / img.height | |
new_width = int(new_height * aspect_ratio) | |
# Resize the image | |
img = img.resize((new_width, new_height), Image.LANCZOS) | |
return img | |
except FileNotFoundError: | |
print(f"File not found: {file_path}") | |
return None | |
except Image.UnidentifiedImageError: | |
print(f"Cannot identify image file: {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 π There is a queue system. According to the logic of first come, first served, only one picture will be made at a time. Must be able to at least see the outline of a human body β" | |
examples = [ | |
[load_image_from_file('example9.webp')], | |
[load_image_from_file('example2.png')], | |
[load_image_from_file('example1.png')], | |
[load_image_from_file('example5.webp')], | |
[load_image_from_file('example6.webp')], | |
[load_image_from_file('example8.webp')], | |
] | |
css = """ | |
body { | |
background-color: rgb(3, 7, 18); | |
color: white; | |
} | |
.gradio-container { | |
background-color: rgb(3, 7, 18) !important; | |
border: none !important; | |
} | |
#example_img .hide-container{ | |
height:100%; | |
width:240px; | |
transition: transform 0.5s ease; | |
} | |
#example_img { | |
width:240px; | |
height:100%; | |
} | |
#example_img img{ | |
height:50px; | |
width:50px; | |
transition: transform 0.5s ease; | |
} | |
#example_img .container{ | |
height:50px; | |
width:50px; | |
transition: transform 0.5s ease; | |
} | |
footer {display: none !important;} | |
""" | |
js=''' | |
<script> | |
window.postMessageToParent = function(img, event, source, value) { | |
// Construct the message object with the provided parameters | |
console.log("post start",event, source, value); | |
const message = { | |
event: event, | |
source: source, | |
value: value | |
}; | |
// Post the message to the parent window | |
window.parent.postMessage(message, '*'); | |
console.log("post finish"); | |
return img; | |
} | |
</script> | |
''' | |
with gr.Blocks(css=css, head=js) as demo: | |
width=240 | |
height=340 | |
with gr.Row(equal_height=False): | |
with gr.Column(min_width=240): # Adjust scale for proper sizing | |
image_input = gr.Image(type="numpy", label="Upload Image", height=height) | |
gr.Examples(examples=examples, inputs=image_input, examples_per_page=10, elem_id="example_img") | |
process_button = gr.Button("Go!")#,size="sm" | |
def update_status(img): | |
processed_img = inference(img) | |
return processed_img | |
process_button.click(update_status, inputs=image_input, outputs=image_input, js='''(i) => window.postMessageToParent(i, "process_started", "demo_hf_deepnude_gan_card", "click_nude")''') | |
demo.queue(max_size=10) | |
demo.launch() | |