Aklavya commited on
Commit
bfc8332
·
verified ·
1 Parent(s): e00265c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -21
app.py CHANGED
@@ -39,19 +39,7 @@ def check_for_adult_content(image):
39
 
40
  return False # No explicit content detected
41
 
42
- # Validate ROI to prevent out-of-bound errors
43
- def validate_roi(roi, image_shape):
44
- x, y, w, h = roi
45
- img_h, img_w = image_shape[:2]
46
-
47
- x = max(0, x)
48
- y = max(0, y)
49
- w = min(w, img_w - x)
50
- h = min(h, img_h - y)
51
-
52
- return x, y, w, h
53
-
54
- # Face swapping function with sharpening
55
  def swap_faces(destination_image, source_image):
56
  # Load the destination and source images from Streamlit inputs
57
  img = cv2.cvtColor(np.array(destination_image), cv2.COLOR_RGB2BGR)
@@ -70,13 +58,7 @@ def swap_faces(destination_image, source_image):
70
  res = img.copy()
71
  try:
72
  for face in faces:
73
- bbox = face['bbox'].astype(int)
74
- x, y, w, h = validate_roi(bbox, img.shape) # Validate ROI
75
-
76
- # Resize source face to match destination face size
77
- test_face_resized = cv2.resize(test, (w, h))
78
-
79
- res = swapper.get(res, face, test_face_resized, paste_back=True)
80
  except MemoryError:
81
  return "Memory error: Face swapping operation failed due to memory overload."
82
 
@@ -86,6 +68,9 @@ def swap_faces(destination_image, source_image):
86
  [0, -1, 0]])
87
  sharpened_res = cv2.filter2D(res, -1, kernel)
88
 
 
 
 
89
  # Convert the result to RGB for display in Streamlit
90
  sharpened_res_rgb = cv2.cvtColor(sharpened_res, cv2.COLOR_BGR2RGB)
91
 
@@ -161,4 +146,4 @@ def main():
161
 
162
  # Run the app
163
  if __name__ == '__main__':
164
- main()
 
39
 
40
  return False # No explicit content detected
41
 
42
+ # Face swapping function with added sharpening
 
 
 
 
 
 
 
 
 
 
 
 
43
  def swap_faces(destination_image, source_image):
44
  # Load the destination and source images from Streamlit inputs
45
  img = cv2.cvtColor(np.array(destination_image), cv2.COLOR_RGB2BGR)
 
58
  res = img.copy()
59
  try:
60
  for face in faces:
61
+ res = swapper.get(res, face, test_face, paste_back=True)
 
 
 
 
 
 
62
  except MemoryError:
63
  return "Memory error: Face swapping operation failed due to memory overload."
64
 
 
68
  [0, -1, 0]])
69
  sharpened_res = cv2.filter2D(res, -1, kernel)
70
 
71
+ # Commented smoothing (blurring) for testing purposes
72
+ smoothed_res = cv2.GaussianBlur(res, (5, 5), 0)
73
+
74
  # Convert the result to RGB for display in Streamlit
75
  sharpened_res_rgb = cv2.cvtColor(sharpened_res, cv2.COLOR_BGR2RGB)
76
 
 
146
 
147
  # Run the app
148
  if __name__ == '__main__':
149
+ main()