Arrcttacsrks commited on
Commit
5eb13b0
·
verified ·
1 Parent(s): b016dab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -8
app.py CHANGED
@@ -6,6 +6,7 @@ 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):
@@ -24,7 +25,22 @@ def inference(net, input_img):
24
  pred = normPRED(1.0 - d1[:, 0, :, :])
25
  return pred.cpu().data.numpy().squeeze()
26
 
27
- # Hàm chính để xử ảnh đầu vào và trả về ảnh chân dung
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  def process_image(img, bw_option):
29
  # Chuyển đổi ảnh thành đen trắng nếu được chọn
30
  if bw_option:
@@ -32,7 +48,12 @@ def process_image(img, bw_option):
32
  img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) # Chuyển lại thành ảnh 3 kênh cho mô hình
33
  # Chạy suy luận để tạo ảnh chân dung
34
  result = inference(u2net, img)
35
- return (result * 255).astype(np.uint8)
 
 
 
 
 
36
 
37
  # Tải mô hình từ Hugging Face Hub
38
  def load_u2net_model():
@@ -49,12 +70,15 @@ u2net = load_u2net_model()
49
  iface = gr.Interface(
50
  fn=process_image,
51
  inputs=[
52
- gr.Image(type="numpy", label="Upload your image"),
53
- gr.Checkbox(label="Convert to Black & White?", value=False) # Thêm tùy chọn tick
 
 
 
 
54
  ],
55
- outputs=gr.Image(type="numpy", label="Portrait Result"),
56
- title="Portrait Generation with U2NET",
57
- description="Upload an image to generate its portrait."
58
  )
59
 
60
- iface.launch()
 
6
  import numpy as np
7
  from huggingface_hub import hf_hub_download
8
  import gradio as gr
9
+ import ezdxf # Thêm thư viện ezdxf để tạo file DXF
10
 
11
  # Chuẩn hóa dự đoán
12
  def normPRED(d):
 
25
  pred = normPRED(1.0 - d1[:, 0, :, :])
26
  return pred.cpu().data.numpy().squeeze()
27
 
28
+ # Hàm tạo file DXF từ ảnh kết quả
29
+ def convert_to_dxf(image, filename="output.dxf"):
30
+ # Tìm các đường nét trong ảnh
31
+ contours, _ = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
32
+
33
+ # Tạo file DXF và thêm các đường nét
34
+ doc = ezdxf.new(dxfversion="R2010")
35
+ msp = doc.modelspace()
36
+ for contour in contours:
37
+ points = contour.reshape(-1, 2)
38
+ msp.add_lwpolyline(points, close=True)
39
+
40
+ doc.saveas(filename)
41
+ return filename
42
+
43
+ # Hàm chính để xử lý ảnh đầu vào, trả về ảnh chân dung và lưu DXF
44
  def process_image(img, bw_option):
45
  # Chuyển đổi ảnh thành đen trắng nếu được chọn
46
  if bw_option:
 
48
  img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) # Chuyển lại thành ảnh 3 kênh cho mô hình
49
  # Chạy suy luận để tạo ảnh chân dung
50
  result = inference(u2net, img)
51
+ result_img = (result * 255).astype(np.uint8)
52
+
53
+ # Tạo file DXF từ kết quả
54
+ dxf_path = convert_to_dxf(result_img)
55
+
56
+ return result_img, dxf_path
57
 
58
  # Tải mô hình từ Hugging Face Hub
59
  def load_u2net_model():
 
70
  iface = gr.Interface(
71
  fn=process_image,
72
  inputs=[
73
+ gr.Image(type="numpy", label="Tải lên ảnh của bạn"),
74
+ gr.Checkbox(label="Chuyển sang trắng đen?", value=False)
75
+ ],
76
+ outputs=[
77
+ gr.Image(type="numpy", label="Kết quả chân dung"),
78
+ gr.File(label="Tải xuống file DXF") # Thêm output cho file DXF
79
  ],
80
+ title="Tạo ảnh chân dung và file DXF từ ảnh",
81
+ description="Tải lên một ảnh để tạo ảnh chân dung và file DXF từ ảnh đó."
 
82
  )
83
 
84
+ iface.launch()