Removing all Optional instances.
This commit is contained in:
@@ -5,7 +5,7 @@ import logging
|
|||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Callable, Optional
|
from typing import Any, Callable
|
||||||
|
|
||||||
import cv2
|
import cv2
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@@ -41,9 +41,9 @@ def load_metadata(p: Path, key_id="image", key_proc_fn=None) -> dict[str, dict[s
|
|||||||
|
|
||||||
def load_dataset(
|
def load_dataset(
|
||||||
root: Path,
|
root: Path,
|
||||||
meta_path: Optional[Path] = None,
|
meta_path: Path | None,
|
||||||
imname_proc_fn: Optional[Callable]=None
|
imname_proc_fn: Callable |None
|
||||||
) -> tuple[dict[Path, np.ndarray], Optional[dict[str, dict[str, Any]]]]:
|
) -> tuple[dict[str, np.ndarray], dict[str, dict[str, Any]] | None]:
|
||||||
"""
|
"""
|
||||||
if `meta_path` is `None`, we won't attempt to read it.
|
if `meta_path` is `None`, we won't attempt to read it.
|
||||||
|
|
||||||
@@ -53,20 +53,22 @@ def load_dataset(
|
|||||||
Root path to the images. We assume that the images are positioned in a
|
Root path to the images. We assume that the images are positioned in a
|
||||||
flat directory under `root`.
|
flat directory under `root`.
|
||||||
|
|
||||||
meta_path: Optional[Path]
|
meta_path: Path | None
|
||||||
Path to the metadata file on the dataset. Default is `None`.
|
Path to the metadata file on the dataset. Default is `None`.
|
||||||
|
|
||||||
imname_proc_fn: Optional[Callable]
|
imname_proc_fn: Callable | None
|
||||||
Function to apply to the image filenames when building the output
|
Function to apply to the image filenames when building the output
|
||||||
dictionary. Use it to uniformize it with the metadata file. Default
|
dictionary. Use it to uniformize it with the metadata file. Default
|
||||||
is `None`.
|
is `None`.
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
data: dict[Path, np.ndarray]
|
data: dict[str, np.ndarray]
|
||||||
metadata, Optional[dict[str, dict[str, Any]]]
|
metadata, dict[str, dict[str, Any]] | None
|
||||||
|
If metadata was not found, or not set to load (by leaving `meta_path`
|
||||||
|
as `None`), then we simply return `None`.
|
||||||
"""
|
"""
|
||||||
metadata = dict()
|
metadata = None
|
||||||
paths = set([p for p in root.iterdir() if not p.is_dir()])
|
paths = set([p for p in root.iterdir() if not p.is_dir()])
|
||||||
if meta_path is not None and meta_path in paths:
|
if meta_path is not None and meta_path in paths:
|
||||||
# Just read the metadata and avoid needing to test for it in the main loop.
|
# Just read the metadata and avoid needing to test for it in the main loop.
|
||||||
@@ -74,14 +76,13 @@ def load_dataset(
|
|||||||
paths.remove(meta_path)
|
paths.remove(meta_path)
|
||||||
|
|
||||||
ims = OrderedDict()
|
ims = OrderedDict()
|
||||||
# TODO(gschardong): Paralellize this?
|
|
||||||
for p in paths:
|
for p in paths:
|
||||||
try:
|
try:
|
||||||
im = cv2.cvtColor(cv2.imread(p), cv2.COLOR_BGR2RGB)
|
im = cv2.cvtColor(cv2.imread(p), cv2.COLOR_BGR2RGB)
|
||||||
except cv2.error:
|
except cv2.error:
|
||||||
logger.info(f'File "{p}" is not an image. Skipping.')
|
logger.info(f'File "{p}" is not an image. Skipping.')
|
||||||
else:
|
else:
|
||||||
proc_imname = imname_proc_fn(p.name) if imname_proc_fn is not None else p.name
|
proc_imname = imname_proc_fn(p.name) if imname_proc_fn is not None else str(p.name)
|
||||||
ims[proc_imname] = im
|
ims[proc_imname] = im
|
||||||
|
|
||||||
if not metadata:
|
if not metadata:
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ class MediapipeDetector(BaseFaceDetector):
|
|||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
model_path: str, PathLike
|
model_path: str, PathLike
|
||||||
mode: Optional[mediapipe.tasks.python.vision.RunningMode]
|
mode: mediapipe.tasks.python.vision.RunningMode
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, model_path: str, mode=vision.RunningMode.IMAGE) -> None:
|
def __init__(self, model_path: str, mode=vision.RunningMode.IMAGE) -> None:
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Optional
|
from typing import Any
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import torch
|
import torch
|
||||||
@@ -24,10 +24,10 @@ class FairFace(BaseEstimator):
|
|||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
checkpoint_path: Optional[Path]
|
checkpoint_path: Path | None
|
||||||
Path to the model weights stored as a PyTorch file.
|
Path to the model weights stored as a PyTorch file.
|
||||||
|
|
||||||
device: Optional[torch.device]
|
device: torch.device
|
||||||
The device to load the model into. By default is `"cpu"`
|
The device to load the model into. By default is `"cpu"`
|
||||||
|
|
||||||
Reference
|
Reference
|
||||||
@@ -36,8 +36,8 @@ class FairFace(BaseEstimator):
|
|||||||
"""
|
"""
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
checkpoint_path: Optional[Path]=None,
|
checkpoint_path: Path | None,
|
||||||
device: Optional[torch.device]=torch.device("cpu")
|
device: torch.device=torch.device("cpu")
|
||||||
):
|
):
|
||||||
self.T = transforms.Compose([
|
self.T = transforms.Compose([
|
||||||
transforms.ToPILImage(),
|
transforms.ToPILImage(),
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Optional
|
from typing import Any
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import torch
|
import torch
|
||||||
@@ -24,7 +24,7 @@ class MiVOLOv1(BaseEstimator):
|
|||||||
Path to the saved checkpoint. Note that we expect the checkpoint to be
|
Path to the saved checkpoint. Note that we expect the checkpoint to be
|
||||||
in PyTorch format.
|
in PyTorch format.
|
||||||
|
|
||||||
device: Optional[torch.device]
|
device: torch.device
|
||||||
Device to load the checkpoints into. By default is "cpu".
|
Device to load the checkpoints into. By default is "cpu".
|
||||||
|
|
||||||
Reference
|
Reference
|
||||||
@@ -34,7 +34,7 @@ class MiVOLOv1(BaseEstimator):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
checkpoint_path: Path,
|
checkpoint_path: Path,
|
||||||
device: Optional[torch.device]=torch.device("cpu")
|
device: torch.device=torch.device("cpu")
|
||||||
):
|
):
|
||||||
self.T = transforms.Compose([
|
self.T = transforms.Compose([
|
||||||
transforms.ToPILImage(),
|
transforms.ToPILImage(),
|
||||||
|
|||||||
Reference in New Issue
Block a user