Arrcttacsrks commited on
Commit
281824c
·
verified ·
1 Parent(s): df65035

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -36
app.py CHANGED
@@ -7,35 +7,6 @@ import numpy as np
7
  from huggingface_hub import hf_hub_download
8
  import gradio as gr
9
 
10
- # Hàm phát hiện một khuôn mặt duy nhất
11
- def detect_single_face(face_cascade, img):
12
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
13
- faces = face_cascade.detectMultiScale(gray, 1.1, 4)
14
- if len(faces) == 0:
15
- print("Warning: No face detected, running on the whole image!")
16
- return None
17
- wh, idx = 0, 0
18
- for i, (x, y, w, h) in enumerate(faces):
19
- if w * h > wh:
20
- idx, wh = i, w * h
21
- return faces[idx]
22
-
23
- # Hàm cắt và chuẩn hóa khuôn mặt
24
- def crop_face(img, face):
25
- if face is None:
26
- return img
27
- (x, y, w, h) = face
28
- height, width = img.shape[:2]
29
- lpad, rpad, tpad, bpad = int(w * 0.4), int(w * 0.4), int(h * 0.6), int(h * 0.2)
30
- left, right = max(0, x - lpad), min(width, x + w + rpad)
31
- top, bottom = max(0, y - tpad), min(height, y + h + bpad)
32
- im_face = img[top:bottom, left:right]
33
- if len(im_face.shape) == 2:
34
- im_face = np.repeat(im_face[:, :, np.newaxis], 3, axis=2)
35
- im_face = np.pad(im_face, ((tpad, bpad), (lpad, rpad), (0, 0)), mode='constant', constant_values=255)
36
- im_face = cv2.resize(im_face, (512, 512), interpolation=cv2.INTER_AREA)
37
- return im_face
38
-
39
  # Chuẩn hóa dự đoán
40
  def normPRED(d):
41
  return (d - torch.min(d)) / (torch.max(d) - torch.min(d))
@@ -66,14 +37,11 @@ def process_image(img, apply_bw, brightness, contrast, saturation, white_balance
66
  # Độ sáng và Độ tương phản
67
  img = cv2.convertScaleAbs(img, alpha=contrast / 50.0, beta=brightness - 50)
68
 
69
- # Độ bão hòa, Cân bằng trắng, và các điều chỉnh khác (phải tự viết hàm hoặc sử dụng thư viện chuyên biệt)
70
- # Placeholder cho các điều chỉnh cần thiết ở đây
71
 
72
- # Phát hiện cắt khuôn mặt
73
- face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
74
- face = detect_single_face(face_cascade, img)
75
- cropped_face = crop_face(img, face)
76
- result = inference(u2net, cropped_face)
77
  return (result * 255).astype(np.uint8)
78
 
79
  # Tải mô hình từ Hugging Face Hub
 
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))
 
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 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