Skip to content

Commit

Permalink
Lazy loading for OpenCV
Browse files Browse the repository at this point in the history
  • Loading branch information
talmo committed Dec 17, 2024
1 parent 7902913 commit d4a5c37
Show file tree
Hide file tree
Showing 19 changed files with 39 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .conda_mac/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ requirements:
- conda-forge::qudida
- conda-forge::albumentations
- conda-forge::ndx-pose <0.2.0
- conda-forge::lazy_loader

run:
- conda-forge::python >=3.10.0,<3.11.0
Expand Down Expand Up @@ -89,3 +90,4 @@ requirements:
- conda-forge::qudida
- conda-forge::albumentations
- conda-forge::ndx-pose <0.2.0
- conda-forge::lazy_loader
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,5 @@ venv.bak/

# OS generated files
.DS_Store

models/
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ channels:
- conda-forge
- nvidia
- sleap
- anaconda

dependencies:
# Packages SLEAP uses directly
Expand Down Expand Up @@ -42,6 +41,7 @@ dependencies:
- conda-forge::qudida
- conda-forge::albumentations
- conda-forge::ndx-pose <0.2.0
- conda-forge::lazy_loader

# Packages required by tensorflow to find/use GPUs
- conda-forge::cudatoolkit ==11.3.1
Expand Down
3 changes: 2 additions & 1 deletion environment_mac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ dependencies:
- conda-forge::qudida
- conda-forge::albumentations
- conda-forge::ndx-pose <0.2.0
- conda-forge::lazy_loader
- pip:
- "--editable=.[conda_dev]"
- "--editable=.[conda_dev]"
2 changes: 1 addition & 1 deletion environment_no_cuda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ dependencies:
- conda-forge::qudida
- conda-forge::albumentations
- conda-forge::ndx-pose <0.2.0

- conda-forge::lazy_loader
- pip:
- "--editable=.[conda_dev]"
8 changes: 5 additions & 3 deletions pypi_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ scipy>=1.7.0
scikit-image
scikit-learn>=1.0.0
seaborn
tensorflow==2.9.2; platform_machine != 'arm64'
tensorflow-hub
albumentations
ndx-pose<0.2.0
lazy-loader
tensorflow==2.9.2; platform_machine != 'arm64'
tensorflow-hub; platform_machine != 'arm64'
# Silicon Mac specific packages
tensorflow-hub >=0.14.0,<=0.16.1; sys_platform == 'darwin' and platform_machine == 'arm64'
tensorflow-macos >=2.10.0,<2.13.0; sys_platform == 'darwin' and platform_machine == 'arm64'
tensorflow-metal; sys_platform == 'darwin' and platform_machine == 'arm64'
tensorflow-metal >=0.8.0,<=1.1.0; sys_platform == 'darwin' and platform_machine == 'arm64'

# Dependencies of dependencies
# tensorboard 2.11.2 has requirement protobuf<4,>=3.9.2
Expand Down
4 changes: 3 additions & 1 deletion sleap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

# Import submodules we want available at top-level
from sleap.version import __version__, versions
from sleap.io.dataset import Labels, load_file

from sleap.io.video import Video, load_video
from sleap.instance import LabeledFrame, Instance, PredictedInstance, Track
from sleap.skeleton import Skeleton
from sleap.io.dataset import Labels, load_file

