Browse Source

Merge pull request #327 from vcozzolino/bug-321

Move algorithm-specific dependencies into class definition (optical flow)
tags/v0.5.1
KubeEdge Bot GitHub 3 years ago
parent
commit
778a2caf1e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 16 deletions
  1. +1
    -0
      lib/requirements.dev.txt
  2. +30
    -16
      lib/sedna/algorithms/optical_flow/__init__.py

+ 1
- 0
lib/requirements.dev.txt View File

@@ -4,3 +4,4 @@ plato-learn~=0.26 # Apache-2.0
scikit-learn~=0.24.1 # BSD scikit-learn~=0.24.1 # BSD
# multi_edge_inference # multi_edge_inference
kafka-python~=2.0.2 # Apache-2.0 kafka-python~=2.0.2 # Apache-2.0
opencv-python~=4.6.0 # Apache-2.0

+ 30
- 16
lib/sedna/algorithms/optical_flow/__init__.py View File

@@ -14,9 +14,7 @@


"""Optical Flow Algorithms""" """Optical Flow Algorithms"""
import abc import abc

import numpy import numpy
import cv2
from sedna.common.class_factory import ClassFactory, ClassType from sedna.common.class_factory import ClassFactory, ClassType
from sedna.common.log import LOGGER from sedna.common.log import LOGGER


@@ -40,6 +38,8 @@ class BaseFilter(metaclass=abc.ABCMeta):


@ClassFactory.register(ClassType.OF, alias="LukasKanadeOF") @ClassFactory.register(ClassType.OF, alias="LukasKanadeOF")
class LukasKanade(BaseFilter, abc.ABC): class LukasKanade(BaseFilter, abc.ABC):
import cv2

""" """
Class to detect movement between two consecutive images. Class to detect movement between two consecutive images.
""" """
@@ -49,7 +49,7 @@ class LukasKanade(BaseFilter, abc.ABC):
dict(maxCorners=100, qualityLevel=0.3, minDistance=7, blockSize=7) dict(maxCorners=100, qualityLevel=0.3, minDistance=7, blockSize=7)


# Parameters for Lucas Kanade optical flow # Parameters for Lucas Kanade optical flow
_criteria = cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT
_criteria = self.cv2.TERM_CRITERIA_EPS | self.cv2.TERM_CRITERIA_COUNT


self.lk_params = dict( self.lk_params = dict(
winSize=(15, 15), winSize=(15, 15),
@@ -67,14 +67,15 @@ class LukasKanade(BaseFilter, abc.ABC):


movement = False movement = False
try: try:
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
p0 = cv2.goodFeaturesToTrack(
old_gray = self.cv2.cvtColor(old_frame, self.cv2.COLOR_BGR2GRAY)
p0 = self.cv2.goodFeaturesToTrack(
old_gray, mask=None, **self.feature_params) old_gray, mask=None, **self.feature_params)


current_gray = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY)
current_gray = \
self.cv2.cvtColor(current_frame, self.cv2.COLOR_BGR2GRAY)


# Calculate Optical Flow # Calculate Optical Flow
p1, st, err = cv2.calcOpticalFlowPyrLK(
p1, st, err = self.cv2.calcOpticalFlowPyrLK(
old_gray, current_gray, p0, None, **self.lk_params old_gray, current_gray, p0, None, **self.lk_params
) )


@@ -88,7 +89,10 @@ class LukasKanade(BaseFilter, abc.ABC):
# Allclose is used instead of array_equal to support # Allclose is used instead of array_equal to support
# array of floats (if we remove rounding). # array of floats (if we remove rounding).
movement = \ movement = \
not numpy.allclose(numpy.rint(good_new), numpy.rint(good_old))
not numpy.allclose(
numpy.rint(good_new),
numpy.rint(good_old)
)
except Exception as ex: except Exception as ex:
LOGGER.error( LOGGER.error(
f"Error during the execution of\ f"Error during the execution of\
@@ -99,6 +103,9 @@ class LukasKanade(BaseFilter, abc.ABC):


@ClassFactory.register(ClassType.OF, alias="LukasKanadeOF_CUDA") @ClassFactory.register(ClassType.OF, alias="LukasKanadeOF_CUDA")
class LukasKanadeCUDA(BaseFilter, abc.ABC): class LukasKanadeCUDA(BaseFilter, abc.ABC):
import cv2
import numpy

""" """
Class to detect movement between Class to detect movement between
two consecutive images (GPU implementation). two consecutive images (GPU implementation).
@@ -107,7 +114,7 @@ class LukasKanadeCUDA(BaseFilter, abc.ABC):
# Parameters for ShiTomasi corner detection # Parameters for ShiTomasi corner detection
self.feature_params = \ self.feature_params = \
dict( dict(
srcType=cv2.CV_8UC1,
srcType=self.cv2.CV_8UC1,
maxCorners=100, maxCorners=100,
qualityLevel=0.3, qualityLevel=0.3,
minDistance=7, minDistance=7,
@@ -120,8 +127,11 @@ class LukasKanadeCUDA(BaseFilter, abc.ABC):
) )


self.corner_detector = \ self.corner_detector = \
cv2.cuda.createGoodFeaturesToTrackDetector(**self.feature_params)
self.of = cv2.cuda.SparsePyrLKOpticalFlow_create(**self.lk_params)
self.cv2.cuda.createGoodFeaturesToTrackDetector(
**self.feature_params
)
self.of = \
self.cv2.cuda.SparsePyrLKOpticalFlow_create(**self.lk_params)


def __call__(self, old_frame=None, current_frame=None): def __call__(self, old_frame=None, current_frame=None):
""" """
@@ -131,16 +141,17 @@ class LukasKanadeCUDA(BaseFilter, abc.ABC):
`False` means that there is no movement. `False` means that there is no movement.
""" """


old_frame = cv2.cuda_GpuMat(old_frame)
current_frame = cv2.cuda_GpuMat(current_frame)
old_frame = self.cv2.cuda_GpuMat(old_frame)
current_frame = self.cv2.cuda_GpuMat(current_frame)


movement = False movement = False
try: try:
old_gray = cv2.cuda.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
old_gray = \
self.cv2.cuda.cvtColor(old_frame, self.cv2.COLOR_BGR2GRAY)
p0 = self.corner_detector.detect(old_gray) p0 = self.corner_detector.detect(old_gray)


current_gray = \ current_gray = \
cv2.cuda.cvtColor(current_frame, cv2.COLOR_BGR2GRAY)
self.cv2.cuda.cvtColor(current_frame, self.cv2.COLOR_BGR2GRAY)


# Calculate Optical Flow # Calculate Optical Flow
p1, st, err = self.of.calc( p1, st, err = self.of.calc(
@@ -161,7 +172,10 @@ class LukasKanadeCUDA(BaseFilter, abc.ABC):
# Allclose is used instead of array_equal to # Allclose is used instead of array_equal to
# support array of floats (if we remove rounding). # support array of floats (if we remove rounding).
movement = \ movement = \
not numpy.allclose(numpy.rint(good_new), numpy.rint(good_old))
not numpy.allclose(
numpy.rint(good_new),
numpy.rint(good_old)
)
except Exception as ex: except Exception as ex:
LOGGER.error( LOGGER.error(
f"Error during the execution of\ f"Error during the execution of\


Loading…
Cancel
Save