RICHERGIRL commited on
Commit
0a96c6e
ยท
verified ยท
1 Parent(s): 21bdca6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -29
app.py CHANGED
@@ -10,28 +10,19 @@ fa = face_alignment.FaceAlignment('2D', device='cpu', flip_input=False)
10
 
11
  def process_mask(mask_img):
12
  """Automatically remove white/transparent background from mask image"""
13
- # Convert to RGBA if not already
14
  mask_img = mask_img.convert("RGBA")
15
-
16
- # Get image data
17
  datas = mask_img.getdata()
18
  new_data = []
19
-
20
- # Define white threshold (can adjust if needed)
21
  white_threshold = 240
22
 
23
  for item in datas:
24
- # If pixel is mostly white (all channels > threshold) or already transparent
25
  if (item[0] > white_threshold and
26
  item[1] > white_threshold and
27
  item[2] > white_threshold) or item[3] == 0:
28
- # Make fully transparent
29
  new_data.append((255, 255, 255, 0))
30
  else:
31
- # Keep the pixel as is
32
  new_data.append(item)
33
 
34
- # Update image data
35
  mask_img.putdata(new_data)
36
  return mask_img
37
 
@@ -45,7 +36,7 @@ def fit_mask_to_face(image):
45
  landmarks = fa.get_landmarks(img_np)
46
 
47
  if not landmarks:
48
- return image # Return original image if no face detected
49
 
50
  image_rgb = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
51
 
@@ -73,39 +64,26 @@ def fit_mask_to_face(image):
73
  return background_pil.convert("RGB")
74
  return image
75
 
76
- # Rest of the code remains the same...
77
-
78
  def capture_and_process():
79
  """Capture image from webcam and process it"""
80
- # Create a temporary file
81
  temp_file = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
82
  temp_path = temp_file.name
83
 
84
- # Open camera
85
  cap = cv2.VideoCapture(0)
86
-
87
- # Capture frame
88
  ret, frame = cap.read()
89
 
90
  if ret:
91
- # Save captured frame
92
  cv2.imwrite(temp_path, frame)
93
-
94
- # Convert to PIL Image
95
  pil_image = Image.open(temp_path)
96
-
97
- # Process the image
98
  result = fit_mask_to_face(pil_image)
99
-
100
- # Release camera
101
  cap.release()
102
-
103
  return result
104
  else:
105
  cap.release()
106
  return None
107
 
108
- with gr.Blocks(css=css) as demo:
 
109
  gr.Markdown("# ๐ŸŽญ Party Face Mask App")
110
  gr.Markdown("Upload an image or capture from your webcam to try on our party mask!")
111
 
@@ -113,19 +91,18 @@ with gr.Blocks(css=css) as demo:
113
  with gr.TabItem("๐Ÿ“ Upload Image"):
114
  with gr.Row():
115
  with gr.Column():
116
- upload_input = gr.Image(label="Upload your photo", type="pil", elem_classes="image-container")
117
  upload_button = gr.Button("Apply Mask")
118
  with gr.Column():
119
- upload_output = gr.Image(label="Result", type="pil", elem_classes="image-container")
120
  upload_button.click(fn=fit_mask_to_face, inputs=upload_input, outputs=upload_output)
121
 
122
  with gr.TabItem("๐Ÿ“ธ Webcam"):
123
  with gr.Row():
124
  with gr.Column():
125
  webcam_button = gr.Button("Capture Photo from Webcam")
126
- webcam_status = gr.Textbox(label="Status", visible=False)
127
  with gr.Column():
128
- webcam_output = gr.Image(label="Result", type="pil", elem_classes="image-container")
129
  webcam_button.click(fn=capture_and_process, outputs=webcam_output)
130
 
131
  demo.launch()
 
10
 
11
  def process_mask(mask_img):
12
  """Automatically remove white/transparent background from mask image"""
 
13
  mask_img = mask_img.convert("RGBA")
 
 
14
  datas = mask_img.getdata()
15
  new_data = []
 
 
16
  white_threshold = 240
17
 
18
  for item in datas:
 
19
  if (item[0] > white_threshold and
20
  item[1] > white_threshold and
21
  item[2] > white_threshold) or item[3] == 0:
 
22
  new_data.append((255, 255, 255, 0))
23
  else:
 
24
  new_data.append(item)
25
 
 
26
  mask_img.putdata(new_data)
27
  return mask_img
28
 
 
36
  landmarks = fa.get_landmarks(img_np)
37
 
38
  if not landmarks:
39
+ return image
40
 
41
  image_rgb = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
42
 
 
64
  return background_pil.convert("RGB")
65
  return image
66
 
 
 
67
  def capture_and_process():
68
  """Capture image from webcam and process it"""
 
69
  temp_file = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
70
  temp_path = temp_file.name
71
 
 
72
  cap = cv2.VideoCapture(0)
 
 
73
  ret, frame = cap.read()
74
 
75
  if ret:
 
76
  cv2.imwrite(temp_path, frame)
 
 
77
  pil_image = Image.open(temp_path)
 
 
78
  result = fit_mask_to_face(pil_image)
 
 
79
  cap.release()
 
80
  return result
81
  else:
82
  cap.release()
83
  return None
84
 
85
+ # Create interface without CSS
86
+ with gr.Blocks() as demo:
87
  gr.Markdown("# ๐ŸŽญ Party Face Mask App")
88
  gr.Markdown("Upload an image or capture from your webcam to try on our party mask!")
89
 
 
91
  with gr.TabItem("๐Ÿ“ Upload Image"):
92
  with gr.Row():
93
  with gr.Column():
94
+ upload_input = gr.Image(label="Upload your photo", type="pil")
95
  upload_button = gr.Button("Apply Mask")
96
  with gr.Column():
97
+ upload_output = gr.Image(label="Result", type="pil")
98
  upload_button.click(fn=fit_mask_to_face, inputs=upload_input, outputs=upload_output)
99
 
100
  with gr.TabItem("๐Ÿ“ธ Webcam"):
101
  with gr.Row():
102
  with gr.Column():
103
  webcam_button = gr.Button("Capture Photo from Webcam")
 
104
  with gr.Column():
105
+ webcam_output = gr.Image(label="Result", type="pil")
106
  webcam_button.click(fn=capture_and_process, outputs=webcam_output)
107
 
108
  demo.launch()