Repository navigation
Expand file tree
/
Copy pathoptical_flow.py
More file actions
118 lines (98 loc) · 3.96 KB
/
Copy pathoptical_flow.py
File metadata and controls
118 lines (98 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Optical Flow
# Copyright (C) 2024-2026 Collabora Ltd.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
from log.global_logger import GlobalLogger
import backend
CAN_REGISTER_ELEMENT = True
try:
from video_transform import VideoTransform
from utils.format_converter import FormatConverter
from engine.optical_flow_engine import ExportedOpticalFlow, OpticalFlowEngine
from engine.engine_factory import EngineFactory
from backend import frameio, GObject
from tasks.optical_flow import OpticalFlowTask
except ImportError as e:
CAN_REGISTER_ELEMENT = False
GlobalLogger().warning(
f"The 'optical_flow' element will not be available. Error {e}"
)
# Colormap names for flow visualization
FLOW_COLORMAPS = {
"hsv": None, # custom HSV-based flow coloring
"jet": 2,
"viridis": 16,
"inferno": 9,
}
class OpticalFlowTransform(VideoTransform, OpticalFlowTask):
"""
GStreamer element for dense optical flow estimation using RAFT.
Set model-name to a RAFT variant: raft_large or raft_small.
Computes dense optical flow between consecutive frames. When
visualize=True (default), the flow is rendered as a color-coded overlay
on the video frame using HSV color space (hue=direction, value=magnitude).
"""
__gstmetadata__ = (
"Optical Flow",
"Transform",
"Dense optical flow estimation using RAFT",
"Aaron Boxer <aaron.boxer@collabora.com>",
)
visualize = GObject.Property(
type=bool,
default=True,
nick="Visualize Flow",
blurb="Overlay color-coded optical flow on the video frame",
flags=GObject.ParamFlags.READWRITE,
)
colormap = GObject.Property(
type=str,
default="hsv",
nick="Colormap",
blurb="Colormap for flow visualization: hsv, jet, viridis, inferno",
flags=GObject.ParamFlags.READWRITE,
)
def __init__(self):
super().__init__()
self.task_engine_name = self.mgr.engine_name = "pyml_optical_flow_engine"
EngineFactory.register(self.mgr.engine_name, OpticalFlowEngine)
self.format_converter = FormatConverter()
self._prev_frame = None
def export_model(self, model_name):
return ExportedOpticalFlow(model_name, self.mgr.engine_name)
def process_frames(self, frames, num_sources, fmt, target):
"""Pair this frame with the previous one and draw the flow overlay."""
frame = frames[0] if frames.ndim == 4 else frames
# Temporal pairing stays in the shell: hold the previous frame.
if self._prev_frame is None:
self._prev_frame = frame.copy()
return
flow = self.forward(self._prev_frame, frame)
self._prev_frame = frame.copy()
if flow is None:
return
if self.visualize:
# Portable task: render the flow overlay frame.
output, blob = self.decode(flow, frame, fmt)
frameio.write_result(target, output, blob)
if CAN_REGISTER_ELEMENT and backend.BACKEND == "gst":
__gstelementfactory__ = backend.register_gst_element(
"pyml_optical_flow", OpticalFlowTransform
)
elif not CAN_REGISTER_ELEMENT:
GlobalLogger().warning(
"The 'pyml_optical_flow' element will not be registered because required modules are missing."
)