Skip to content

Commit

Permalink
Add a filter to rotate and flip
Browse files Browse the repository at this point in the history
  • Loading branch information
aMarcireau committed Apr 4, 2023
1 parent df4db06 commit 7a846b7
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 17 deletions.
44 changes: 42 additions & 2 deletions charidotella/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from . import filters as filters
from . import tasks as tasks
from . import utilities as utilities
from .version import __version__ as __version__

filter_apply = typing.Callable[
[
Expand All @@ -35,6 +36,7 @@
"default": filters.default.apply,
"arbiter_saturation": filters.arbiter_saturation.apply,
"hot_pixels": filters.hot_pixels.apply,
"transpose": filters.transpose.apply,
}

task_run = typing.Callable[
Expand All @@ -61,6 +63,9 @@ def main():
description="Process Event Stream files",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--version", "-v", action="store_true", help="show the version and exit"
)
subparsers = parser.add_subparsers(dest="command")
configure_parser = subparsers.add_parser(
"configure", help="Generate a configuration file"
Expand Down Expand Up @@ -116,6 +121,12 @@ def main():
help="Resolved render configuration file path",
)
args = parser.parse_args()
if args.version:
print(__version__)
sys.exit(0)
if args.command is None:
parser.print_help(sys.stderr)
sys.exit(1)

class Encoder(toml.TomlEncoder):
def dump_list(self, v):
Expand Down Expand Up @@ -418,6 +429,35 @@ def run_generators(configuration: dict[str, typing.Any]):
"ratio": "@raw(ratio)",
},
},
{
"parameters": {
"suffix": [
"flip-left-right",
"flip-top_bottom",
"rotate-90",
"rotate-180",
"rotate-270",
"transpose",
"transverse",
],
"method": [
"flip_left_right",
"flip_top_bottom",
"rotate_90",
"rotate_180",
"rotate_270",
"transpose",
"transverse",
],
},
"template": {
"name": "transpose-@suffix",
"type": "transpose",
"icon": "📐",
"suffix": "@suffix",
"method": "@method",
},
},
]
},
configuration_file,
Expand Down Expand Up @@ -513,13 +553,13 @@ def run_generators(configuration: dict[str, typing.Any]):
"icon": "🌀",
"forward_duration": "@raw(forward_duration)",
"tau_to_frametime_ratio": 3.0,
"style": "linear",
"style": "cumulative",
"idle_color": "#191919",
"on_color": "#F4C20D",
"off_color": "#1E88E5",
"idle_color": "#191919",
"cumulative_ratio": 0.01,
"timecode": False,
"timecode": True,
"ffmpeg": "ffmpeg",
"scale": 1,
},
Expand Down
1 change: 1 addition & 0 deletions charidotella/filters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from . import arbiter_saturation as arbiter_saturation
from . import default as default
from . import hot_pixels as hot_pixels
from . import transpose as transpose
89 changes: 89 additions & 0 deletions charidotella/filters/transpose.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import pathlib
import typing

import event_stream
import numpy


def apply(
input: pathlib.Path,
output: pathlib.Path,
begin: int,
end: int,
parameters: dict[str, typing.Any],
) -> None:
with event_stream.Decoder(input) as decoder:
if parameters["method"] == "flip_left_right":
width, height = decoder.width, decoder.height
method = 0
elif parameters["method"] == "flip_top_bottom":
width, height = decoder.width, decoder.height
method = 1
elif parameters["method"] == "rotate_90":
width, height = decoder.height, decoder.width
method = 2
elif parameters["method"] == "rotate_180":
width, height = decoder.width, decoder.height
method = 3
elif parameters["method"] == "rotate_270":
width, height = decoder.height, decoder.width
method = 4
elif parameters["method"] == "transpose":
width, height = decoder.height, decoder.width
method = 5
elif parameters["method"] == "transverse":
width, height = decoder.height, decoder.width
method = 6
else:
raise Exception(f"unknown method \"{parameters['method']}\"")
with event_stream.Encoder(
output,
"dvs",
width,
height,
) as encoder:
for packet in decoder:
if packet["t"][-1] < begin:
continue
if packet["t"][0] >= end:
break
if packet["t"][0] < begin and packet["t"][-1] >= end:
events = packet[
numpy.logical_and(packet["t"] >= begin, packet["t"] < end)
]
elif begin is not None and packet["t"][0] < begin:
events = packet[packet["t"] >= begin]
elif end is not None and packet["t"][-1] >= end:
events = packet[packet["t"] < end]
else:
events = packet
if method == 0: # flip_left_right
events["x"] = width - 1 - events["x"]
elif method == 1: # flip_top_bottom
events["y"] = height - 1 - events["y"]
elif method == 2: # rotate_90
new_events = events.copy()
new_events["x"] = width - 1 - events["y"]
new_events["y"] = events["x"]
events = new_events
elif method == 3: # rotate_180
events["x"] = width - 1 - events["x"]
events["y"] = height - 1 - events["y"]
elif method == 4: # rotate_270
new_events = events.copy()
new_events["x"] = events["y"]
new_events["y"] = height - 1 - events["x"]
events = new_events
elif method == 5: # transpose
new_events = events.copy()
new_events["x"] = events["y"]
new_events["y"] = events["x"]
events = new_events
elif method == 6: # transverse
new_events = events.copy()
new_events["x"] = width - 1 - events["y"]
new_events["y"] = height - 1 - events["x"]
events = new_events
else:
raise Exception(f"unknown method {method}")
encoder.write(events)
4 changes: 1 addition & 3 deletions charidotella/tasks/event_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ def run(
subprocess.run(
[
str(
importlib.resources.files("charidotella").joinpath(
"assets/event_rate"
)
importlib.resources.files("charidotella").joinpath("assets/event_rate")
),
str(input),
str(output),
Expand Down
12 changes: 2 additions & 10 deletions charidotella/tasks/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ def run(
int(value)
for value in subprocess.run(
[
str(
importlib.resources.files("charidotella").joinpath(
"assets/size"
)
),
str(importlib.resources.files("charidotella").joinpath("assets/size")),
str(input),
],
check=True,
Expand All @@ -32,11 +28,7 @@ def run(
width *= parameters["scale"]
height *= parameters["scale"]
es_to_frames_arguments = [
str(
importlib.resources.files("charidotella").joinpath(
"assets/es_to_frames"
)
),
str(importlib.resources.files("charidotella").joinpath("assets/es_to_frames")),
f"--input={input}",
f"--begin={begin}",
f"--end={end}",
Expand Down
1 change: 1 addition & 0 deletions charidotella/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.7"
27 changes: 27 additions & 0 deletions configuration-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,33 @@
},
"additionalProperties": false,
"required": ["type", "icon", "suffix", "ratio"]
},
{
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["transpose"]
},
"icon": {
"type": "string"
},
"suffix": {
"type": "string"
},
"method": {
"type": "string",
"enum": [
"flip_left_right",
"flip_top_bottom",
"rotate_90",
"rotate_180",
"rotate_270",
"transpose",
"transverse"
]
}
}
}
]
}
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pathlib
import subprocess
import shutil
import subprocess
import sys

import setuptools
Expand Down Expand Up @@ -87,7 +87,7 @@

setuptools.setup(
name="charidotella",
version="0.6",
version="0.7",
url="https://github.com/neuromorphicsystems/charidotella",
author="Alexandre Marcireau",
author_email="alexandre.marcireau@gmail.com",
Expand Down

0 comments on commit 7a846b7

Please sign in to comment.