Upload 3 files
Browse files- LICENSE +189 -0
- requirements.txt +7 -0
- utils.py +170 -0
LICENSE
ADDED
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Apache License
|
2 |
+
Version 2.0, January 2004
|
3 |
+
http://www.apache.org/licenses/
|
4 |
+
|
5 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6 |
+
|
7 |
+
1. Definitions.
|
8 |
+
|
9 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
10 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
11 |
+
|
12 |
+
"Licensor" shall mean the copyright owner or entity granting the License.
|
13 |
+
|
14 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
15 |
+
other entities that control, are controlled by, or are under common
|
16 |
+
control with that entity. For the purposes of the definition of
|
17 |
+
"control", an entity controls another entity when such entity:
|
18 |
+
(i) has the power, direct or indirect, to cause the direction or
|
19 |
+
management of such other entity, whether by contract or otherwise,
|
20 |
+
(ii) owns fifty percent (50%) or more of the outstanding shares, or
|
21 |
+
(iii) has beneficial ownership of such entity.
|
22 |
+
|
23 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24 |
+
exercising permissions granted by this License.
|
25 |
+
|
26 |
+
"Source" shall mean the preferred form for making modifications,
|
27 |
+
including but not limited to software source code, documentation
|
28 |
+
source, and configuration files.
|
29 |
+
|
30 |
+
"Object" shall mean any form resulting from mechanical
|
31 |
+
transformation or translation of a Source form, including but
|
32 |
+
not limited to compiled object code, generated documentation,
|
33 |
+
and conversions to other media types.
|
34 |
+
|
35 |
+
"Work" shall mean the work of authorship, whether in Source or
|
36 |
+
Object form, made available under the License, as indicated by a
|
37 |
+
copyright notice that is included in or attached to the work
|
38 |
+
(which shall not include communication that is conspicuously
|
39 |
+
marked or otherwise designated in writing by the copyright owner
|
40 |
+
as "Not a Contribution").
|
41 |
+
|
42 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
43 |
+
form, that is based upon (or derived from) the Work and for which the
|
44 |
+
editorial revisions, annotations, elaborations, or other modifications
|
45 |
+
represent, as a whole, an original work of authorship. For the purposes
|
46 |
+
of this License, Derivative Works shall not include works that remain
|
47 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
48 |
+
the Work and derivative works thereof.
|
49 |
+
|
50 |
+
"Contribution" shall mean any work of authorship, including
|
51 |
+
the original version of the Work and any modifications or additions
|
52 |
+
to that Work or Derivative Works thereof, that is intentionally
|
53 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
54 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
55 |
+
the copyright owner. For the purposes of the definition of "Contribution",
|
56 |
+
any such Contribution intentionally submitted for inclusion in the Work
|
57 |
+
by You to the Licensor shall be deemed to have been made under the
|
58 |
+
terms and conditions of this License, without any additional terms or
|
59 |
+
conditions. Notwithstanding the above, nothing herein shall supersede or
|
60 |
+
modify the terms of any separate license agreement you may have executed
|
61 |
+
with Licensor regarding such Contributions.
|
62 |
+
|
63 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
64 |
+
this License, each Contributor hereby grants to You a perpetual,
|
65 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
66 |
+
copyright license to use, reproduce, modify, merge, publish,
|
67 |
+
distribute, sublicense, and/or sell copies of the Work, and to
|
68 |
+
permit persons to whom the Work is furnished to do so, subject to
|
69 |
+
the following conditions:
|
70 |
+
|
71 |
+
The above copyright notice and this permission notice shall be
|
72 |
+
included in all copies or substantial portions of the Work.
|
73 |
+
|
74 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75 |
+
this License, each Contributor hereby grants to You a perpetual,
|
76 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77 |
+
(except as stated in this section) patent license to make, have made,
|
78 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79 |
+
where such license applies only to those patent claims licensable
|
80 |
+
by such Contributor that are necessarily infringed by their
|
81 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
82 |
+
with the Work to which such Contribution(s) was submitted. If You
|
83 |
+
institute patent litigation against any entity (including a
|
84 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85 |
+
or a Contribution incorporated within the Work constitutes direct
|
86 |
+
or contributory patent infringement, then any patent licenses
|
87 |
+
granted to You under this License for that Work shall terminate
|
88 |
+
as of the date such litigation is filed.
|
89 |
+
|
90 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
91 |
+
Work or Derivative Works thereof in any medium, with or without
|
92 |
+
modifications, and in Source or Object form, provided that You
|
93 |
+
meet the following conditions:
|
94 |
+
|
95 |
+
(a) You must give any other recipients of the Work or
|
96 |
+
Derivative Works a copy of this License; and
|
97 |
+
|
98 |
+
(b) You must cause any modified files to carry prominent notices
|
99 |
+
stating that You changed the files; and
|
100 |
+
|
101 |
+
(c) You must retain, in the Source form of any Derivative Works
|
102 |
+
that You distribute, all copyright, trademark, patent,
|
103 |
+
and attribution notices from the Source form of the Work,
|
104 |
+
excluding those notices that do not pertain to any part of
|
105 |
+
the Derivative Works; and
|
106 |
+
|
107 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108 |
+
distribution, then any Derivative Works that You distribute must
|
109 |
+
include a readable copy of the attribution notices contained
|
110 |
+
within such NOTICE file, excluding those notices that do not
|
111 |
+
pertain to any part of the Derivative Works, in at least one
|
112 |
+
of the following places: within a NOTICE text file distributed
|
113 |
+
as part of the Derivative Works; within the Source form or
|
114 |
+
documentation, if provided along with the Derivative Works; or,
|
115 |
+
within a display generated by the Derivative Works, if and
|
116 |
+
wherever such third-party notices normally appear. The contents
|
117 |
+
of the NOTICE file are for informational purposes only and
|
118 |
+
do not modify the License. You may add Your own attribution
|
119 |
+
notices within Derivative Works that You distribute, alongside
|
120 |
+
or as an addendum to the NOTICE text from the Work, provided
|
121 |
+
that such additional attribution notices cannot be construed
|
122 |
+
as modifying the License.
|
123 |
+
|
124 |
+
You may add Your own copyright notice to Your modifications and
|
125 |
+
may provide additional or different license terms and conditions
|
126 |
+
for use, reproduction, or distribution of Your modifications, or
|
127 |
+
for any such Derivative Works as a whole, provided Your use,
|
128 |
+
reproduction, and distribution of the Work otherwise complies with
|
129 |
+
the conditions stated in this License.
|
130 |
+
|
131 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132 |
+
any Contribution intentionally submitted for inclusion in the Work
|
133 |
+
by You to the Licensor shall be under the terms and conditions of
|
134 |
+
this License, without any additional terms or conditions.
|
135 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136 |
+
the terms of any separate license agreement you may have executed
|
137 |
+
with Licensor regarding such Contributions.
|
138 |
+
|
139 |
+
6. Trademarks. This License does not grant permission to use the trade
|
140 |
+
names, trademarks, service marks, or product names of the Licensor,
|
141 |
+
except as required for reasonable and customary use in describing the
|
142 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
143 |
+
|
144 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145 |
+
agreed to in writing, Licensor provides the Work (and each
|
146 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148 |
+
implied, including, without limitation, any warranties or conditions
|
149 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151 |
+
appropriateness of using or redistributing the Work and assume any
|
152 |
+
risks associated with Your exercise of permissions under this License.
|
153 |
+
|
154 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
155 |
+
whether in tort (including negligence), contract, or otherwise,
|
156 |
+
unless required by applicable law (such as deliberate and grossly
|
157 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158 |
+
liable to You for damages, including any direct, indirect, special,
|
159 |
+
incidental, or consequential damages of any character arising as a
|
160 |
+
result of this License or out of the use or inability to use the
|
161 |
+
Work (including but not limited to damages for loss of goodwill,
|
162 |
+
work stoppage, computer failure or malfunction, or any and all
|
163 |
+
other commercial damages or losses), even if such Contributor
|
164 |
+
has been advised of the possibility of such damages.
|
165 |
+
|
166 |
+
9. Accepting Warranty or Support. You are not required to accept
|
167 |
+
warranty or support for the Work under this License. However, if You
|
168 |
+
choose to accept warranty or support, You may act only on Your own
|
169 |
+
behalf and on Your sole responsibility, not on behalf of any other
|
170 |
+
Contributor, and only if You agree to indemnify, defend, and hold each
|
171 |
+
Contributor harmless for any liability incurred by, or claims asserted
|
172 |
+
against, such Contributor by reason of your accepting any such warranty
|
173 |
+
or support.
|
174 |
+
|
175 |
+
END OF TERMS AND CONDITIONS
|
176 |
+
|
177 |
+
Copyright 2025 Trendyol
|
178 |
+
|
179 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
180 |
+
you may not use this file except in compliance with the License.
|
181 |
+
You may obtain a copy of the License at
|
182 |
+
|
183 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
184 |
+
|
185 |
+
Unless required by applicable law or agreed to in writing, software
|
186 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
187 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
188 |
+
See the License for the specific language governing permissions and
|
189 |
+
limitations under the License.
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch==2.7.1
|
2 |
+
torchvision==0.22.1
|
3 |
+
opencv-python==4.12.0.88
|
4 |
+
numpy
|
5 |
+
requests
|
6 |
+
Pillow
|
7 |
+
onnxruntime==1.22.1
|
utils.py
ADDED
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import requests
|
4 |
+
from PIL import Image
|
5 |
+
from io import BytesIO
|
6 |
+
import torch
|
7 |
+
from pathlib import Path
|
8 |
+
import torch.nn.functional as F
|
9 |
+
from typing import Dict, Any, List, Union
|
10 |
+
from torchvision.transforms.functional import normalize
|
11 |
+
|
12 |
+
INPUT_SIZE = [1200, 1800]
|
13 |
+
|
14 |
+
def keep_large_components(a: np.ndarray) -> np.ndarray:
|
15 |
+
"""Remove small connected components from a binary mask, keeping only large regions.
|
16 |
+
|
17 |
+
Args:
|
18 |
+
a: Input binary mask as numpy array
|
19 |
+
|
20 |
+
Returns:
|
21 |
+
Processed mask with only large connected components remaining
|
22 |
+
"""
|
23 |
+
dilate_kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(9, 9))
|
24 |
+
a_mask = (a > 25).astype(np.uint8) * 255
|
25 |
+
|
26 |
+
# Apply the Component analysis function
|
27 |
+
analysis = cv2.connectedComponentsWithStats(a_mask, 4, cv2.CV_32S)
|
28 |
+
(totalLabels, label_ids, values, centroid) = analysis
|
29 |
+
|
30 |
+
# Find the components to be kept
|
31 |
+
h, w = a.shape[:2]
|
32 |
+
area_limit = 50000 * (h * w) / (INPUT_SIZE[1] * INPUT_SIZE[0])
|
33 |
+
i_to_keep = []
|
34 |
+
for i in range(1, totalLabels):
|
35 |
+
area = values[i, cv2.CC_STAT_AREA]
|
36 |
+
if area > area_limit:
|
37 |
+
i_to_keep.append(i)
|
38 |
+
|
39 |
+
if len(i_to_keep) > 0:
|
40 |
+
# Or masks to be kept
|
41 |
+
final_mask = np.zeros_like(a, dtype=np.uint8)
|
42 |
+
for i in i_to_keep:
|
43 |
+
componentMask = (label_ids == i).astype("uint8") * 255
|
44 |
+
final_mask = cv2.bitwise_or(final_mask, componentMask)
|
45 |
+
|
46 |
+
# Remove other components
|
47 |
+
# Keep edges
|
48 |
+
final_mask = cv2.dilate(final_mask, dilate_kernel, iterations = 2)
|
49 |
+
a = cv2.bitwise_and(a, final_mask)
|
50 |
+
a = a.reshape((a.shape[0], a.shape[1], 1))
|
51 |
+
|
52 |
+
return a
|
53 |
+
|
54 |
+
def read_img(img: Union[str, Path]) -> np.ndarray:
|
55 |
+
"""Read an image from a URL or local path.
|
56 |
+
|
57 |
+
Args:
|
58 |
+
img: URL or file path to image
|
59 |
+
|
60 |
+
Returns:
|
61 |
+
Image as numpy array in RGB format
|
62 |
+
"""
|
63 |
+
if img[0: 4] == 'http':
|
64 |
+
response = requests.get(img)
|
65 |
+
im = np.asarray(Image.open(BytesIO(response.content)))
|
66 |
+
|
67 |
+
else:
|
68 |
+
im = cv2.imread(str(img))
|
69 |
+
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
|
70 |
+
|
71 |
+
return im
|
72 |
+
|
73 |
+
def preprocess_input(im: np.ndarray) -> torch.Tensor:
|
74 |
+
"""Preprocess image for model input.
|
75 |
+
|
76 |
+
Args:
|
77 |
+
im: Input image as numpy array
|
78 |
+
|
79 |
+
Returns:
|
80 |
+
Preprocessed image as normalized torch tensor
|
81 |
+
"""
|
82 |
+
if len(im.shape) < 3:
|
83 |
+
im = im[:, :, np.newaxis]
|
84 |
+
|
85 |
+
if im.shape[2] == 4: # if image has alpha channel, remove it
|
86 |
+
im = im[:,:,:3]
|
87 |
+
|
88 |
+
im_tensor = torch.tensor(im, dtype=torch.float32).permute(2,0,1)
|
89 |
+
im_tensor = F.upsample(torch.unsqueeze(im_tensor,0), INPUT_SIZE, mode="bilinear").type(torch.uint8)
|
90 |
+
image = torch.divide(im_tensor,255.0)
|
91 |
+
image = normalize(image,[0.5,0.5,0.5],[1.0,1.0,1.0])
|
92 |
+
|
93 |
+
if torch.cuda.is_available():
|
94 |
+
image=image.cuda()
|
95 |
+
|
96 |
+
return image
|
97 |
+
|
98 |
+
def postprocess_output(result: List[torch.Tensor]) -> np.ndarray:
|
99 |
+
"""Postprocess model output.
|
100 |
+
|
101 |
+
Args:
|
102 |
+
result: Model output as list of tensors
|
103 |
+
|
104 |
+
Returns:
|
105 |
+
Processed binary mask as numpy array
|
106 |
+
"""
|
107 |
+
result = torch.squeeze(F.upsample(
|
108 |
+
result[0][0], (INPUT_SIZE[1], INPUT_SIZE[0]), mode='bilinear'), 0)
|
109 |
+
ma = torch.max(result)
|
110 |
+
mi = torch.min(result)
|
111 |
+
result = (result-mi)/(ma-mi)
|
112 |
+
|
113 |
+
# a is alpha channel. 255 means foreground, 0 means background.
|
114 |
+
a = (result*255).permute(1,2,0).cpu().data.numpy().astype(np.uint8)
|
115 |
+
|
116 |
+
# postprocessing
|
117 |
+
a = keep_large_components(a)
|
118 |
+
|
119 |
+
return a
|
120 |
+
|
121 |
+
def postprocess_output_onnx(result: np.ndarray) -> np.ndarray:
|
122 |
+
"""Postprocess ONNX model output.
|
123 |
+
|
124 |
+
Args:
|
125 |
+
result: Model output as numpy array
|
126 |
+
|
127 |
+
Returns:
|
128 |
+
Processed binary mask as numpy array
|
129 |
+
"""
|
130 |
+
result = torch.squeeze(F.upsample(
|
131 |
+
torch.from_numpy(result).unsqueeze(0), (INPUT_SIZE[1], INPUT_SIZE[0]), mode='bilinear'), 0)
|
132 |
+
ma = torch.max(result)
|
133 |
+
mi = torch.min(result)
|
134 |
+
result = (result-mi)/(ma-mi)
|
135 |
+
|
136 |
+
# a is alpha channel. 255 means foreground, 0 means background.
|
137 |
+
a = (result*255).permute(1,2,0).cpu().data.numpy().astype(np.uint8)
|
138 |
+
|
139 |
+
# postprocessing
|
140 |
+
a = keep_large_components(a)
|
141 |
+
|
142 |
+
return a
|
143 |
+
|
144 |
+
def process_image(src: str, ort_session: Any, model_path: str, outname: str) -> None:
|
145 |
+
"""Process an image through ONNX model to generate alpha mask and save result.
|
146 |
+
|
147 |
+
Args:
|
148 |
+
src: Source image URL or path
|
149 |
+
ort_session: ONNX runtime inference session
|
150 |
+
model_path: Path to ONNX model file
|
151 |
+
outname: Output filename for saving result
|
152 |
+
|
153 |
+
Returns:
|
154 |
+
None
|
155 |
+
"""
|
156 |
+
# Load and preprocess image
|
157 |
+
image_orig = read_img(src)
|
158 |
+
image = preprocess_input(image_orig)
|
159 |
+
|
160 |
+
# Prepare ONNX input
|
161 |
+
inputs: Dict[str, Any] = {ort_session.get_inputs()[0].name: image.numpy()}
|
162 |
+
|
163 |
+
# Get ONNX output and post-process
|
164 |
+
result = ort_session.run(None, inputs)[0][0]
|
165 |
+
alpha = postprocess_output_onnx(result)
|
166 |
+
|
167 |
+
# Combine RGB image with alpha mask and save
|
168 |
+
img_w_alpha = np.dstack((cv2.cvtColor(image_orig, cv2.COLOR_BGR2RGB), alpha))
|
169 |
+
cv2.imwrite(outname, img_w_alpha)
|
170 |
+
print(f"Saved: {outname}")
|