mahan_ym commited on
Commit
697792b
·
1 Parent(s): e0d0ee2

update related code for recoloring. added search with rapidfuzz

Browse files
pyproject.toml CHANGED
@@ -19,6 +19,7 @@ dev = [
19
  "jupyterlab>=4.4.3",
20
  "matplotlib>=3.10.3",
21
  "opencv-contrib-python>=4.11.0.86",
 
22
  "ruff>=0.11.12",
23
  "supervision>=0.25.1",
24
  ]
 
19
  "jupyterlab>=4.4.3",
20
  "matplotlib>=3.10.3",
21
  "opencv-contrib-python>=4.11.0.86",
22
+ "rapidfuzz>=3.13.0",
23
  "ruff>=0.11.12",
24
  "supervision>=0.25.1",
25
  ]
src/app.py CHANGED
@@ -127,7 +127,7 @@ remove_background_tool = gr.Interface(
127
  ],
128
  outputs=gr.Image(label="Output Image"),
129
  title="Remove Image Background Tool",
130
- description="Upload an image remove the background.",
131
  examples=[
132
  [
133
  "https://raw.githubusercontent.com/mahan-ym/ImageAlfred/main/src/assets/examples/test_5.jpg",
 
127
  ],
128
  outputs=gr.Image(label="Output Image"),
129
  title="Remove Image Background Tool",
130
+ description="Upload an image to remove the background.",
131
  examples=[
132
  [
133
  "https://raw.githubusercontent.com/mahan-ym/ImageAlfred/main/src/assets/examples/test_5.jpg",
src/assets/examples/test_7.jpg ADDED

Git LFS Details

  • SHA256: 1ab95b5752d51f55bf4d774b4bd028e66897b4ca2b1c459f73014cca949d0945
  • Pointer size: 132 Bytes
  • Size of remote file: 1.47 MB
src/assets/examples/test_8.jpg ADDED

Git LFS Details

  • SHA256: a28b42702894d8ebc72c7f80b1ee218cbe2f0a4db9b554f70b07ef3aa823ffb4
  • Pointer size: 132 Bytes
  • Size of remote file: 1.22 MB
src/modal_app.py CHANGED
@@ -5,6 +5,7 @@ import cv2
5
  import modal
6
  import numpy as np
7
  from PIL import Image
 
8
 
9
  app = modal.App("ImageAlfred")
10
 
@@ -36,6 +37,7 @@ image = (
36
  "Pillow",
37
  "numpy",
38
  "opencv-contrib-python-headless",
 
39
  gpu="A10G",
40
  )
41
  .pip_install(
@@ -130,27 +132,25 @@ def change_image_objects_hsv(
130
  langsam_results = lang_sam_segment.remote(image_pil=image_pil, prompt=prompts)
131
  if not langsam_results:
132
  return image_pil
133
-
134
- labels = langsam_results[0]["labels"]
135
  scores = langsam_results[0]["scores"]
136
 
137
  img_array = np.array(image_pil)
138
  img_hsv = cv2.cvtColor(img_array, cv2.COLOR_RGB2HSV).astype(np.float32)
139
 
140
- for target_spec in targets_config:
141
- target_obj = target_spec[0]
142
- hue = target_spec[1]
143
- saturation_scale = target_spec[2]
144
-
145
- try:
146
- mask_idx = labels.index(target_obj)
147
- except ValueError:
148
- print(
149
- f"Warning: Label '{target_obj}' not found in the image. Skipping this target." # noqa: E501
150
- )
151
  continue
 
 
 
 
 
 
152
 
153
- mask = langsam_results[0]["masks"][mask_idx]
154
  mask_bool = mask.astype(bool)
155
 
156
  img_hsv[mask_bool, 0] = float(hue)
@@ -204,25 +204,25 @@ def change_image_objects_lab(
204
  )
205
  if not langsam_results:
206
  return image_pil
207
-
208
- labels = langsam_results[0]["labels"]
 
209
  scores = langsam_results[0]["scores"]
 
210
  img_array = np.array(image_pil)
211
  img_lab = cv2.cvtColor(img_array, cv2.COLOR_RGB2Lab).astype(np.float32)
212
- for target_spec in targets_config:
213
- target_obj = target_spec[0]
214
- new_a = target_spec[1]
215
- new_b = target_spec[2]
216
-
217
- try:
218
- mask_idx = labels.index(target_obj)
219
- except ValueError:
220
- print(
221
- f"Warning: Label '{target_obj}' not found in the image. Skipping this target." # noqa: E501
222
- )
223
  continue
 
 
 
 
 
224
 
225
- mask = langsam_results[0]["masks"][mask_idx]
226
  mask_bool = mask.astype(bool)
227
 
228
  img_lab[mask_bool, 1] = new_a
 
5
  import modal
6
  import numpy as np
7
  from PIL import Image
8
+ from rapidfuzz import process
9
 
10
  app = modal.App("ImageAlfred")
11
 
 
37
  "Pillow",
38
  "numpy",
39
  "opencv-contrib-python-headless",
40
+ "RapidFuzz",
41
  gpu="A10G",
42
  )
43
  .pip_install(
 
132
  langsam_results = lang_sam_segment.remote(image_pil=image_pil, prompt=prompts)
133
  if not langsam_results:
134
  return image_pil
135
+ input_labels = [target[0] for target in targets_config]
136
+ output_labels = langsam_results[0]["labels"]
137
  scores = langsam_results[0]["scores"]
138
 
139
  img_array = np.array(image_pil)
140
  img_hsv = cv2.cvtColor(img_array, cv2.COLOR_RGB2HSV).astype(np.float32)
141
 
142
+ for idx, label in enumerate(output_labels):
143
+ if not label or label=="":
144
+ print("Skipping empty label.")
 
 
 
 
 
 
 
 
145
  continue
146
+ input_label, score, _ = process.extractOne(label, input_labels)
147
+ input_label_idx = input_labels.index(input_label)
148
+
149
+ hue = targets_config[input_label_idx][1]
150
+ saturation_scale = targets_config[input_label_idx][2]
151
+
152
 
153
+ mask = langsam_results[0]["masks"][idx]
154
  mask_bool = mask.astype(bool)
155
 
156
  img_hsv[mask_bool, 0] = float(hue)
 
204
  )
205
  if not langsam_results:
206
  return image_pil
207
+
208
+ input_labels = [target[0] for target in targets_config]
209
+ output_labels = langsam_results[0]["labels"]
210
  scores = langsam_results[0]["scores"]
211
+
212
  img_array = np.array(image_pil)
213
  img_lab = cv2.cvtColor(img_array, cv2.COLOR_RGB2Lab).astype(np.float32)
214
+
215
+ for idx, label in enumerate(output_labels):
216
+ if not label or label == "":
217
+ print("Skipping empty label.")
 
 
 
 
 
 
 
218
  continue
219
+ input_label, score, _ = process.extractOne(label, input_labels)
220
+ input_label_idx = input_labels.index(input_label)
221
+
222
+ new_a = targets_config[input_label_idx][1]
223
+ new_b = targets_config[input_label_idx][2]
224
 
225
+ mask = langsam_results[0]["masks"][idx]
226
  mask_bool = mask.astype(bool)
227
 
228
  img_lab[mask_bool, 1] = new_a
uv.lock CHANGED
@@ -841,6 +841,7 @@ dev = [
841
  { name = "jupyterlab" },
842
  { name = "matplotlib" },
843
  { name = "opencv-contrib-python" },
 
844
  { name = "ruff" },
845
  { name = "supervision" },
846
  ]
@@ -858,6 +859,7 @@ dev = [
858
  { name = "jupyterlab", specifier = ">=4.4.3" },
859
  { name = "matplotlib", specifier = ">=3.10.3" },
860
  { name = "opencv-contrib-python", specifier = ">=4.11.0.86" },
 
861
  { name = "ruff", specifier = ">=0.11.12" },
862
  { name = "supervision", specifier = ">=0.25.1" },
863
  ]
@@ -2128,6 +2130,44 @@ wheels = [
2128
  { url = "https://files.pythonhosted.org/packages/05/4c/bf3cad0d64c3214ac881299c4562b815f05d503bccc513e3fd4fdc6f67e4/pyzmq-26.4.0-cp313-cp313t-musllinux_1_1_x86_64.whl", hash = "sha256:26a2a7451606b87f67cdeca2c2789d86f605da08b4bd616b1a9981605ca3a364", size = 1395540, upload-time = "2025-04-04T12:04:30.562Z" },
2129
  ]
2130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2131
  [[package]]
2132
  name = "referencing"
2133
  version = "0.36.2"
 
841
  { name = "jupyterlab" },
842
  { name = "matplotlib" },
843
  { name = "opencv-contrib-python" },
844
+ { name = "rapidfuzz" },
845
  { name = "ruff" },
846
  { name = "supervision" },
847
  ]
 
859
  { name = "jupyterlab", specifier = ">=4.4.3" },
860
  { name = "matplotlib", specifier = ">=3.10.3" },
861
  { name = "opencv-contrib-python", specifier = ">=4.11.0.86" },
862
+ { name = "rapidfuzz", specifier = ">=3.13.0" },
863
  { name = "ruff", specifier = ">=0.11.12" },
864
  { name = "supervision", specifier = ">=0.25.1" },
865
  ]
 
2130
  { url = "https://files.pythonhosted.org/packages/05/4c/bf3cad0d64c3214ac881299c4562b815f05d503bccc513e3fd4fdc6f67e4/pyzmq-26.4.0-cp313-cp313t-musllinux_1_1_x86_64.whl", hash = "sha256:26a2a7451606b87f67cdeca2c2789d86f605da08b4bd616b1a9981605ca3a364", size = 1395540, upload-time = "2025-04-04T12:04:30.562Z" },
2131
  ]
2132
 
2133
+ [[package]]
2134
+ name = "rapidfuzz"
2135
+ version = "3.13.0"
2136
+ source = { registry = "https://pypi.org/simple" }
2137
+ sdist = { url = "https://files.pythonhosted.org/packages/ed/f6/6895abc3a3d056b9698da3199b04c0e56226d530ae44a470edabf8b664f0/rapidfuzz-3.13.0.tar.gz", hash = "sha256:d2eaf3839e52cbcc0accbe9817a67b4b0fcf70aaeb229cfddc1c28061f9ce5d8", size = 57904226, upload-time = "2025-04-03T20:38:51.226Z" }
2138
+ wheels = [
2139
+ { url = "https://files.pythonhosted.org/packages/13/4b/a326f57a4efed8f5505b25102797a58e37ee11d94afd9d9422cb7c76117e/rapidfuzz-3.13.0-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:4a1a6a906ba62f2556372282b1ef37b26bca67e3d2ea957277cfcefc6275cca7", size = 1989501, upload-time = "2025-04-03T20:36:13.43Z" },
2140
+ { url = "https://files.pythonhosted.org/packages/b7/53/1f7eb7ee83a06c400089ec7cb841cbd581c2edd7a4b21eb2f31030b88daa/rapidfuzz-3.13.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:2fd0975e015b05c79a97f38883a11236f5a24cca83aa992bd2558ceaa5652b26", size = 1445379, upload-time = "2025-04-03T20:36:16.439Z" },
2141
+ { url = "https://files.pythonhosted.org/packages/07/09/de8069a4599cc8e6d194e5fa1782c561151dea7d5e2741767137e2a8c1f0/rapidfuzz-3.13.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:5d4e13593d298c50c4f94ce453f757b4b398af3fa0fd2fde693c3e51195b7f69", size = 1405986, upload-time = "2025-04-03T20:36:18.447Z" },
2142
+ { url = "https://files.pythonhosted.org/packages/5d/77/d9a90b39c16eca20d70fec4ca377fbe9ea4c0d358c6e4736ab0e0e78aaf6/rapidfuzz-3.13.0-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:ed6f416bda1c9133000009d84d9409823eb2358df0950231cc936e4bf784eb97", size = 5310809, upload-time = "2025-04-03T20:36:20.324Z" },
2143
+ { url = "https://files.pythonhosted.org/packages/1e/7d/14da291b0d0f22262d19522afaf63bccf39fc027c981233fb2137a57b71f/rapidfuzz-3.13.0-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:1dc82b6ed01acb536b94a43996a94471a218f4d89f3fdd9185ab496de4b2a981", size = 1629394, upload-time = "2025-04-03T20:36:22.256Z" },
2144
+ { url = "https://files.pythonhosted.org/packages/b7/e4/79ed7e4fa58f37c0f8b7c0a62361f7089b221fe85738ae2dbcfb815e985a/rapidfuzz-3.13.0-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:e9d824de871daa6e443b39ff495a884931970d567eb0dfa213d234337343835f", size = 1600544, upload-time = "2025-04-03T20:36:24.207Z" },
2145
+ { url = "https://files.pythonhosted.org/packages/4e/20/e62b4d13ba851b0f36370060025de50a264d625f6b4c32899085ed51f980/rapidfuzz-3.13.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:2d18228a2390375cf45726ce1af9d36ff3dc1f11dce9775eae1f1b13ac6ec50f", size = 3052796, upload-time = "2025-04-03T20:36:26.279Z" },
2146
+ { url = "https://files.pythonhosted.org/packages/cd/8d/55fdf4387dec10aa177fe3df8dbb0d5022224d95f48664a21d6b62a5299d/rapidfuzz-3.13.0-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:9f5fe634c9482ec5d4a6692afb8c45d370ae86755e5f57aa6c50bfe4ca2bdd87", size = 2464016, upload-time = "2025-04-03T20:36:28.525Z" },
2147
+ { url = "https://files.pythonhosted.org/packages/9b/be/0872f6a56c0f473165d3b47d4170fa75263dc5f46985755aa9bf2bbcdea1/rapidfuzz-3.13.0-cp312-cp312-musllinux_1_2_i686.whl", hash = "sha256:694eb531889f71022b2be86f625a4209c4049e74be9ca836919b9e395d5e33b3", size = 7556725, upload-time = "2025-04-03T20:36:30.629Z" },
2148
+ { url = "https://files.pythonhosted.org/packages/5d/f3/6c0750e484d885a14840c7a150926f425d524982aca989cdda0bb3bdfa57/rapidfuzz-3.13.0-cp312-cp312-musllinux_1_2_ppc64le.whl", hash = "sha256:11b47b40650e06147dee5e51a9c9ad73bb7b86968b6f7d30e503b9f8dd1292db", size = 2859052, upload-time = "2025-04-03T20:36:32.836Z" },
2149
+ { url = "https://files.pythonhosted.org/packages/6f/98/5a3a14701b5eb330f444f7883c9840b43fb29c575e292e09c90a270a6e07/rapidfuzz-3.13.0-cp312-cp312-musllinux_1_2_s390x.whl", hash = "sha256:98b8107ff14f5af0243f27d236bcc6e1ef8e7e3b3c25df114e91e3a99572da73", size = 3390219, upload-time = "2025-04-03T20:36:35.062Z" },
2150
+ { url = "https://files.pythonhosted.org/packages/e9/7d/f4642eaaeb474b19974332f2a58471803448be843033e5740965775760a5/rapidfuzz-3.13.0-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:b836f486dba0aceb2551e838ff3f514a38ee72b015364f739e526d720fdb823a", size = 4377924, upload-time = "2025-04-03T20:36:37.363Z" },
2151
+ { url = "https://files.pythonhosted.org/packages/8e/83/fa33f61796731891c3e045d0cbca4436a5c436a170e7f04d42c2423652c3/rapidfuzz-3.13.0-cp312-cp312-win32.whl", hash = "sha256:4671ee300d1818d7bdfd8fa0608580d7778ba701817216f0c17fb29e6b972514", size = 1823915, upload-time = "2025-04-03T20:36:39.451Z" },
2152
+ { url = "https://files.pythonhosted.org/packages/03/25/5ee7ab6841ca668567d0897905eebc79c76f6297b73bf05957be887e9c74/rapidfuzz-3.13.0-cp312-cp312-win_amd64.whl", hash = "sha256:6e2065f68fb1d0bf65adc289c1bdc45ba7e464e406b319d67bb54441a1b9da9e", size = 1616985, upload-time = "2025-04-03T20:36:41.631Z" },
2153
+ { url = "https://files.pythonhosted.org/packages/76/5e/3f0fb88db396cb692aefd631e4805854e02120a2382723b90dcae720bcc6/rapidfuzz-3.13.0-cp312-cp312-win_arm64.whl", hash = "sha256:65cc97c2fc2c2fe23586599686f3b1ceeedeca8e598cfcc1b7e56dc8ca7e2aa7", size = 860116, upload-time = "2025-04-03T20:36:43.915Z" },
2154
+ { url = "https://files.pythonhosted.org/packages/0a/76/606e71e4227790750f1646f3c5c873e18d6cfeb6f9a77b2b8c4dec8f0f66/rapidfuzz-3.13.0-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:09e908064d3684c541d312bd4c7b05acb99a2c764f6231bd507d4b4b65226c23", size = 1982282, upload-time = "2025-04-03T20:36:46.149Z" },
2155
+ { url = "https://files.pythonhosted.org/packages/0a/f5/d0b48c6b902607a59fd5932a54e3518dae8223814db8349b0176e6e9444b/rapidfuzz-3.13.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:57c390336cb50d5d3bfb0cfe1467478a15733703af61f6dffb14b1cd312a6fae", size = 1439274, upload-time = "2025-04-03T20:36:48.323Z" },
2156
+ { url = "https://files.pythonhosted.org/packages/59/cf/c3ac8c80d8ced6c1f99b5d9674d397ce5d0e9d0939d788d67c010e19c65f/rapidfuzz-3.13.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:0da54aa8547b3c2c188db3d1c7eb4d1bb6dd80baa8cdaeaec3d1da3346ec9caa", size = 1399854, upload-time = "2025-04-03T20:36:50.294Z" },
2157
+ { url = "https://files.pythonhosted.org/packages/09/5d/ca8698e452b349c8313faf07bfa84e7d1c2d2edf7ccc67bcfc49bee1259a/rapidfuzz-3.13.0-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:df8e8c21e67afb9d7fbe18f42c6111fe155e801ab103c81109a61312927cc611", size = 5308962, upload-time = "2025-04-03T20:36:52.421Z" },
2158
+ { url = "https://files.pythonhosted.org/packages/66/0a/bebada332854e78e68f3d6c05226b23faca79d71362509dbcf7b002e33b7/rapidfuzz-3.13.0-cp313-cp313-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:461fd13250a2adf8e90ca9a0e1e166515cbcaa5e9c3b1f37545cbbeff9e77f6b", size = 1625016, upload-time = "2025-04-03T20:36:54.639Z" },
2159
+ { url = "https://files.pythonhosted.org/packages/de/0c/9e58d4887b86d7121d1c519f7050d1be5eb189d8a8075f5417df6492b4f5/rapidfuzz-3.13.0-cp313-cp313-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:c2b3dd5d206a12deca16870acc0d6e5036abeb70e3cad6549c294eff15591527", size = 1600414, upload-time = "2025-04-03T20:36:56.669Z" },
2160
+ { url = "https://files.pythonhosted.org/packages/9b/df/6096bc669c1311568840bdcbb5a893edc972d1c8d2b4b4325c21d54da5b1/rapidfuzz-3.13.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:1343d745fbf4688e412d8f398c6e6d6f269db99a54456873f232ba2e7aeb4939", size = 3053179, upload-time = "2025-04-03T20:36:59.366Z" },
2161
+ { url = "https://files.pythonhosted.org/packages/f9/46/5179c583b75fce3e65a5cd79a3561bd19abd54518cb7c483a89b284bf2b9/rapidfuzz-3.13.0-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:b1b065f370d54551dcc785c6f9eeb5bd517ae14c983d2784c064b3aa525896df", size = 2456856, upload-time = "2025-04-03T20:37:01.708Z" },
2162
+ { url = "https://files.pythonhosted.org/packages/6b/64/e9804212e3286d027ac35bbb66603c9456c2bce23f823b67d2f5cabc05c1/rapidfuzz-3.13.0-cp313-cp313-musllinux_1_2_i686.whl", hash = "sha256:11b125d8edd67e767b2295eac6eb9afe0b1cdc82ea3d4b9257da4b8e06077798", size = 7567107, upload-time = "2025-04-03T20:37:04.521Z" },
2163
+ { url = "https://files.pythonhosted.org/packages/8a/f2/7d69e7bf4daec62769b11757ffc31f69afb3ce248947aadbb109fefd9f65/rapidfuzz-3.13.0-cp313-cp313-musllinux_1_2_ppc64le.whl", hash = "sha256:c33f9c841630b2bb7e69a3fb5c84a854075bb812c47620978bddc591f764da3d", size = 2854192, upload-time = "2025-04-03T20:37:06.905Z" },
2164
+ { url = "https://files.pythonhosted.org/packages/05/21/ab4ad7d7d0f653e6fe2e4ccf11d0245092bef94cdff587a21e534e57bda8/rapidfuzz-3.13.0-cp313-cp313-musllinux_1_2_s390x.whl", hash = "sha256:ae4574cb66cf1e85d32bb7e9ec45af5409c5b3970b7ceb8dea90168024127566", size = 3398876, upload-time = "2025-04-03T20:37:09.692Z" },
2165
+ { url = "https://files.pythonhosted.org/packages/0f/a8/45bba94c2489cb1ee0130dcb46e1df4fa2c2b25269e21ffd15240a80322b/rapidfuzz-3.13.0-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:e05752418b24bbd411841b256344c26f57da1148c5509e34ea39c7eb5099ab72", size = 4377077, upload-time = "2025-04-03T20:37:11.929Z" },
2166
+ { url = "https://files.pythonhosted.org/packages/0c/f3/5e0c6ae452cbb74e5436d3445467447e8c32f3021f48f93f15934b8cffc2/rapidfuzz-3.13.0-cp313-cp313-win32.whl", hash = "sha256:0e1d08cb884805a543f2de1f6744069495ef527e279e05370dd7c83416af83f8", size = 1822066, upload-time = "2025-04-03T20:37:14.425Z" },
2167
+ { url = "https://files.pythonhosted.org/packages/96/e3/a98c25c4f74051df4dcf2f393176b8663bfd93c7afc6692c84e96de147a2/rapidfuzz-3.13.0-cp313-cp313-win_amd64.whl", hash = "sha256:9a7c6232be5f809cd39da30ee5d24e6cadd919831e6020ec6c2391f4c3bc9264", size = 1615100, upload-time = "2025-04-03T20:37:16.611Z" },
2168
+ { url = "https://files.pythonhosted.org/packages/60/b1/05cd5e697c00cd46d7791915f571b38c8531f714832eff2c5e34537c49ee/rapidfuzz-3.13.0-cp313-cp313-win_arm64.whl", hash = "sha256:3f32f15bacd1838c929b35c84b43618481e1b3d7a61b5ed2db0291b70ae88b53", size = 858976, upload-time = "2025-04-03T20:37:19.336Z" },
2169
+ ]
2170
+
2171
  [[package]]
2172
  name = "referencing"
2173
  version = "0.36.2"