File size: 2,285 Bytes
167bdc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import cv2
import torch
from model import U2NET
from torch.autograd import Variable
import numpy as np
from huggingface_hub import hf_hub_download
import gradio as gr

# Chuẩn hóa dự đoán
def normPRED(d):
    return (d - torch.min(d)) / (torch.max(d) - torch.min(d))

# Hàm suy luận với U2NET
def inference(net, input_img):
    input_img = input_img / np.max(input_img)
    tmpImg = np.zeros((input_img.shape[0], input_img.shape[1], 3))
    tmpImg[:, :, 0] = (input_img[:, :, 2] - 0.406) / 0.225
    tmpImg[:, :, 1] = (input_img[:, :, 1] - 0.456) / 0.224
    tmpImg[:, :, 2] = (input_img[:, :, 0] - 0.485) / 0.229
    tmpImg = torch.from_numpy(tmpImg.transpose((2, 0, 1))[np.newaxis, :, :, :]).type(torch.FloatTensor)
    tmpImg = Variable(tmpImg.cuda() if torch.cuda.is_available() else tmpImg)
    d1, _, _, _, _, _, _ = net(tmpImg)
    pred = normPRED(1.0 - d1[:, 0, :, :])
    return pred.cpu().data.numpy().squeeze()

# Hàm chính để xử lý ảnh đầu vào và trả về ảnh chân dung
def process_image(img, bw_option):
    # Chuyển đổi ảnh thành đen trắng nếu được chọn
    if bw_option:
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)  # Chuyển lại thành ảnh 3 kênh cho mô hình
    # Chạy suy luận để tạo ảnh chân dung
    result = inference(u2net, img)
    return (result * 255).astype(np.uint8)

# Tải mô hình từ Hugging Face Hub
def load_u2net_model():
    model_path = hf_hub_download(repo_id="Arrcttacsrks/U2net", filename="u2net_portrait.pth", use_auth_token=os.getenv("HF_TOKEN"))
    net = U2NET(3, 1)
    net.load_state_dict(torch.load(model_path, map_location="cuda" if torch.cuda.is_available() else "cpu"))
    net.eval()
    return net

# Khởi tạo mô hình U2NET
u2net = load_u2net_model()

# Tạo giao diện với Gradio
iface = gr.Interface(
    fn=process_image,
    inputs=[
        gr.Image(type="numpy", label="Upload your image"),
        gr.Checkbox(label="Convert to Black & White?", value=False)  # Thêm tùy chọn tick
    ],
    outputs=gr.Image(type="numpy", label="Portrait Result"),
    title="Portrait Generation with U2NET",
    description="Upload an image to generate its portrait."
)

iface.launch()