Skip to content

Commit a132ff2

Browse files
Add stubs for pycocotools (#9086)
Co-authored-by: Alex Waygood <[email protected]>
1 parent ea2ccc3 commit a132ff2

File tree

5 files changed

+200
-0
lines changed

5 files changed

+200
-0
lines changed

stubs/pycocotools/METADATA.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
version = "2.0.*"
2+
3+
[tool.stubtest]
4+
ignore_missing_stub = false
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from typing_extensions import TypedDict
2+
3+
# Unused in this module, but imported in multiple submodules.
4+
class _EncodedRLE(TypedDict): # noqa: Y049
5+
size: list[int]
6+
counts: str | bytes
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
from _typeshed import Incomplete
2+
from collections.abc import Collection, Sequence
3+
from pathlib import Path
4+
from typing import Generic, TypeVar, overload
5+
from typing_extensions import Literal, TypeAlias, TypedDict
6+
7+
from . import _EncodedRLE
8+
9+
# TODO: Use numpy types when #5768 is resolved.
10+
# import numpy as np
11+
# import numpy.typing as npt
12+
13+
PYTHON_VERSION: Incomplete
14+
_NDArray: TypeAlias = Incomplete
15+
16+
class _Image(TypedDict):
17+
id: int
18+
width: int
19+
height: int
20+
file_name: str
21+
22+
_TPolygonSegmentation: TypeAlias = list[list[float]]
23+
24+
class _RLE(TypedDict):
25+
size: list[int]
26+
counts: list[int]
27+
28+
class _Annotation(TypedDict):
29+
id: int
30+
image_id: int
31+
category_id: int
32+
segmentation: _TPolygonSegmentation | _RLE | _EncodedRLE
33+
area: float
34+
bbox: list[float]
35+
iscrowd: int
36+
37+
_TSeg = TypeVar("_TSeg", _TPolygonSegmentation, _RLE, _EncodedRLE)
38+
39+
class _AnnotationG(TypedDict, Generic[_TSeg]):
40+
id: int
41+
image_id: int
42+
category_id: int
43+
segmentation: _TSeg
44+
area: float
45+
bbox: list[float]
46+
iscrowd: int
47+
48+
class _Category(TypedDict):
49+
id: int
50+
name: str
51+
supercategory: str
52+
53+
class _Dataset(TypedDict):
54+
images: list[_Image]
55+
annotations: list[_Annotation]
56+
categories: list[_Category]
57+
58+
class COCO:
59+
anns: dict[int, _Annotation]
60+
dataset: _Dataset
61+
cats: dict[int, _Category]
62+
imgs: dict[int, _Image]
63+
imgToAnns: dict[int, list[_Annotation]]
64+
catToImgs: dict[int, list[int]]
65+
def __init__(self, annotation_file: str | Path | None = ...) -> None: ...
66+
def createIndex(self) -> None: ...
67+
def info(self) -> None: ...
68+
def getAnnIds(
69+
self,
70+
imgIds: Collection[int] | int = ...,
71+
catIds: Collection[int] | int = ...,
72+
areaRng: Sequence[float] = ...,
73+
iscrowd: bool | None = ...,
74+
) -> list[int]: ...
75+
def getCatIds(
76+
self, catNms: Collection[str] | str = ..., supNms: Collection[str] | str = ..., catIds: Collection[int] | int = ...
77+
) -> list[int]: ...
78+
def getImgIds(self, imgIds: Collection[int] | int = ..., catIds: list[int] | int = ...) -> list[int]: ...
79+
def loadAnns(self, ids: Collection[int] | int = ...) -> list[_Annotation]: ...
80+
def loadCats(self, ids: Collection[int] | int = ...) -> list[_Category]: ...
81+
def loadImgs(self, ids: Collection[int] | int = ...) -> list[_Image]: ...
82+
def showAnns(self, anns: Sequence[_Annotation], draw_bbox: bool = ...) -> None: ...
83+
def loadRes(self, resFile: str) -> COCO: ...
84+
def download(self, tarDir: str | None = ..., imgIds: Collection[int] = ...) -> Literal[-1] | None: ...
85+
def loadNumpyAnnotations(self, data: _NDArray) -> list[_Annotation]: ...
86+
# def loadNumpyAnnotations(self, data: npt.NDArray[np.float64]) -> list[_Annotation]: ...
87+
@overload
88+
def annToRLE(self, ann: _AnnotationG[_RLE]) -> _RLE: ...
89+
@overload
90+
def annToRLE(self, ann: _AnnotationG[_EncodedRLE]) -> _EncodedRLE: ...
91+
@overload
92+
def annToRLE(self, ann: _AnnotationG[_TPolygonSegmentation]) -> _EncodedRLE: ...
93+
def annToMask(self, ann: _Annotation) -> _NDArray: ...
94+
# def annToMask(self, ann: _Annotation) -> npt.NDArray[np.uint8]: ...
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from _typeshed import Incomplete
2+
from typing_extensions import Literal, TypeAlias, TypedDict
3+
4+
from .coco import COCO
5+
6+
# TODO: Use numpy types when #5768 is resolved.
7+
# import numpy as np
8+
# import numpy.typing as npt
9+
10+
_NDArray: TypeAlias = Incomplete
11+
_TIOU: TypeAlias = Literal["segm", "bbox", "keypoints"]
12+
13+
class _EvaluationResult(TypedDict):
14+
image_id: int
15+
category_id: int
16+
aRng: list[int]
17+
maxDet: int
18+
dtIds: list[int]
19+
gtIds: list[int]
20+
dtMatches: _NDArray
21+
# dtMatches: npt.NDArray[np.float64]
22+
gtMatches: _NDArray
23+
# gtMatches: npt.NDArray[np.float64]
24+
dtScores: list[float]
25+
gtIgnore: _NDArray
26+
# gtIgnore: npt.NDArray[np.float64]
27+
dtIgnore: _NDArray
28+
# dtIgnore: npt.NDArray[np.float64]
29+
30+
class COCOeval:
31+
cocoGt: COCO
32+
cocoDt: COCO
33+
evalImgs: list[_EvaluationResult]
34+
eval: _EvaluationResult
35+
params: Params
36+
stats: _NDArray
37+
# stats: npt.NDArray[np.float64]
38+
ious: dict[tuple[int, int], list[float]]
39+
def __init__(self, cocoGt: COCO | None = ..., cocoDt: COCO | None = ..., iouType: _TIOU = ...) -> None: ...
40+
def evaluate(self) -> None: ...
41+
def computeIoU(self, imgId: int, catId: int) -> list[float]: ...
42+
def computeOks(self, imgId: int, catId: int) -> _NDArray: ...
43+
# def computeOks(self, imgId: int, catId: int) -> npt.NDArray[np.float64]: ...
44+
def evaluateImg(self, imgId: int, catId: int, aRng: list[int], maxDet: int) -> _EvaluationResult: ...
45+
def accumulate(self, p: Params | None = ...) -> None: ...
46+
def summarize(self) -> None: ...
47+
48+
class Params:
49+
imgIds: list[int]
50+
catIds: list[int]
51+
iouThrs: _NDArray
52+
# iouThrs: npt.NDArray[np.float64]
53+
recThrs: _NDArray
54+
# recThrs: npt.NDArray[np.float64]
55+
maxDets: list[int]
56+
areaRng: list[int]
57+
areaRngLbl: list[str]
58+
useCats: int
59+
kpt_oks_sigmas: _NDArray
60+
# kpt_oks_sigmas: npt.NDArray[np.float64]
61+
iouType: _TIOU
62+
useSegm: int | None
63+
def __init__(self, iouType: _TIOU = ...) -> None: ...
64+
def setDetParams(self) -> None: ...
65+
def setKpParams(self) -> None: ...
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from _typeshed import Incomplete
2+
from typing import Any, overload
3+
from typing_extensions import TypeAlias
4+
5+
from . import _EncodedRLE
6+
7+
# TODO: Use numpy types when #5768 is resolved.
8+
# import numpy as np
9+
# import numpy.typing as npt
10+
11+
_NPUInt32: TypeAlias = Incomplete # np.uint32
12+
_NDArrayUInt8: TypeAlias = Incomplete # npt.NDArray[np.uint8]
13+
_NDArrayUInt32: TypeAlias = Incomplete # npt.NDArray[np.uint32]
14+
_NDArrayFloat64: TypeAlias = Incomplete # npt.NDArray[np.float64]
15+
16+
def iou(
17+
dt: _NDArrayUInt32 | list[float] | list[_EncodedRLE],
18+
gt: _NDArrayUInt32 | list[float] | list[_EncodedRLE],
19+
pyiscrowd: list[int] | _NDArrayUInt8,
20+
) -> list[Any] | _NDArrayFloat64: ...
21+
def merge(rleObjs: list[_EncodedRLE], intersect: int = ...) -> _EncodedRLE: ...
22+
23+
# ignore an "overlapping overloads" error due to _NDArrayInt32 being an alias for `Incomplete` for now
24+
@overload
25+
def frPyObjects(pyobj: _NDArrayUInt32 | list[list[int]] | list[_EncodedRLE], h: int, w: int) -> list[_EncodedRLE]: ... # type: ignore[misc]
26+
@overload
27+
def frPyObjects(pyobj: list[int] | _EncodedRLE, h: int, w: int) -> _EncodedRLE: ...
28+
def encode(bimask: _NDArrayUInt8) -> _EncodedRLE: ...
29+
def decode(rleObjs: _EncodedRLE) -> _NDArrayUInt8: ...
30+
def area(rleObjs: _EncodedRLE) -> _NPUInt32: ...
31+
def toBbox(rleObjs: _EncodedRLE) -> _NDArrayFloat64: ...

0 commit comments

Comments
 (0)