-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
122 lines (111 loc) · 3.61 KB
/
main.py
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
119
120
121
122
from pathlib import Path
from typing import Annotated, cast
import typer
from piano_midi.color_picker import ColorPicker
from piano_midi.key_picker import KeyPicker
from piano_midi.key_press_detector import KeyPressDetector
from piano_midi.key_sequence_writer import KeySequenceWriter
from piano_midi.models import KeyColors, KeySegments
from piano_midi.time_slicer import TimeSlicer
from piano_midi.video_capture import VideoCapture
app = typer.Typer(
name="midi tools",
add_completion=False,
)
@app.command()
def key_picker(
*,
video_path: Annotated[
Path,
typer.Option(
"--video-path", help="Video path that is used for creating the mask"
),
],
key_segments_path: Annotated[
Path,
typer.Option("--key-segments-path", help="Path to store the keysegments to"),
],
) -> None:
typer.echo(f"Starting key picker with image path: {video_path}")
video_capture = VideoCapture(video_path)
with video_capture as cap:
frame = cap.get_frame(frame_number=0)
key_picker = KeyPicker(frame=frame, key_segments_path=key_segments_path)
key_picker.run()
@app.command()
def color_picker(
*,
video_path: Annotated[
Path,
typer.Option(
"--video-path", help="Video path that is used for creating the mask"
),
],
colors_path: Annotated[
Path,
typer.Option("--colors-path", help="Path to store the colors to"),
],
frame_start: Annotated[
int,
typer.Option("--frame-start", help="Frame start for the timeslice"),
] = 0,
frame_end: Annotated[
int | None,
typer.Option("--frame-end", help="Frame end for the timeslice"),
] = None,
) -> None:
typer.echo(f"Starting color picker with image path: {video_path}")
video_capture = VideoCapture(video_path)
time_slicer = TimeSlicer(video_capture)
time_slice = time_slicer.generate(
frame_start=frame_start, frame_end=frame_end, scan_line_px=100
)
color_picker = ColorPicker(time_slice=time_slice, colors_path=colors_path)
color_picker.run()
@app.command()
def video_to_midi(
*,
video_path: Annotated[
Path,
typer.Option(
"--video-path", help="Video path that is used for creating the mask"
),
],
key_segments_path: Annotated[
Path,
typer.Option("--key-segments-path", help="Path to store the keysegments to"),
],
colors_path: Annotated[
Path,
typer.Option("--colors-path", help="Path to store the colors to"),
],
midi_path: Annotated[
Path,
typer.Option("--midi-path", help="Path to store the midi file to"),
],
frame_start: Annotated[
int,
typer.Option("--frame-start", help="Frame start for the timeslice"),
] = 0,
frame_end: Annotated[
int | None,
typer.Option("--frame-end", help="Frame end for the timeslice"),
] = None,
) -> None:
typer.echo(f"Starting video to midi with image path: {video_path}")
video_capture = VideoCapture(video_path)
key_segments = KeySegments.from_yaml(key_segments_path)
key_colors = KeyColors.from_yaml(colors_path)
with video_capture as cap:
key_sequence_writer = KeySequenceWriter(fps=cast(float,cap.fps))
key_press_detector = KeyPressDetector(
video_capture=video_capture, key_segments=key_segments, key_colors=key_colors
)
key_press_detector.run(
key_sequence_writer=key_sequence_writer,
frame_start=frame_start,
frame_end=frame_end,
)
key_sequence_writer.save(midi_file_path=midi_path)
if __name__ == "__main__":
app()