Spaces:
Sleeping
Sleeping
Update app.py
Browse files29 unmodified lines3031323334353637381 unmodified line40414243444546474849505152532 unmodified lines565758596061626364656611 unmodified lines7879808129 unmodified linesraise ValueError(f"Invalid color format: {color_str}. Use hex like '#ff0000', color name like 'red', or rgba format.")def apply_mask(image: Image.Image, prompt: str, color: str) -> Image.Image:# Convert image to RGBAimage_np = np.array(image.convert("RGBA"))# Process the input image and promptinputs = processor(text=prompt, images=image, return_tensors="pt")outputs = model(**inputs)1 unmodified line# Get the binary mask from predictionsmask = preds.sigmoid().detach().cpu().numpy()mask = cv2.resize(mask[0], (image_np.shape[1], image_np.shape[0]))mask = cv2.GaussianBlur(mask, (15, 15), 0)mask_bin = (mask > 0.4).astype(np.uint8)# Resize mask to match image size# mask_resized = cv2.resize(mask, (image_np.shape[1], image_np.shape[0]))mask_3d = np.stack([mask_bin] * 4, axis=-1) # Extend mask to 3D# Convert the color string to an RGB tuplecolor_rgb = parse_color(color)2 unmodified lines# Create an overlay with the selected coloroverlay = np.zeros_like(image_np, dtype=np.uint8)overlay[:] = overlay_colormasked_image = image_np.copy()# masked_image[mask_bin == 1] = (# 0.5 * masked_image[mask_bin == 1] + 0.5 * overlay[mask_bin == 1]).astype(np.units)# Apply the mask to the imagemasked_image = np.where(mask_3d == 1, overlay, image_np)return Image.fromarray(masked_image)11 unmodified linesdescription="Upload an image, input a prompt (e.g., 'person', 'sky'), and pick a mask color.")iface.launch()29 unmodified lines3031323334351 unmodified line37383940414243444546474849502 unmodified lines5354555657585911 unmodified lines7172737429 unmodified linesraise ValueError(f"Invalid color format: {color_str}. Use hex like '#ff0000', color name like 'red', or rgba format.")def apply_mask(image: Image.Image, prompt: str, color: str) -> Image.Image:# Process the input image and promptinputs = processor(text=prompt, images=image, return_tensors="pt")outputs = model(**inputs)1 unmodified line# Get the binary mask from predictionsmask = preds.sigmoid().detach().cpu().numpy()mask = (mask > 0.5).astype(np.uint8)# Convert image to RGBAimage_np = np.array(image.convert("RGBA"))# Resize mask to match image sizemask_resized = cv2.resize(mask, (image_np.shape[1], image_np.shape[0]))mask_3d = np.stack([mask_resized] * 4, axis=-1) # Extend mask to 3D# Convert the color string to an RGB tuplecolor_rgb = parse_color(color)2 unmodified lines# Create an overlay with the selected coloroverlay = np.zeros_like(image_np, dtype=np.uint8)overlay[:] = overlay_color# Apply the mask to the imagemasked_image = np.where(mask_3d == 1, overlay, image_np)return Image.fromarray(masked_image)11 unmodified linesdescription="Upload an image, input a prompt (e.g., 'person', 'sky'), and pick a mask color.")iface.launch()
app.py+7-14