import sleap.nn
from sleap.nn.data import pipelines
from sleap.nn import inference
Expand Down
4 changes: 1 addition & 3 deletions sleap/gui/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class which inherits from `AppCommand` (or a more specialized class such as
from typing import Callable, Dict, Iterator, List, Optional, Tuple, Type, Union, cast

import attr
import cv2
import numpy as np
from qtpy import QtCore, QtGui, QtWidgets

Expand Down Expand Up @@ -1794,8 +1793,7 @@ def ask(context: CommandContext, params: dict) -> bool:
"""Shows gui for replacing videos in project."""

def _get_truncation_message(truncation_messages, path, video):
reader = cv2.VideoCapture(path)
last_vid_frame = int(reader.get(cv2.CAP_PROP_FRAME_COUNT))
last_vid_frame = len(Video.from_filename(path))
lfs: List[LabeledFrame] = list(context.labels.get(video))
if lfs is not None:
lfs.sort(key=lambda lf: lf.frame_idx)
Expand Down
3 changes: 2 additions & 1 deletion sleap/gui/dialogs/importvideos.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@

import h5py
import qimage2ndarray
import cv2
import lazy_loader
cv2 = lazy_loader.load("cv2")

from typing import Any, Dict, List, Optional

Expand Down
4 changes: 0 additions & 4 deletions sleap/gui/dialogs/missingfiles.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
"""
Gui for prompting the user to locate one or more missing files.
"""

import os

from pathlib import Path, PurePath
from typing import Callable, List

from qtpy import QtWidgets, QtCore, QtGui

from sleap.io import pathutils
from sleap.gui.dialogs.filedialog import FileDialog

Expand Down
4 changes: 2 additions & 2 deletions sleap/info/feature_suggestions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
from time import time
from typing import Dict, List, Optional, Tuple

import cv2

from sklearn.decomposition import PCA
from sklearn.cluster import KMeans

Expand All @@ -22,6 +20,8 @@
from skimage.util.shape import view_as_windows

from sleap.io.video import Video
import lazy_loader
cv2 = lazy_loader.load("cv2")

logger = logging.getLogger(__name__)

Expand Down
5 changes: 3 additions & 2 deletions sleap/io/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,13 @@
make_instance_cattr,
PredictedInstance,
)

from sleap.io import pathutils
from sleap.io.video import Video, ImgStoreVideo, HDF5Video
from sleap.gui.suggestions import SuggestionFrame
from sleap.gui.dialogs.missingfiles import MissingFilesDialog
from sleap.rangelist import RangeList
from sleap.util import uniquify, json_dumps


"""
The version number to put in the Labels JSON format.
"""
Expand Down Expand Up @@ -2657,6 +2656,8 @@ def video_callback(
context["changed_on_load"] = True

if use_gui:
from sleap.gui.dialogs.missingfiles import MissingFilesDialog

# If there are still missing paths, prompt user
if sum(missing):
# If we are using dummy for any video not found by user
Expand Down
6 changes: 4 additions & 2 deletions sleap/io/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import shutil

import h5py as h5
import cv2
import imgstore
import numpy as np
import attr
import cattr
Expand All @@ -15,6 +13,10 @@
from typing import Iterable, List, Optional, Tuple, Union, Text

from sleap.util import json_loads, json_dumps
import lazy_loader

cv2 = lazy_loader.load("cv2")
imgstore = lazy_loader.load("imgstore")

logger = logging.getLogger(__name__)

Expand Down
3 changes: 2 additions & 1 deletion sleap/io/videowriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
"""

from abc import ABC, abstractmethod
import cv2
import numpy as np
import imageio.v2 as iio
import lazy_loader
cv2 = lazy_loader.load("cv2")


class VideoWriter(ABC):
Expand Down
6 changes: 2 additions & 4 deletions sleap/io/visuals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@
from sleap.io.dataset import Labels
from sleap.gui.color import ColorManager
from sleap.util import usable_cpu_count

import cv2
import os
import numpy as np
import math
from collections import deque
from time import perf_counter
from typing import List, Optional, Tuple

from queue import Queue
from threading import Thread

import logging
import lazy_loader
cv2 = lazy_loader.load("cv2")

logger = logging.getLogger(__name__)

Expand Down
1 change: 1 addition & 0 deletions sleap/nn/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import numpy as np
import tensorflow as tf
import qtpy
import zmq
import io
import os
Expand Down
3 changes: 2 additions & 1 deletion sleap/nn/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import abc
import attr
import numpy as np
import cv2
import lazy_loader
cv2 = lazy_loader.load("cv2")
import functools
from typing import Callable, Deque, Dict, Iterable, List, Optional, Tuple

Expand Down
3 changes: 2 additions & 1 deletion tests/io/test_videowriter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import cv2
import lazy_loader
cv2 = lazy_loader.load("cv2")
from pathlib import Path
from sleap.io.videowriter import VideoWriter, VideoWriterOpenCV, VideoWriterImageio

Expand Down
3 changes: 2 additions & 1 deletion tests/io/test_visuals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import numpy as np
import os
import pytest
import cv2
import lazy_loader
cv2 = lazy_loader.load("cv2")
from sleap.io.dataset import Labels
from sleap.io.visuals import (
save_labeled_video,
Expand Down

0 comments on commit d4a5c37

Please sign in to comment.