-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update ultralytics models (take two) (#613)
Co-authored-by: Helio Machado <[email protected]>
- Loading branch information
1 parent
894c13d
commit 224f8a6
Showing
19 changed files
with
830 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from io import BytesIO | ||
|
||
from PIL import Image | ||
from ultralytics import YOLO | ||
|
||
from datachain import C, DataChain, File | ||
from datachain.model.ultralytics import YoloBBoxes | ||
|
||
|
||
def process_bboxes(yolo: YOLO, file: File) -> YoloBBoxes: | ||
results = yolo(Image.open(BytesIO(file.read()))) | ||
return YoloBBoxes.from_results(results) | ||
|
||
|
||
( | ||
DataChain.from_storage("gs://datachain-demo/openimages-v6-test-jsonpairs/") | ||
.filter(C("file.path").glob("*.jpg")) | ||
.limit(20) | ||
.setup(yolo=lambda: YOLO("yolo11n.pt")) | ||
.map(boxes=process_bboxes) | ||
.show() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from io import BytesIO | ||
|
||
from PIL import Image | ||
from ultralytics import YOLO | ||
|
||
from datachain import C, DataChain, File | ||
from datachain.model.ultralytics import YoloPoses | ||
|
||
|
||
def process_poses(yolo: YOLO, file: File) -> YoloPoses: | ||
results = yolo(Image.open(BytesIO(file.read()))) | ||
return YoloPoses.from_results(results) | ||
|
||
|
||
( | ||
DataChain.from_storage("gs://datachain-demo/openimages-v6-test-jsonpairs/") | ||
.filter(C("file.path").glob("*.jpg")) | ||
.limit(20) | ||
.setup(yolo=lambda: YOLO("yolo11n-pose.pt")) | ||
.map(poses=process_poses) | ||
.show() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from io import BytesIO | ||
|
||
from PIL import Image | ||
from ultralytics import YOLO | ||
|
||
from datachain import C, DataChain, File | ||
from datachain.model.ultralytics import YoloSegments | ||
|
||
|
||
def process_segments(yolo: YOLO, file: File) -> YoloSegments: | ||
results = yolo(Image.open(BytesIO(file.read()))) | ||
return YoloSegments.from_results(results) | ||
|
||
|
||
( | ||
DataChain.from_storage("gs://datachain-demo/openimages-v6-test-jsonpairs/") | ||
.filter(C("file.path").glob("*.jpg")) | ||
.limit(20) | ||
.setup(yolo=lambda: YOLO("yolo11n-seg.pt")) | ||
.map(segments=process_segments) | ||
.show() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from . import ultralytics | ||
from .bbox import BBox, OBBox | ||
from .pose import Pose, Pose3D | ||
from .segment import Segment | ||
|
||
__all__ = ["BBox", "OBBox", "Pose", "Pose3D", "Segment", "ultralytics"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
from pydantic import Field | ||
|
||
from datachain.lib.data_model import DataModel | ||
|
||
|
||
class BBox(DataModel): | ||
""" | ||
A data model for representing bounding box. | ||
Attributes: | ||
title (str): The title of the bounding box. | ||
coords (list[int]): The coordinates of the bounding box. | ||
The bounding box is defined by two points: | ||
- (x1, y1): The top-left corner of the box. | ||
- (x2, y2): The bottom-right corner of the box. | ||
""" | ||
|
||
title: str = Field(default="") | ||
coords: list[int] = Field(default=None) | ||
|
||
@staticmethod | ||
def from_list(coords: list[float], title: str = "") -> "BBox": | ||
assert len(coords) == 4, "Bounding box must be a list of 4 coordinates." | ||
assert all( | ||
isinstance(value, (int, float)) for value in coords | ||
), "Bounding box coordinates must be floats or integers." | ||
return BBox( | ||
title=title, | ||
coords=[round(c) for c in coords], | ||
) | ||
|
||
@staticmethod | ||
def from_dict(coords: dict[str, float], title: str = "") -> "BBox": | ||
assert isinstance(coords, dict) and set(coords) == { | ||
"x1", | ||
"y1", | ||
"x2", | ||
"y2", | ||
}, "Bounding box must be a dictionary with keys 'x1', 'y1', 'x2' and 'y2'." | ||
return BBox.from_list( | ||
[coords["x1"], coords["y1"], coords["x2"], coords["y2"]], | ||
title=title, | ||
) | ||
|
||
|
||
class OBBox(DataModel): | ||
""" | ||
A data model for representing oriented bounding boxes. | ||
Attributes: | ||
title (str): The title of the oriented bounding box. | ||
coords (list[int]): The coordinates of the oriented bounding box. | ||
The oriented bounding box is defined by four points: | ||
- (x1, y1): The first corner of the box. | ||
- (x2, y2): The second corner of the box. | ||
- (x3, y3): The third corner of the box. | ||
- (x4, y4): The fourth corner of the box. | ||
""" | ||
|
||
title: str = Field(default="") | ||
coords: list[int] = Field(default=None) | ||
|
||
@staticmethod | ||
def from_list(coords: list[float], title: str = "") -> "OBBox": | ||
assert ( | ||
len(coords) == 8 | ||
), "Oriented bounding box must be a list of 8 coordinates." | ||
assert all( | ||
isinstance(value, (int, float)) for value in coords | ||
), "Oriented bounding box coordinates must be floats or integers." | ||
return OBBox( | ||
title=title, | ||
coords=[round(c) for c in coords], | ||
) | ||
|
||
@staticmethod | ||
def from_dict(coords: dict[str, float], title: str = "") -> "OBBox": | ||
assert isinstance(coords, dict) and set(coords) == { | ||
"x1", | ||
"y1", | ||
"x2", | ||
"y2", | ||
"x3", | ||
"y3", | ||
"x4", | ||
"y4", | ||
}, "Oriented bounding box must be a dictionary with coordinates." | ||
return OBBox.from_list( | ||
[ | ||
coords["x1"], | ||
coords["y1"], | ||
coords["x2"], | ||
coords["y2"], | ||
coords["x3"], | ||
coords["y3"], | ||
coords["x4"], | ||
coords["y4"], | ||
], | ||
title=title, | ||
) |
Oops, something went wrong.