Arrcttacsrks commited on
Commit
fc9c002
·
verified ·
1 Parent(s): 09db3d2

Upload app(24).py

Browse files
Files changed (1) hide show
  1. app(24).py +79 -0
app(24).py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import torch
4
+ from model import U2NET
5
+ from torch.autograd import Variable
6
+ import numpy as np
7
+ from huggingface_hub import hf_hub_download
8
+ import gradio as gr
9
+
10
+ # Chuẩn hóa dự đoán
11
+ def normPRED(d):
12
+ return (d - torch.min(d)) / (torch.max(d) - torch.min(d))
13
+
14
+ # Hàm suy luận với U2NET
15
+ def inference(net, input_img):
16
+ input_img = input_img / np.max(input_img)
17
+ tmpImg = np.zeros((input_img.shape[0], input_img.shape[1], 3))
18
+ tmpImg[:, :, 0] = (input_img[:, :, 2] - 0.406) / 0.225
19
+ tmpImg[:, :, 1] = (input_img[:, :, 1] - 0.456) / 0.224
20
+ tmpImg[:, :, 2] = (input_img[:, :, 0] - 0.485) / 0.229
21
+ tmpImg = torch.from_numpy(tmpImg.transpose((2, 0, 1))[np.newaxis, :, :, :]).type(torch.FloatTensor)
22
+ tmpImg = Variable(tmpImg.cuda() if torch.cuda.is_available() else tmpImg)
23
+ d1, _, _, _, _, _, _ = net(tmpImg)
24
+ pred = normPRED(1.0 - d1[:, 0, :, :])
25
+ return pred.cpu().data.numpy().squeeze()
26
+
27
+ # Hàm chính để xử lý ảnh đầu vào và trả về ảnh chân dung
28
+ def process_image(img, apply_bw, brightness, contrast, saturation, white_balance, hue, highlights_shadows, sharpness, noise_reduction,
29
+ apply_adjustments):
30
+ # Chuyển ảnh sang đen trắng nếu cần
31
+ if apply_bw:
32
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
33
+ img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
34
+
35
+ # Áp dụng các điều chỉnh nếu được yêu cầu
36
+ if apply_adjustments:
37
+ # Độ sáng và Độ tương phản
38
+ img = cv2.convertScaleAbs(img, alpha=contrast / 50.0, beta=brightness - 50)
39
+
40
+ # Các điều chỉnh khác có thể được thêm vào đây (bão hòa, cân bằng trắng, v.v.)
41
+ # Placeholder cho các điều chỉnh chi tiết hơn
42
+
43
+ # Chạy suy luận với U2NET
44
+ result = inference(u2net, img)
45
+ return (result * 255).astype(np.uint8)
46
+
47
+ # Tải mô hình từ Hugging Face Hub
48
+ def load_u2net_model():
49
+ model_path = hf_hub_download(repo_id="Arrcttacsrks/U2net", filename="u2net_portrait.pth", use_auth_token=os.getenv("HF_TOKEN"))
50
+ net = U2NET(3, 1)
51
+ net.load_state_dict(torch.load(model_path, map_location="cuda" if torch.cuda.is_available() else "cpu"))
52
+ net.eval()
53
+ return net
54
+
55
+ # Khởi tạo mô hình U2NET
56
+ u2net = load_u2net_model()
57
+
58
+ # Tạo giao diện với Gradio
59
+ iface = gr.Interface(
60
+ fn=process_image,
61
+ inputs=[
62
+ gr.Image(type="numpy", label="Upload your image"),
63
+ gr.Checkbox(label="Black & White Image"),
64
+ gr.Slider(0, 100, value=50, label="Brightness"),
65
+ gr.Slider(0, 100, value=50, label="Contrast"),
66
+ gr.Slider(0, 100, value=50, label="Saturation"),
67
+ gr.Slider(0, 100, value=50, label="White Balance"),
68
+ gr.Slider(0, 100, value=50, label="Hue"),
69
+ gr.Slider(0, 100, value=50, label="Highlights and Shadows"),
70
+ gr.Slider(0, 100, value=50, label="Sharpness"),
71
+ gr.Slider(0, 100, value=50, label="Noise Reduction"),
72
+ gr.Checkbox(label="Apply Adjustments")
73
+ ],
74
+ outputs=gr.Image(type="numpy", label="Portrait Result"),
75
+ title="Portrait Generation with U2NET",
76
+ description="Upload an image to generate its portrait with optional adjustments."
77
+ )
78
+
79
+ iface.launch()