flix-object-detection-wrapper 0.0.1__tar.gz

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.
@@ -0,0 +1,14 @@
1
+ Metadata-Version: 2.4
2
+ Name: flix_object_detection_wrapper
3
+ Version: 0.0.1
4
+ Summary: object_detection_wrapper
5
+ Author: sanchi
6
+ Author-email: sanchi@flixstock.com
7
+ Classifier: Programming Language :: Python :: 3.11
8
+ Dynamic: author
9
+ Dynamic: author-email
10
+ Dynamic: classifier
11
+ Dynamic: description
12
+ Dynamic: summary
13
+
14
+ object_detection_wrapper
@@ -0,0 +1,31 @@
1
+ import os
2
+
3
+
4
+ class objDetConfig:
5
+ def __init__(self, checkpointPath):
6
+ # checkpoints directory must be supplied by the caller (e.g. the unit test)
7
+ self.checkpointsPath = checkpointPath
8
+
9
+ def getGarmentObjDetectConfig(self):
10
+ return {
11
+ "obj_model": os.path.join(self.checkpointsPath, "outfitClipper", "AllGarmentOutfit_step_100000_v4.pth"),
12
+ "category_count": 2,
13
+ }
14
+
15
+ def getModelObjDetectConfig(self):
16
+ return {
17
+ "obj_model": os.path.join(self.checkpointsPath, "modelClipper", "GPDHuman_v4_step_100000.pth"),
18
+ "category_count": 2,
19
+ }
20
+
21
+ def getShoesObjDetectConfig(self):
22
+ return {
23
+ "obj_model": os.path.join(self.checkpointsPath, "shoesClipper", "Model_shoe_step_032500_v4.pth"),
24
+ "category_count": 2,
25
+ }
26
+
27
+ def getHairObjDetectConfig(self):
28
+ return {
29
+ "obj_model": os.path.join(self.checkpointsPath, "hairClipper", "hair_v4_step_057500.pth"),
30
+ "category_count": 2,
31
+ }
@@ -0,0 +1,110 @@
1
+ import os
2
+
3
+ import cv2
4
+ import numpy as np
5
+ from termcolor import colored
6
+
7
+ # try:
8
+ # from objdetect_pytorch import Bolt
9
+ # except ImportError:
10
+ # from .objdetect_pytorch import Bolt
11
+
12
+ from flix_clip2.objdetect_pytorch import Bolt
13
+
14
+ def initialize_detector(checkpoint_dict, gpu="0"):
15
+ """Build the object-detector object once, so it can be reused across many images.
16
+
17
+ checkpoint_dict: {"obj_model": <path.pth>, "category_count": <int>} (from any *ObjDetectConfig).
18
+ gpu: gpu id as a string; "-1" for CPU.
19
+ Returns the initialized detector (Bolt) object.
20
+ """
21
+ assert os.path.isfile(checkpoint_dict["obj_model"]), \
22
+ "Checkpoint not found: %s" % checkpoint_dict["obj_model"]
23
+ detector = Bolt(checkpoint_dict, gpu)
24
+ return detector
25
+
26
+
27
+ def pad_bbox(box, img_w, img_h, height_percent=0.20, width_percent=0.20):
28
+ """Expand a detection box by a fraction of its own size, clamped to image bounds.
29
+
30
+ height_percent: fraction of the bbox height added to BOTH top and bottom.
31
+ width_percent: fraction of the bbox width added to BOTH left and right.
32
+ Returns (xmin, ymin, xmax, ymax).
33
+ """
34
+ bbox_w = box['xmax'] - box['xmin']
35
+ bbox_h = box['ymax'] - box['ymin']
36
+ pad_x = int(width_percent * bbox_w)
37
+ pad_y = int(height_percent * bbox_h)
38
+ xmin = max(0, box['xmin'] - pad_x)
39
+ ymin = max(0, box['ymin'] - pad_y)
40
+ xmax = min(img_w, box['xmax'] + pad_x)
41
+ ymax = min(img_h, box['ymax'] + pad_y)
42
+ return xmin, ymin, xmax, ymax
43
+
44
+
45
+ def run_inference(detector, input_dict):
46
+ """Run detection on one image, save all outputs, and return the first (top) bbox.
47
+
48
+ This is detector-agnostic — identical whether the object came from the garment,
49
+ model, shoes, ... config. Only the input_dict carries per-call parameters, so new
50
+ parameters can be added later without changing the signature.
51
+
52
+ input_dict keys:
53
+ image_path (required) path to the image to detect on
54
+ output_dir (required) directory to write the crops/masks into
55
+ height_percent (optional, default 0.20) padding fraction, top+bottom
56
+ width_percent (optional, default 0.20) padding fraction, left+right
57
+
58
+ For each detection i it saves:
59
+ <i>.jpg crop exactly as returned by detector.run
60
+ <i>_pad.jpg crop expanded by (height_percent, width_percent)
61
+ <i>_mask.jpg full-size grayscale mask: padded region = 255, background = 0
62
+
63
+ Returns the first bbox dict {'xmin','ymin','xmax','ymax','score','label'}, or None
64
+ if nothing was detected.
65
+ """
66
+ image_path = input_dict['image_path']
67
+ output_dir = input_dict['output_dir']
68
+ height_percent = input_dict.get('height_percent', 0.20)
69
+ width_percent = input_dict.get('width_percent', 0.20)
70
+
71
+ assert os.path.isfile(image_path), "Image not found: %s" % image_path
72
+ os.makedirs(output_dir, exist_ok=True)
73
+
74
+ # ------------ inference ------------
75
+ flag, crop_img_list, detection_box_list = detector.run({"filepath": image_path}, MARGIN=0)
76
+ if not flag or len(detection_box_list) == 0:
77
+ print(colored("Detection FAILED: %s" % image_path, "red"))
78
+ return None
79
+
80
+ # original image size — masks are full-size grayscale of the detection input
81
+ orig_img = cv2.imread(image_path, 1)
82
+ orig_h, orig_w = orig_img.shape[:2]
83
+
84
+ # ------------ save all outputs ------------
85
+ for i, box in enumerate(detection_box_list):
86
+ print(box['xmin'], box['ymin'], box['xmax'], box['ymax'], box['label'], box['score'])
87
+
88
+ # 1. crop exactly as returned by detector.run
89
+ crop_path = os.path.join(output_dir, "%d.jpg" % i)
90
+ cv2.imwrite(crop_path, crop_img_list[i])
91
+ print(colored(" crop saved: %s" % crop_path, "cyan"))
92
+
93
+ # padded box (clamped to image bounds)
94
+ px_min, py_min, px_max, py_max = pad_bbox(box, orig_w, orig_h, height_percent, width_percent)
95
+
96
+ # 2. padded crop
97
+ pad_crop = orig_img[py_min:py_max, px_min:px_max]
98
+ pad_crop_path = os.path.join(output_dir, "%d_pad.jpg" % i)
99
+ cv2.imwrite(pad_crop_path, pad_crop)
100
+ print(colored(" padded crop saved: %s" % pad_crop_path, "cyan"))
101
+
102
+ # 3. full-size grayscale mask of the PADDED region: white (255) inside, black (0) outside
103
+ mask = np.zeros((orig_h, orig_w), dtype=np.uint8)
104
+ mask[py_min:py_max, px_min:px_max] = 255
105
+ mask_path = os.path.join(output_dir, "%d_mask.jpg" % i)
106
+ cv2.imwrite(mask_path, mask)
107
+ print(colored(" mask saved: %s" % mask_path, "cyan"))
108
+
109
+ # returns the first (top-confidence) bbox
110
+ return detection_box_list[0]
@@ -0,0 +1,16 @@
1
+ import setuptools
2
+
3
+ setuptools.setup(
4
+ name="flix_object_detection_wrapper",
5
+ version="0.0.1",
6
+ author="sanchi",
7
+ author_email="sanchi@flixstock.com",
8
+ description="object_detection_wrapper",
9
+ long_description='object_detection_wrapper',
10
+ packages=setuptools.find_packages(),
11
+ include_package_data=True,
12
+ classifiers=[
13
+ "Programming Language :: Python :: 3.11",
14
+ # "Operating System :: Ubuntu 20.04",
15
+ ],
16
+ )
@@ -0,0 +1,42 @@
1
+ import os
2
+
3
+ from termcolor import colored
4
+ from flix_object_detection_wrapper.main import initialize_detector, run_inference
5
+ from flix_object_detection_wrapper.config import objDetConfig
6
+
7
+ # try:
8
+ # from main import initialize_detector, run_inference
9
+ # from config import objDetConfig
10
+ # except ImportError:
11
+ # from .main import initialize_detector, run_inference
12
+ # from .config import objDetConfig
13
+
14
+ if __name__ == "__main__":
15
+ gpu = "0" # use "-1" for CPU
16
+
17
+ # module-local checkpoints, so object_det/ works wherever it is copied
18
+ checkpoints_dir = "/home/ubuntu/projects/python/sanchi/clippers/object_detection_wrapper/checkpoints"
19
+ img_path = "/home/ubuntu/projects/python/sanchi/clippers/object_detection_wrapper/data/test_object_detection/input/aleksandra_18998.jpg"
20
+ out_dir = "/home/ubuntu/projects/python/sanchi/clippers/object_detection_wrapper/data/test_object_detection/output/aleksandra_18998_2"
21
+
22
+ # ------------ config ------------
23
+ # reads {"obj_model": <path.pth>, "category_count": <int>}; swap the getter to use another detector
24
+ checkpoint_dict = objDetConfig(checkpoints_dir).getGarmentObjDetectConfig()
25
+ print(colored("Config: %s" % checkpoint_dict, "yellow"))
26
+ print(colored("Image : %s" % img_path, "yellow"))
27
+
28
+ # ------------ initialize once (reuse across many images) ------------
29
+ detector = initialize_detector(checkpoint_dict, gpu)
30
+
31
+ # ------------ infer ------------
32
+ # per-call parameters go in input_dict, so new ones can be added later without signature changes
33
+ input_dict = {
34
+ "image_path": img_path,
35
+ "output_dir": out_dir,
36
+ "height_percent": 0.01,
37
+ "width_percent": 0.01,
38
+ }
39
+ first_bbox = run_inference(detector, input_dict)
40
+
41
+ assert first_bbox is not None, "Detection FAILED — no boxes returned"
42
+ print(colored("PASS: first bbox = %s" % first_bbox, "green"))
@@ -0,0 +1,43 @@
1
+ import os
2
+
3
+ from termcolor import colored
4
+
5
+ # try:
6
+ # from main import initialize_detector, run_inference
7
+ # from config import objDetConfig
8
+ # except ImportError:
9
+ # from .main import initialize_detector, run_inference
10
+ # from .config import objDetConfig
11
+
12
+ from flix_object_detection_wrapper.main import initialize_detector, run_inference
13
+ from flix_object_detection_wrapper.config import objDetConfig
14
+
15
+ if __name__ == "__main__":
16
+ gpu = "0" # use "-1" for CPU
17
+
18
+ # module-local checkpoints, so object_det/ works wherever it is copied
19
+ checkpoints_dir = "/home/ubuntu/projects/python/sanchi/clippers/object_detection_wrapper/checkpoints"
20
+ img_path = "/home/ubuntu/projects/python/sanchi/clippers/object_detection_wrapper/data/test_object_detection/input/aleksandra_18998.jpg"
21
+ out_dir = "/home/ubuntu/projects/python/sanchi/clippers/object_detection_wrapper/data/test_object_detection/output/aleksandra_18998_hair"
22
+
23
+ # ------------ config ------------
24
+ # hair detector; only this line differs from the garment unit test
25
+ checkpoint_dict = objDetConfig(checkpoints_dir).getHairObjDetectConfig()
26
+ print(colored("Config: %s" % checkpoint_dict, "yellow"))
27
+ print(colored("Image : %s" % img_path, "yellow"))
28
+
29
+ # ------------ initialize once (reuse across many images) ------------
30
+ detector = initialize_detector(checkpoint_dict, gpu)
31
+
32
+ # ------------ infer ------------
33
+ # per-call parameters go in input_dict, so new ones can be added later without signature changes
34
+ input_dict = {
35
+ "image_path": img_path,
36
+ "output_dir": out_dir,
37
+ "height_percent": 0.20,
38
+ "width_percent": 0.20,
39
+ }
40
+ first_bbox = run_inference(detector, input_dict)
41
+
42
+ assert first_bbox is not None, "Detection FAILED — no boxes returned"
43
+ print(colored("PASS: first bbox = %s" % first_bbox, "green"))
@@ -0,0 +1,43 @@
1
+ import os
2
+
3
+ from termcolor import colored
4
+
5
+ # try:
6
+ # from main import initialize_detector, run_inference
7
+ # from config import objDetConfig
8
+ # except ImportError:
9
+ # from .main import initialize_detector, run_inference
10
+ # from .config import objDetConfig
11
+
12
+ from flix_object_detection_wrapper.main import initialize_detector, run_inference
13
+ from flix_object_detection_wrapper.config import objDetConfig
14
+
15
+ if __name__ == "__main__":
16
+ gpu = "0" # use "-1" for CPU
17
+
18
+ # module-local checkpoints, so object_det/ works wherever it is copied
19
+ checkpoints_dir = "/home/ubuntu/projects/python/sanchi/clippers/object_detection_wrapper/checkpoints"
20
+ img_path = "/home/ubuntu/projects/python/sanchi/clippers/object_detection_wrapper/data/test_object_detection/input/aleksandra_18998.jpg"
21
+ out_dir = "/home/ubuntu/projects/python/sanchi/clippers/object_detection_wrapper/data/test_object_detection/output/aleksandra_18998_model"
22
+
23
+ # ------------ config ------------
24
+ # model (human) detector; only this line differs from the garment unit test
25
+ checkpoint_dict = objDetConfig(checkpoints_dir).getModelObjDetectConfig()
26
+ print(colored("Config: %s" % checkpoint_dict, "yellow"))
27
+ print(colored("Image : %s" % img_path, "yellow"))
28
+
29
+ # ------------ initialize once (reuse across many images) ------------
30
+ detector = initialize_detector(checkpoint_dict, gpu)
31
+
32
+ # ------------ infer ------------
33
+ # per-call parameters go in input_dict, so new ones can be added later without signature changes
34
+ input_dict = {
35
+ "image_path": img_path,
36
+ "output_dir": out_dir,
37
+ "height_percent": 0.01,
38
+ "width_percent": 0.01,
39
+ }
40
+ first_bbox = run_inference(detector, input_dict)
41
+
42
+ assert first_bbox is not None, "Detection FAILED — no boxes returned"
43
+ print(colored("PASS: first bbox = %s" % first_bbox, "green"))
@@ -0,0 +1,43 @@
1
+ import os
2
+
3
+ from termcolor import colored
4
+
5
+ # try:
6
+ # from main import initialize_detector, run_inference
7
+ # from config import objDetConfig
8
+ # except ImportError:
9
+ # from .main import initialize_detector, run_inference
10
+ # from .config import objDetConfig
11
+
12
+ from flix_object_detection_wrapper.main import initialize_detector, run_inference
13
+ from flix_object_detection_wrapper.config import objDetConfig
14
+
15
+ if __name__ == "__main__":
16
+ gpu = "0" # use "-1" for CPU
17
+
18
+ # module-local checkpoints, so object_det/ works wherever it is copied
19
+ checkpoints_dir = "/home/ubuntu/projects/python/sanchi/clippers/object_detection_wrapper/checkpoints"
20
+ img_path = "/home/ubuntu/projects/python/sanchi/clippers/object_detection_wrapper/data/test_object_detection/input/aleksandra_18998.jpg"
21
+ out_dir = "/home/ubuntu/projects/python/sanchi/clippers/object_detection_wrapper/data/test_object_detection/output/aleksandra_18998_shoes"
22
+
23
+ # ------------ config ------------
24
+ # shoes detector; only this line differs from the garment unit test
25
+ checkpoint_dict = objDetConfig(checkpoints_dir).getShoesObjDetectConfig()
26
+ print(colored("Config: %s" % checkpoint_dict, "yellow"))
27
+ print(colored("Image : %s" % img_path, "yellow"))
28
+
29
+ # ------------ initialize once (reuse across many images) ------------
30
+ detector = initialize_detector(checkpoint_dict, gpu)
31
+
32
+ # ------------ infer ------------
33
+ # per-call parameters go in input_dict, so new ones can be added later without signature changes
34
+ input_dict = {
35
+ "image_path": img_path,
36
+ "output_dir": out_dir,
37
+ "height_percent": 0.01,
38
+ "width_percent": 0.01,
39
+ }
40
+ first_bbox = run_inference(detector, input_dict)
41
+
42
+ assert first_bbox is not None, "Detection FAILED — no boxes returned"
43
+ print(colored("PASS: first bbox = %s" % first_bbox, "green"))
@@ -0,0 +1,14 @@
1
+ Metadata-Version: 2.4
2
+ Name: flix_object_detection_wrapper
3
+ Version: 0.0.1
4
+ Summary: object_detection_wrapper
5
+ Author: sanchi
6
+ Author-email: sanchi@flixstock.com
7
+ Classifier: Programming Language :: Python :: 3.11
8
+ Dynamic: author
9
+ Dynamic: author-email
10
+ Dynamic: classifier
11
+ Dynamic: description
12
+ Dynamic: summary
13
+
14
+ object_detection_wrapper
@@ -0,0 +1,13 @@
1
+ setup.py
2
+ flix_object_detection_wrapper/__init__.py
3
+ flix_object_detection_wrapper/config.py
4
+ flix_object_detection_wrapper/main.py
5
+ flix_object_detection_wrapper/setup.py
6
+ flix_object_detection_wrapper/unit_test.py
7
+ flix_object_detection_wrapper/unit_test_hair.py
8
+ flix_object_detection_wrapper/unit_test_model.py
9
+ flix_object_detection_wrapper/unit_test_shoes.py
10
+ flix_object_detection_wrapper.egg-info/PKG-INFO
11
+ flix_object_detection_wrapper.egg-info/SOURCES.txt
12
+ flix_object_detection_wrapper.egg-info/dependency_links.txt
13
+ flix_object_detection_wrapper.egg-info/top_level.txt
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,16 @@
1
+ import setuptools
2
+
3
+ setuptools.setup(
4
+ name="flix_object_detection_wrapper",
5
+ version="0.0.1",
6
+ author="sanchi",
7
+ author_email="sanchi@flixstock.com",
8
+ description="object_detection_wrapper",
9
+ long_description='object_detection_wrapper',
10
+ packages=setuptools.find_packages(),
11
+ include_package_data=True,
12
+ classifiers=[
13
+ "Programming Language :: Python :: 3.11",
14
+ # "Operating System :: Ubuntu 20.04",
15
+ ],
16
+ )