dora-sam2 0.0.0__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.
dora_sam2/__init__.py
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
import os
|
2
|
+
|
3
|
+
# Define the path to the README file relative to the package directory
|
4
|
+
readme_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "README.md")
|
5
|
+
|
6
|
+
# Read the content of the README file
|
7
|
+
try:
|
8
|
+
with open(readme_path, encoding="utf-8") as f:
|
9
|
+
__doc__ = f.read()
|
10
|
+
except FileNotFoundError:
|
11
|
+
__doc__ = "README file not found."
|
dora_sam2/__main__.py
ADDED
dora_sam2/main.py
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
import cv2
|
2
|
+
import numpy as np
|
3
|
+
import pyarrow as pa
|
4
|
+
import torch
|
5
|
+
from dora import Node
|
6
|
+
from PIL import Image
|
7
|
+
from sam2.sam2_image_predictor import SAM2ImagePredictor
|
8
|
+
|
9
|
+
predictor = SAM2ImagePredictor.from_pretrained("facebook/sam2-hiera-large")
|
10
|
+
|
11
|
+
|
12
|
+
def main():
|
13
|
+
pa.array([]) # initialize pyarrow array
|
14
|
+
node = Node()
|
15
|
+
frames = {}
|
16
|
+
for event in node:
|
17
|
+
event_type = event["type"]
|
18
|
+
|
19
|
+
if event_type == "INPUT":
|
20
|
+
event_id = event["id"]
|
21
|
+
|
22
|
+
if "image" in event_id:
|
23
|
+
storage = event["value"]
|
24
|
+
metadata = event["metadata"]
|
25
|
+
encoding = metadata["encoding"]
|
26
|
+
width = metadata["width"]
|
27
|
+
height = metadata["height"]
|
28
|
+
|
29
|
+
if (
|
30
|
+
encoding == "bgr8"
|
31
|
+
or encoding == "rgb8"
|
32
|
+
or encoding in ["jpeg", "jpg", "jpe", "bmp", "webp", "png"]
|
33
|
+
):
|
34
|
+
channels = 3
|
35
|
+
storage_type = np.uint8
|
36
|
+
else:
|
37
|
+
error = f"Unsupported image encoding: {encoding}"
|
38
|
+
raise RuntimeError(error)
|
39
|
+
|
40
|
+
if encoding == "bgr8":
|
41
|
+
frame = (
|
42
|
+
storage.to_numpy()
|
43
|
+
.astype(storage_type)
|
44
|
+
.reshape((height, width, channels))
|
45
|
+
)
|
46
|
+
frame = frame[:, :, ::-1] # OpenCV image (BGR to RGB)
|
47
|
+
elif encoding == "rgb8":
|
48
|
+
frame = (
|
49
|
+
storage.to_numpy()
|
50
|
+
.astype(storage_type)
|
51
|
+
.reshape((height, width, channels))
|
52
|
+
)
|
53
|
+
elif encoding in ["jpeg", "jpg", "jpe", "bmp", "webp", "png"]:
|
54
|
+
storage = storage.to_numpy()
|
55
|
+
frame = cv2.imdecode(storage, cv2.IMREAD_COLOR)
|
56
|
+
frame = frame[:, :, ::-1] # OpenCV image (BGR to RGB)
|
57
|
+
else:
|
58
|
+
raise RuntimeError(f"Unsupported image encoding: {encoding}")
|
59
|
+
image = Image.fromarray(frame)
|
60
|
+
frames[event_id] = image
|
61
|
+
|
62
|
+
elif "boxes2d" in event_id:
|
63
|
+
boxes2d = event["value"].to_numpy()
|
64
|
+
metadata = event["metadata"]
|
65
|
+
encoding = metadata["encoding"]
|
66
|
+
if encoding != "xyxy":
|
67
|
+
raise RuntimeError(f"Unsupported boxes2d encoding: {encoding}")
|
68
|
+
|
69
|
+
image_id = metadata["image_id"]
|
70
|
+
with torch.inference_mode(), torch.autocast(
|
71
|
+
"cuda",
|
72
|
+
dtype=torch.bfloat16,
|
73
|
+
):
|
74
|
+
predictor.set_image(frames[image_id])
|
75
|
+
masks, _, _ = predictor.predict(box=boxes2d)
|
76
|
+
masks = masks[0]
|
77
|
+
## Mask to 3 channel image
|
78
|
+
|
79
|
+
node.send_output(
|
80
|
+
"masks",
|
81
|
+
pa.array(masks.ravel()),
|
82
|
+
metadata={
|
83
|
+
"image_id": image_id,
|
84
|
+
"width": frames[image_id].width,
|
85
|
+
"height": frames[image_id].height,
|
86
|
+
},
|
87
|
+
)
|
88
|
+
|
89
|
+
elif event_type == "ERROR":
|
90
|
+
print("Event Error:" + event["error"])
|
91
|
+
|
92
|
+
|
93
|
+
if __name__ == "__main__":
|
94
|
+
main()
|
@@ -0,0 +1,53 @@
|
|
1
|
+
Metadata-Version: 2.2
|
2
|
+
Name: dora-sam2
|
3
|
+
Version: 0.0.0
|
4
|
+
Summary: dora-sam2
|
5
|
+
Author-email: Your Name <email@email.com>
|
6
|
+
License: MIT
|
7
|
+
Requires-Python: >=3.10
|
8
|
+
Description-Content-Type: text/markdown
|
9
|
+
Requires-Dist: dora-rs>=0.3.6
|
10
|
+
Requires-Dist: huggingface-hub>=0.29.0
|
11
|
+
Requires-Dist: opencv-python>=4.11.0.86
|
12
|
+
Requires-Dist: sam2>=1.1.0
|
13
|
+
|
14
|
+
# dora-sam2
|
15
|
+
|
16
|
+
> [!WARNING]
|
17
|
+
> SAM2 requires Nvidia GPU to be able to run.
|
18
|
+
|
19
|
+
## Getting started
|
20
|
+
|
21
|
+
- Install it with pip:
|
22
|
+
|
23
|
+
```bash
|
24
|
+
pip install -e .
|
25
|
+
```
|
26
|
+
|
27
|
+
## Contribution Guide
|
28
|
+
|
29
|
+
- Format with [ruff](https://docs.astral.sh/ruff/):
|
30
|
+
|
31
|
+
```bash
|
32
|
+
ruff check . --fix
|
33
|
+
```
|
34
|
+
|
35
|
+
- Lint with ruff:
|
36
|
+
|
37
|
+
```bash
|
38
|
+
ruff check .
|
39
|
+
```
|
40
|
+
|
41
|
+
- Test with [pytest](https://github.com/pytest-dev/pytest)
|
42
|
+
|
43
|
+
```bash
|
44
|
+
pytest . # Test
|
45
|
+
```
|
46
|
+
|
47
|
+
## YAML Specification
|
48
|
+
|
49
|
+
## Examples
|
50
|
+
|
51
|
+
## License
|
52
|
+
|
53
|
+
dora-sam2's code are released under the MIT License
|
@@ -0,0 +1,8 @@
|
|
1
|
+
dora_sam2/__init__.py,sha256=HuSK3dnyI9Pb5QAuaKFwQQ3J5SIZnLcKHPJO0norGzc,353
|
2
|
+
dora_sam2/__main__.py,sha256=Vdhw8YA1K3wPMlbJQYL5WqvRzAKVeZ16mZQFO9VRmCo,62
|
3
|
+
dora_sam2/main.py,sha256=7z7kZyI25eOSqnbwUEL8bGXOUnoa6UQ_AXThrjHUAB0,3380
|
4
|
+
dora_sam2-0.0.0.dist-info/METADATA,sha256=If_OUNOaxv2wAK8BIze_gb07Bh9SpeMiMksV3N0a4qY,819
|
5
|
+
dora_sam2-0.0.0.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
|
6
|
+
dora_sam2-0.0.0.dist-info/entry_points.txt,sha256=eObMaDQauVA_sv4J6fsNjn8V-8syGJK7mO-LrsBu1aA,50
|
7
|
+
dora_sam2-0.0.0.dist-info/top_level.txt,sha256=IgKcOITGe2Nlyc79J6dwh3dcp3Wsf-IipIy-1h9GcPE,10
|
8
|
+
dora_sam2-0.0.0.dist-info/RECORD,,
|
@@ -0,0 +1 @@
|
|
1
|
+
dora_sam2
|