Skip to content

Commit

Permalink
Use Tensorflow 2.5 now, go to 1.1 version
Browse files Browse the repository at this point in the history
1.1 releases supports now tensorflow >=2.5

see #46
  • Loading branch information
metal3d committed Jul 23, 2021
1 parent e63492c commit 39dc329
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 158 deletions.
29 changes: 12 additions & 17 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
#!/usr/bin/env python

from setuptools import setup, find_packages
from setuptools import find_packages, setup

with open("README.md", "r") as fh:
long_description = fh.read()

setup(
name='keras-video-generators',
version='1.0.13',
description='Keras sequence generators for video data',
name="keras-video-generators",
version="1.1.0",
description="Keras sequence generators for video data",
long_description=long_description,
long_description_content_type="text/markdown",
license='MIT',
licence_file='LICENSE',
author='Patrice Ferlet',
author_email='[email protected]',
url='https://github.com/metal3d/keras-video-generators',
package_dir={'': 'src'},
packages=find_packages('src'),
install_requires=[
'keras>=2',
'numpy',
'opencv-python',
'matplotlib'
]
license="MIT",
licence_file="LICENSE",
author="Patrice Ferlet",
author_email="[email protected]",
url="https://github.com/metal3d/keras-video-generators",
package_dir={"": "src"},
packages=find_packages("src"),
install_requires=["tensorflow>=2.5", "numpy", "opencv-python", "matplotlib"],
)
64 changes: 37 additions & 27 deletions src/keras_video/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
"""

import numpy as np
import cv2 as cv
import numpy as np
import tensorflow.keras.preprocessing.image as kimage

from .generator import VideoFrameGenerator
import keras.preprocessing.image as kimage

METHOD_OPTICAL_FLOW = 1
METHOD_FLOW_MASK = 2
Expand Down Expand Up @@ -57,15 +58,16 @@ class OpticalFlowGenerator(VideoFrameGenerator):
"""

def __init__(
self,
*args,
nb_frames=5,
method=METHOD_OPTICAL_FLOW,
flowlevel=3,
iterations=3,
winsize=15,
**kwargs):
super().__init__(nb_frames=nb_frames+1, *args, **kwargs)
self,
*args,
nb_frames=5,
method=METHOD_OPTICAL_FLOW,
flowlevel=3,
iterations=3,
winsize=15,
**kwargs
):
super().__init__(nb_frames=nb_frames + 1, *args, **kwargs)
self.flowlevel = flowlevel
self.iterations = iterations
self.winsize = winsize
Expand Down Expand Up @@ -107,13 +109,20 @@ def make_optical_flow(self, images):
images[i] = cv.cvtColor(image, cv.COLOR_RGB2GRAY)

flow = cv.calcOpticalFlowFarneback(
images[0], images[1], # image prev and next
None, 0.5, self.flowlevel,
self.winsize, self.iterations,
5, 1.1, 0)
images[0],
images[1], # image prev and next
None,
0.5,
self.flowlevel,
self.winsize,
self.iterations,
5,
1.1,
0,
)

mag, ang = cv.cartToPolar(flow[..., 0], flow[..., 1])
hsv[..., 0] = ang*180/np.pi/2
hsv[..., 0] = ang * 180 / np.pi / 2
hsv[..., 2] = cv.normalize(mag, None, 0, 255, cv.NORM_MINMAX)
rgb = cv.cvtColor(hsv, cv.COLOR_HSV2BGR)

Expand All @@ -123,53 +132,54 @@ def make_optical_flow(self, images):
return rgb

def diff_mask(self, images):
""" Get absolute diff mask, then merge frames and apply the mask
"""
"""Get absolute diff mask, then merge frames and apply the mask"""
mask = self.absdiff(images)
mask = cv.GaussianBlur(mask, (15, 15), 0)

image = cv.addWeighted(images[0], .5, images[1], .5, 0)
image = cv.addWeighted(images[0], 0.5, images[1], 0.5, 0)

return cv.multiply(image, mask)

def flow_mask(self, images):
"""
Get optical flow on images, then merge images and apply the mask
"""
mask = self.make_optical_flow(images) / 255.
mask = self.make_optical_flow(images) / 255.0
mask = cv.GaussianBlur(mask, (15, 15), 0)

image = cv.addWeighted(images[0], .5, images[1], .5, 0)
image = cv.addWeighted(images[0], 0.5, images[1], 0.5, 0)

return cv.multiply(image, mask)

def get_validation_generator(self):
""" Return the validation generator if you've provided split factor """
return self.__class__(
method=self.method,
nb_frames=self.nbframe-1,
nb_frames=self.nbframe - 1,
nb_channel=self.nb_channel,
target_shape=self.target_shape,
classes=self.classes,
batch_size=self.batch_size,
shuffle=self.shuffle,
rescale=self.rescale,
glob_pattern=self.glob_pattern,
_validation_data=self.validation)
_validation_data=self.validation,
)

def get_test_generator(self):
""" Return the validation generator if you've provided split factor """
return self.__class__(
method=self.method,
nb_frames=self.nbframe-1,
nb_frames=self.nbframe - 1,
nb_channel=self.nb_channel,
target_shape=self.target_shape,
classes=self.classes,
batch_size=self.batch_size,
shuffle=self.shuffle,
rescale=self.rescale,
glob_pattern=self.glob_pattern,
_test_data=self.test)
_test_data=self.test,
)

def __getitem__(self, idx):
batch = super().__getitem__(idx)
Expand All @@ -179,9 +189,9 @@ def __getitem__(self, idx):
imgs = item
batch_len = len(imgs)
frames = []
for i in range(batch_len-1):
for i in range(batch_len - 1):
im1 = imgs[i]
im2 = imgs[i+1]
im2 = imgs[i + 1]
if self.method == METHOD_OPTICAL_FLOW:
image = self.make_optical_flow((im1, im2))
elif self.method == METHOD_ABS_DIFF:
Expand Down
Loading

0 comments on commit 39dc329

Please sign in to comment.