File size: 6,008 Bytes
cc8f276 748425c cc8f276 748425c cc8f276 d8cda25 cc8f276 748425c cc8f276 748425c cc8f276 748425c d63f692 cc8f276 d63f692 748425c e608228 d63f692 e608228 748425c d8cda25 e4ccc11 748425c cc8f276 748425c cc8f276 d63f692 e608228 d63f692 e608228 d63f692 e608228 d63f692 748425c cc8f276 748425c cc8f276 d63f692 e4ccc11 d63f692 e4ccc11 d63f692 e4ccc11 d63f692 748425c cc8f276 d63f692 79b337b 748425c cc8f276 748425c d63f692 79b337b d63f692 79b337b d63f692 e4ccc11 79b337b e4ccc11 79b337b e4ccc11 79b337b e4ccc11 79b337b e4ccc11 d63f692 748425c d8cda25 697792b d8cda25 e4ccc11 d8cda25 748425c d8cda25 e4ccc11 d8cda25 e4ccc11 748425c cc8f276 748425c cc8f276 748425c cc8f276 748425c cc8f276 748425c d63f692 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
from pathlib import Path
import gradio as gr
from tools import (
change_color_objects_hsv,
change_color_objects_lab,
privacy_preserve_image,
remove_background,
)
gr.set_static_paths(paths=[Path.cwd().absolute() / "assets"])
icon = """<link rel="icon" type="image/x-icon" href="https://raw.githubusercontent.com/mahan-ym/ImageAlfred/main/src/assets/icons/ImageAlfredIcon.png">"""
title = """Image Alfred - Recolor and Privacy Preserving Image MCP Tools
<img src="https://raw.githubusercontent.com/mahan-ym/ImageAlfred/main/src/assets/icons/ImageAlfredIcon.png" alt="Image Alfred Logo" style="width: 120px; height: auto; margin: 0 auto;">
<h4 style="text-align: center;"></h4>
""" # noqa: E501
hsv_df_input = gr.Dataframe(
headers=["Object", "Red", "Green", "Blue"],
datatype=["str", "number", "number", "number"],
col_count=(4, "fixed"),
show_row_numbers=True,
label="Target Objects and Their new RGB Colors",
type="array",
)
lab_df_input = gr.Dataframe(
headers=["Object", "New A", "New B"],
datatype=["str", "number", "number"],
col_count=(3, "fixed"),
label="Target Objects and New Settings.(0-255 -- 128 = Neutral)",
type="array",
)
change_color_objects_hsv_tool = gr.Interface(
fn=change_color_objects_hsv,
inputs=[
gr.Image(label="Input Image", type="pil"),
hsv_df_input,
],
outputs=gr.Image(label="Output Image"),
title="Image Recolor Tool (HSV)",
description="""
This tool allows you to recolor objects in an image using the HSV color space.
You can specify the RGB values for each object.""", # noqa: E501
examples=[
[
"https://raw.githubusercontent.com/mahan-ym/ImageAlfred/main/src/assets/examples/test_1.jpg",
[
["pants", 255, 178, 102],
],
],
[
"https://raw.githubusercontent.com/mahan-ym/ImageAlfred/main/src/assets/examples/test_8.jpg",
[
["pants", 114, 117, 34],
["shirt", 51, 51, 37],
],
],
],
)
change_color_objects_lab_tool = gr.Interface(
fn=change_color_objects_lab,
inputs=[
gr.Image(label="Input Image", type="pil"),
lab_df_input,
],
outputs=gr.Image(label="Output Image"),
title="Image Recolor Tool (LAB)",
description="""
Recolor an image based on user input using the LAB color space.
You can specify the new A and new B values for each object.
""", # noqa: E501
examples=[
[
"https://raw.githubusercontent.com/mahan-ym/ImageAlfred/main/src/assets/examples/test_1.jpg",
[["pants", 112, 128]],
],
[
"https://raw.githubusercontent.com/mahan-ym/ImageAlfred/main/src/assets/examples/test_4.jpg",
[["desk", 166, 193]],
],
[
"https://raw.githubusercontent.com/mahan-ym/ImageAlfred/main/src/assets/examples/test_5.jpg",
[["suits coat", 110, 133]],
],
],
)
privacy_preserve_tool = gr.Interface(
fn=privacy_preserve_image,
inputs=[
gr.Image(label="Input Image", type="pil"),
gr.Textbox(
label="Objects to Mask (dot-separated)",
placeholder="e.g., person. car. license plate",
),
gr.Slider(
label="Privacy Strength",
minimum=1,
maximum=50,
value=15,
step=1,
info="Higher values result in stronger blurring.",
),
gr.Slider(
label="Detection Threshold",
minimum=0.01,
maximum=0.99,
value=0.2,
step=0.01,
info="Model threshold for detecting objects.",
),
],
outputs=gr.Image(label="Output Image"),
title="Privacy Preserving Tool",
description="Upload an image and provide a prompt for the object to enforce privacy. The tool will use blurring to obscure the specified objects in the image.", # noqa: E501
examples=[
[
"https://raw.githubusercontent.com/mahan-ym/ImageAlfred/main/src/assets/examples/test_3.jpg",
"license plate",
10,
0.5,
],
[
"https://raw.githubusercontent.com/mahan-ym/ImageAlfred/main/src/assets/examples/test_8.jpg",
"face",
15,
0.1,
],
[
"https://raw.githubusercontent.com/mahan-ym/ImageAlfred/main/src/assets/examples/test_6.jpg",
"face",
20,
0.1,
],
],
)
remove_background_tool = gr.Interface(
fn=remove_background,
inputs=[
gr.Image(label="Input Image", type="pil"),
],
outputs=gr.Image(label="Output Image"),
title="Remove Image Background Tool",
description="Upload an image to remove the background.",
examples=[
[
"https://raw.githubusercontent.com/mahan-ym/ImageAlfred/main/src/assets/examples/test_5.jpg",
],
[
"https://raw.githubusercontent.com/mahan-ym/ImageAlfred/main/src/assets/examples/test_6.jpg",
],
[
"https://raw.githubusercontent.com/mahan-ym/ImageAlfred/main/src/assets/examples/test_8.jpg",
],
],
)
demo = gr.TabbedInterface(
[
privacy_preserve_tool,
remove_background_tool,
change_color_objects_hsv_tool,
change_color_objects_lab_tool,
],
[
"Privacy Preserving Tool",
"Remove Background Tool",
"Change Color Objects HSV",
"Change Color Objects LAB",
],
title=title,
theme=gr.themes.Default(
primary_hue="blue",
secondary_hue="green",
# font="Inter",
# font_mono="Courier New",
),
head=icon,
)
# with gr.Blocks(title="Image Alfred", head=test) as demo:
# gr.HTML(header)
# tabs_interface.render()
if __name__ == "__main__":
demo.launch(mcp_server=True, max_file_size="15mb")
|