flix-clip2 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- flix_clip2/__init__.py +4 -0
- flix_clip2/api.py +214 -0
- flix_clip2/api_v4.py +478 -0
- flix_clip2/api_v5.py +554 -0
- flix_clip2/api_v6.py +570 -0
- flix_clip2/api_v7.py +636 -0
- flix_clip2/birefnet_flix/__init__.py +0 -0
- flix_clip2/birefnet_flix/api.py +149 -0
- flix_clip2/birefnet_flix/config.py +186 -0
- flix_clip2/birefnet_flix/dataset.py +121 -0
- flix_clip2/birefnet_flix/eval_existingOnes.py +155 -0
- flix_clip2/birefnet_flix/evaluation/__init__.py +0 -0
- flix_clip2/birefnet_flix/evaluation/metrics.py +791 -0
- flix_clip2/birefnet_flix/gen_best_ep.py +85 -0
- flix_clip2/birefnet_flix/image_proc.py +116 -0
- flix_clip2/birefnet_flix/infer_flix.py +81 -0
- flix_clip2/birefnet_flix/inference.py +106 -0
- flix_clip2/birefnet_flix/loss.py +277 -0
- flix_clip2/birefnet_flix/models/__init__.py +0 -0
- flix_clip2/birefnet_flix/models/backbones/__init__.py +0 -0
- flix_clip2/birefnet_flix/models/backbones/build_backbone.py +44 -0
- flix_clip2/birefnet_flix/models/backbones/pvt_v2.py +435 -0
- flix_clip2/birefnet_flix/models/backbones/swin_v1.py +627 -0
- flix_clip2/birefnet_flix/models/birefnet.py +286 -0
- flix_clip2/birefnet_flix/models/modules/__init__.py +0 -0
- flix_clip2/birefnet_flix/models/modules/aspp.py +119 -0
- flix_clip2/birefnet_flix/models/modules/decoder_blocks.py +65 -0
- flix_clip2/birefnet_flix/models/modules/deform_conv.py +66 -0
- flix_clip2/birefnet_flix/models/modules/lateral_blocks.py +21 -0
- flix_clip2/birefnet_flix/models/modules/mlp.py +118 -0
- flix_clip2/birefnet_flix/models/modules/prompt_encoder.py +222 -0
- flix_clip2/birefnet_flix/models/modules/utils.py +54 -0
- flix_clip2/birefnet_flix/models/refinement/__init__.py +0 -0
- flix_clip2/birefnet_flix/models/refinement/refiner.py +252 -0
- flix_clip2/birefnet_flix/models/refinement/stem_layer.py +45 -0
- flix_clip2/birefnet_flix/train.py +337 -0
- flix_clip2/birefnet_flix/unit_test_biref.py +15 -0
- flix_clip2/birefnet_flix/utils.py +97 -0
- flix_clip2/dis_flix/Inference.py +53 -0
- flix_clip2/dis_flix/__init__.py +4 -0
- flix_clip2/dis_flix/api.py +150 -0
- flix_clip2/dis_flix/basics.py +74 -0
- flix_clip2/dis_flix/data_loader_cache.py +385 -0
- flix_clip2/dis_flix/hce_metric_main.py +188 -0
- flix_clip2/dis_flix/models/__init__.py +5 -0
- flix_clip2/dis_flix/models/isnet.py +610 -0
- flix_clip2/dis_flix/setup.py +16 -0
- flix_clip2/dis_flix/train_board_test.py +808 -0
- flix_clip2/dis_flix/train_board_test_beer.py +773 -0
- flix_clip2/dis_flix/train_valid_inference_main.py +731 -0
- flix_clip2/dis_flix/vizualization.py +59 -0
- flix_clip2/img2psd.py +102 -0
- flix_clip2/matteformer/__init__.py +0 -0
- flix_clip2/matteformer/api.py +348 -0
- flix_clip2/matteformer/dataloader/__init__.py +0 -0
- flix_clip2/matteformer/dataloader/data_generator.py +922 -0
- flix_clip2/matteformer/dataloader/image_file.py +130 -0
- flix_clip2/matteformer/dataloader/prefetcher.py +42 -0
- flix_clip2/matteformer/evaluation.py +50 -0
- flix_clip2/matteformer/generate_metrics.py +120 -0
- flix_clip2/matteformer/generate_psd.py +198 -0
- flix_clip2/matteformer/img2psd.py +102 -0
- flix_clip2/matteformer/inference.py +318 -0
- flix_clip2/matteformer/main.py +163 -0
- flix_clip2/matteformer/networks/__init__.py +1 -0
- flix_clip2/matteformer/networks/decoders/__init__.py +7 -0
- flix_clip2/matteformer/networks/decoders/resnet_decoder.py +219 -0
- flix_clip2/matteformer/networks/encoders/MatteFormer.py +643 -0
- flix_clip2/matteformer/networks/encoders/__init__.py +0 -0
- flix_clip2/matteformer/networks/generators.py +117 -0
- flix_clip2/matteformer/networks/ops.py +78 -0
- flix_clip2/matteformer/trainers/__init__.py +0 -0
- flix_clip2/matteformer/trainers/trainer.py +744 -0
- flix_clip2/matteformer/unit_test.py +19 -0
- flix_clip2/matteformer/unit_test_folder.py +40 -0
- flix_clip2/matteformer/utils/__init__.py +4 -0
- flix_clip2/matteformer/utils/config.py +127 -0
- flix_clip2/matteformer/utils/evaluate.py +17 -0
- flix_clip2/matteformer/utils/logger.py +102 -0
- flix_clip2/matteformer/utils/util.py +133 -0
- flix_clip2/metric_batches.py +166 -0
- flix_clip2/objdetect_pytorch/__init__.py +1 -0
- flix_clip2/objdetect_pytorch/api.py +185 -0
- flix_clip2/objdetect_pytorch/cp_dataset.py +134 -0
- flix_clip2/objdetect_pytorch/infer.py +101 -0
- flix_clip2/objdetect_pytorch/model.py +15 -0
- flix_clip2/objdetect_pytorch/preproc.py +96 -0
- flix_clip2/objdetect_pytorch/preproc_1ch.py +111 -0
- flix_clip2/objdetect_pytorch/preproc_obj.py +66 -0
- flix_clip2/objdetect_pytorch/test_unit_obj_v2.py +24 -0
- flix_clip2/objdetect_pytorch/train.py +196 -0
- flix_clip2/objdetect_pytorch/utils.py +113 -0
- flix_clip2/psd_inspy.py +154 -0
- flix_clip2/psd_inspy_list.py +157 -0
- flix_clip2/sam2_flix/__init__.py +0 -0
- flix_clip2/sam2_flix/cp_dataset.py +139 -0
- flix_clip2/sam2_flix/infer.py +151 -0
- flix_clip2/sam2_flix/run.py +185 -0
- flix_clip2/sam2_flix/train.py +242 -0
- flix_clip2/sam2_flix/unit_test.py +23 -0
- flix_clip2/sam2_flix/utils.py +101 -0
- flix_clip2/sapiens_flix/__init__.py +0 -0
- flix_clip2/sapiens_flix/adhoc_image_dataset.py +43 -0
- flix_clip2/sapiens_flix/classes_and_palettes.py +568 -0
- flix_clip2/sapiens_flix/detector_utils.py +158 -0
- flix_clip2/sapiens_flix/extract_feature.py +207 -0
- flix_clip2/sapiens_flix/pose_utils.py +312 -0
- flix_clip2/sapiens_flix/sapiens_api.py +303 -0
- flix_clip2/sapiens_flix/sapiens_api_v2.py +147 -0
- flix_clip2/sapiens_flix/sapiens_api_v3.py +193 -0
- flix_clip2/sapiens_flix/sapiens_unittest.py +16 -0
- flix_clip2/sapiens_flix/vis_depth.py +300 -0
- flix_clip2/sapiens_flix/vis_normal.py +235 -0
- flix_clip2/sapiens_flix/vis_pose.py +459 -0
- flix_clip2/sapiens_flix/vis_seg.py +271 -0
- flix_clip2/sapiens_flix/worker_pool.py +93 -0
- flix_clip2/sapiens_unittest.py +16 -0
- flix_clip2/temp_move_list.py +65 -0
- flix_clip2/unit_test_api.py +58 -0
- flix_clip2/unit_test_api_outfit.py +46 -0
- flix_clip2/unit_test_benchmark.py +77 -0
- flix_clip2/unit_test_biref.py +15 -0
- flix_clip2/unit_test_dis.py +15 -0
- flix_clip2/unit_test_dissam2birefmatte_dict.py +81 -0
- flix_clip2/unit_test_dissam2birefsapiensmatte_dict.py +84 -0
- flix_clip2/unit_test_matteformer.py +22 -0
- flix_clip2/unit_test_pipeline.py +112 -0
- flix_clip2/unit_test_sam2.py +23 -0
- flix_clip2-0.0.1.dist-info/METADATA +14 -0
- flix_clip2-0.0.1.dist-info/RECORD +132 -0
- flix_clip2-0.0.1.dist-info/WHEEL +5 -0
- flix_clip2-0.0.1.dist-info/top_level.txt +1 -0
flix_clip2/__init__.py
ADDED
flix_clip2/api.py
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import json
|
|
3
|
+
import cv2
|
|
4
|
+
import numpy as np
|
|
5
|
+
from time import time
|
|
6
|
+
from termcolor import colored
|
|
7
|
+
import shutil
|
|
8
|
+
|
|
9
|
+
from dis_flix.api import Bolt as Bolt_dis
|
|
10
|
+
from matteformer.api import Bolt as Bolt_matteformer
|
|
11
|
+
from objdetect_pytorch import Bolt as Bolt_obj
|
|
12
|
+
|
|
13
|
+
UPDATE = "25 Oct 2023"
|
|
14
|
+
|
|
15
|
+
class Bolt:
|
|
16
|
+
def __init__(self, init_dict_main):
|
|
17
|
+
self.flag_detect = init_dict_main['detect']
|
|
18
|
+
self.flag_fine = init_dict_main['fine_segmentation']
|
|
19
|
+
self.single_object = True #init_dict_main['single_object']
|
|
20
|
+
self.gpu_id = init_dict_main['gpu_id']
|
|
21
|
+
self.temp_dir = init_dict_main['temp_dir']
|
|
22
|
+
self.house_keeping = init_dict_main['house_keeping']
|
|
23
|
+
|
|
24
|
+
os.makedirs(self.temp_dir, exist_ok=True)
|
|
25
|
+
|
|
26
|
+
# ------------ Intro ------------
|
|
27
|
+
bolt_names = ["DIS"]
|
|
28
|
+
if self.flag_fine:
|
|
29
|
+
bolt_names.append("Matteformer")
|
|
30
|
+
if self.flag_detect:
|
|
31
|
+
bolt_names.append("Object Detection")
|
|
32
|
+
msg = "Bolt (%s)"%(" + ".join(bolt_names))
|
|
33
|
+
msg = msg + " | Update: %s"%(UPDATE)
|
|
34
|
+
print_special(msg,color="yellow",line_char="=",n=100)
|
|
35
|
+
|
|
36
|
+
# ------------ Bolt DIS------------
|
|
37
|
+
init_dict = {'model_path':init_dict_main['coarse_model_path'],'input_size':init_dict_main['dis_input_size'],'gpu_id':self.gpu_id}
|
|
38
|
+
self.bolt_dis = Bolt_dis(init_dict)
|
|
39
|
+
print(colored('DIS Bolt Initiallized','yellow'))
|
|
40
|
+
print(colored("-"*20, "yellow"))
|
|
41
|
+
|
|
42
|
+
# ------------ Bolt Matteformer------------
|
|
43
|
+
if self.flag_fine:
|
|
44
|
+
# load config
|
|
45
|
+
# config_path = init_dict_main['matte_config_path']
|
|
46
|
+
# with open(config_path, "r") as f:
|
|
47
|
+
# config = json.load(f)
|
|
48
|
+
# Initialize Bolt
|
|
49
|
+
config=init_dict_main['matte_config_dict']
|
|
50
|
+
config["gpu_id"] = self.gpu_id
|
|
51
|
+
self.bolt_matte = Bolt_matteformer(config)
|
|
52
|
+
print(colored('Mattefomer Bolt Initiallized (new)','yellow'))
|
|
53
|
+
print(colored("-"*20, "yellow"))
|
|
54
|
+
|
|
55
|
+
# ------------ Bolt Object Detection------------
|
|
56
|
+
if self.flag_detect:
|
|
57
|
+
gpu_id = self.gpu_id
|
|
58
|
+
assert os.path.isfile(init_dict_main['object_detect_model_path']), "Object detection model not found: %s"%(init_dict_main['object_detect_model_path'])
|
|
59
|
+
checkpoint_dict = {
|
|
60
|
+
"obj_model": init_dict_main['object_detect_model_path'],
|
|
61
|
+
"category_count": 2
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
self.obj = Bolt_obj(checkpoint_dict, gpu_id)
|
|
65
|
+
print(colored('Object Detection Bolt Initiallized','yellow'))
|
|
66
|
+
print(colored("-"*20, "yellow"))
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def run(self, input_dict):
|
|
70
|
+
try:
|
|
71
|
+
tic_all = time()
|
|
72
|
+
image_path = input_dict['image_path']
|
|
73
|
+
temp_path = os.path.join(self.temp_dir, str(time()))
|
|
74
|
+
os.makedirs(temp_path, exist_ok=True)
|
|
75
|
+
# ------------ Object Detection ------------
|
|
76
|
+
if self.flag_detect:
|
|
77
|
+
tic = time()
|
|
78
|
+
detect_path = os.path.join(temp_path, "detect.jpg")
|
|
79
|
+
input_dict_obj = {
|
|
80
|
+
"filepath": image_path,
|
|
81
|
+
"setid": 'sku_id',
|
|
82
|
+
"out_dir": ""
|
|
83
|
+
}
|
|
84
|
+
flag, crop_img_list, detection_box_list = self.obj.run(input_dict_obj)
|
|
85
|
+
if flag:
|
|
86
|
+
if self.single_object:
|
|
87
|
+
cv2.imwrite(detect_path, crop_img_list[0])
|
|
88
|
+
else:
|
|
89
|
+
err_msg = "Detection FAILED!!!!"
|
|
90
|
+
raise Exception(err_msg)
|
|
91
|
+
toc = time()
|
|
92
|
+
print(colored('Time (Detect): %0.2f'%(toc-tic),'yellow'))
|
|
93
|
+
print(colored("-"*20, "yellow"))
|
|
94
|
+
|
|
95
|
+
# ------------ DIS ------------
|
|
96
|
+
tic = time()
|
|
97
|
+
if self.flag_detect:
|
|
98
|
+
image_path = detect_path
|
|
99
|
+
result_detect_path = os.path.join(temp_path, "result.png")
|
|
100
|
+
else:
|
|
101
|
+
result_detect_path = input_dict['result_path'] # final result
|
|
102
|
+
|
|
103
|
+
if self.flag_fine:
|
|
104
|
+
segment_path = os.path.join(temp_path, "coarse.png")
|
|
105
|
+
fine_path = result_detect_path
|
|
106
|
+
else:
|
|
107
|
+
segment_path = result_detect_path
|
|
108
|
+
|
|
109
|
+
input_dict_dis = {'image_path': image_path,'result_path': segment_path}
|
|
110
|
+
os.makedirs(os.path.dirname(input_dict_dis["result_path"]), exist_ok=True)
|
|
111
|
+
out_dict_dis, status = self.bolt_dis.run(input_dict_dis)
|
|
112
|
+
toc = time()
|
|
113
|
+
print(colored('Time (Coarse): %0.2f'%(toc-tic),'yellow'))
|
|
114
|
+
if status:
|
|
115
|
+
if self.flag_fine:
|
|
116
|
+
assert os.path.isfile(out_dict_dis['result_path']), "Coarse segmentation not found: %s"%(out_dict_dis['result_path'])
|
|
117
|
+
tic_matte = time()
|
|
118
|
+
# ------------ Infer (fine)------------
|
|
119
|
+
# Create input dictionary
|
|
120
|
+
input_dict_fine = {"image_path": image_path,
|
|
121
|
+
"segment_path": segment_path,
|
|
122
|
+
"output_path": fine_path}
|
|
123
|
+
os.makedirs(os.path.dirname(input_dict_fine["output_path"]), exist_ok=True)
|
|
124
|
+
# Run Bolt
|
|
125
|
+
self.bolt_matte.run(input_dict_fine)
|
|
126
|
+
|
|
127
|
+
toc_matte = time()
|
|
128
|
+
print(colored('Time (Matte): %0.2f'%(toc_matte-tic_matte),'yellow'))
|
|
129
|
+
else:
|
|
130
|
+
err_msg = out_dict_dis['error_status']
|
|
131
|
+
raise Exception(err_msg)
|
|
132
|
+
|
|
133
|
+
# ------------ Construct Original Mask ------------
|
|
134
|
+
if self.flag_detect:
|
|
135
|
+
org = cv2.imread(input_dict['image_path'], 0)
|
|
136
|
+
clip_detect = cv2.imread(result_detect_path, 0)
|
|
137
|
+
canvas = self.unpad_result(org, clip_detect, detection_box_list)
|
|
138
|
+
os.makedirs(os.path.dirname(input_dict['result_path']), exist_ok=True)
|
|
139
|
+
cv2.imwrite(input_dict['result_path'], canvas)
|
|
140
|
+
|
|
141
|
+
# ------------save coarse ------------
|
|
142
|
+
if "coarse_path" in input_dict:
|
|
143
|
+
coarse_path = input_dict['coarse_path']
|
|
144
|
+
clip_detect = cv2.imread(segment_path, 0)
|
|
145
|
+
canvas = self.unpad_result(org, clip_detect, detection_box_list)
|
|
146
|
+
os.makedirs(os.path.dirname(coarse_path), exist_ok=True)
|
|
147
|
+
cv2.imwrite(coarse_path, canvas)
|
|
148
|
+
print(colored("Coarse saved: %s"%(coarse_path),"cyan","on_yellow"))
|
|
149
|
+
# else: result is saved as required already
|
|
150
|
+
else:
|
|
151
|
+
# ------------save coarse ------------
|
|
152
|
+
if "coarse_path" in input_dict:
|
|
153
|
+
coarse_path = input_dict['coarse_path']
|
|
154
|
+
os.makedirs(os.path.dirname(coarse_path), exist_ok=True)
|
|
155
|
+
shutil.copyfile(segment_path, coarse_path)
|
|
156
|
+
|
|
157
|
+
# ------------ House Keeping ------------
|
|
158
|
+
if self.house_keeping:
|
|
159
|
+
shutil.rmtree(temp_path)
|
|
160
|
+
print(colored("House keeping done: %s"%temp_path,"blue"))
|
|
161
|
+
|
|
162
|
+
status = True
|
|
163
|
+
toc_all = time()
|
|
164
|
+
print(colored('Time (Total): %0.2f'%(toc_all-tic_all),'cyan'))
|
|
165
|
+
print(colored("-"*100, "cyan"))
|
|
166
|
+
except Exception as e:
|
|
167
|
+
err_msg = str(e)
|
|
168
|
+
status = False
|
|
169
|
+
print(colored(err_msg, "red"))
|
|
170
|
+
import traceback
|
|
171
|
+
traceback.print_exc()
|
|
172
|
+
|
|
173
|
+
return status
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def unpad_result(self, org, clip_detect, detection_box_list):
|
|
177
|
+
canv = np.zeros_like(org)
|
|
178
|
+
left, top, right, bottom = detection_box_list[0]['xmin'], detection_box_list[0]['ymin'], detection_box_list[0]['xmax'], detection_box_list[0]['ymax']
|
|
179
|
+
canv[top:bottom, left:right] = clip_detect
|
|
180
|
+
return canv
|
|
181
|
+
|
|
182
|
+
def print_special(msg,color="yellow",line_char="-",n=100):
|
|
183
|
+
schar = " "
|
|
184
|
+
nfill = (n-len(msg)+2)//2 # number of fillers
|
|
185
|
+
msg = schar*nfill+" "+msg+" "+schar*nfill
|
|
186
|
+
print(colored(line_char*n,color=color))
|
|
187
|
+
print(colored(msg,color=color))
|
|
188
|
+
print(colored(line_char*n,color=color))
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
if __name__ == "__main__":
|
|
192
|
+
init_dict_main = {}
|
|
193
|
+
init_dict_main['detect'] = True
|
|
194
|
+
init_dict_main['object_detect_model_path'] = '/datarive1/projects/python/beer/DeploymentMaster/flix_clip/packages/object_detect_package/step_075000.pth'
|
|
195
|
+
init_dict_main['coarse_model_path'] = '/datarive1/projects/python/DIS/saved_models/IS-Net/test_v3.pth'
|
|
196
|
+
init_dict_main['dis_input_size'] = [1024,1024]
|
|
197
|
+
init_dict_main['gpu_id'] = '0'
|
|
198
|
+
init_dict_main['fine_segmentation'] = True
|
|
199
|
+
init_dict_main['matte_config_path'] = '/datarive1/projects/python/beer/DeploymentMaster/flix_clip/packages/matteformer_package/infer_config.json'
|
|
200
|
+
init_dict_main['temp_dir'] = '/datarive1/projects/python/beer/DeploymentMaster/flix_clip/temp'
|
|
201
|
+
init_dict_main['house_keeping'] = True
|
|
202
|
+
|
|
203
|
+
# ------------ Bolt ------------
|
|
204
|
+
bolt = Bolt(init_dict_main)
|
|
205
|
+
# ------------ Infer ------------
|
|
206
|
+
input_dict = {}
|
|
207
|
+
input_dict['image_path'] = 'sample/images/356957_1_edit.jpg'
|
|
208
|
+
input_dict['result_path'] = 'sample/results/356957_1_edit.png'
|
|
209
|
+
# optional (only with fine_segmentation:true)
|
|
210
|
+
input_dict['coarse_path'] = 'sample/coarses/356957_1_edit.png'
|
|
211
|
+
|
|
212
|
+
flag = bolt.run(input_dict)
|
|
213
|
+
|
|
214
|
+
print(flag)
|