File size: 2,991 Bytes
8732886
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import streamlit as st
from run import process
import time
import cv2
from PIL import Image
import numpy as np

# Global variable
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

def load_image_from_file(file_path, new_height=None):
    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 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

# Streamlit app
st.title("Undress AI")
st.markdown("β›” 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 β›”")

# Example images
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'),
]

# Display example images
st.subheader("Examples")
for example in examples:
    st.image(example, use_column_width=True)

# Image upload
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "webp"])

if uploaded_file is not None:
    file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
    img = cv2.imdecode(file_bytes, 1)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    if st.button("Run"):
        processed_img = inference(img)
        st.image(processed_img, caption="Processed Image", use_column_width=True)