Lerobot-Dataset-Ruby 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +21 -0
- data/ext/opencv/extconf.rb +7 -0
- data/ext/opencv/libopencv.cpp +281 -0
- data/lib/Lerobot/Dataset/Ruby/reader.rb +174 -0
- data/lib/Lerobot/Dataset/Ruby/version.rb +9 -0
- data/lib/Lerobot/Dataset/Ruby/writer.rb +362 -0
- data/lib/Lerobot/Dataset/Ruby.rb +13 -0
- data/sig/Lerobot/Dataset/Ruby.rbs +8 -0
- data/test/minimal_lerobot_check.rb +49 -0
- data/test/sample.rb +56 -0
- data/test/test_dataset.py +189 -0
- data/test/test_dataset_reader.rb +95 -0
- data/test/test_helper.rb +6 -0
- data/test/test_libopencv.rb +52 -0
- metadata +108 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 244035b2c7eb6cf559933949e548ad146bf5b5fc4f85741cc583d0d663f2df0a
|
|
4
|
+
data.tar.gz: 4ecb290a52027bb0d1c250a61f4a31474e95e290e39b79b59084ae913caa8c52
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 2ffbe6e6b3c0a3b226eef5b065010b281b26ac7c6e98edca3519087d6ed018bac646ddb79472d06757a094d513d538c24cf148cff33aaac2a6b80dd0a87f8866
|
|
7
|
+
data.tar.gz: 677f955ef392be20c35159ca73d49d33b87578b16b2b144c8b944b4b6baf4c4e0b24934693284c35203984b887907470981090a3b266e367bae89421ff07b8c6
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Code of Conduct
|
|
2
|
+
|
|
3
|
+
"Lerobot-Dataset-Ruby" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
|
|
4
|
+
|
|
5
|
+
* Participants will be tolerant of opposing views.
|
|
6
|
+
* Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
|
|
7
|
+
* When interpreting the words and actions of others, participants should always assume good intentions.
|
|
8
|
+
* Behaviour which can be reasonably considered harassment will not be tolerated.
|
|
9
|
+
|
|
10
|
+
If you have any concerns about behaviour within this project, please contact us at ["kimura_yuuto@it-mirai-h.ibk.ed.jp"](mailto:"kimura_yuuto@it-mirai-h.ibk.ed.jp").
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 kimurayuuto-netizen
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Lerobot::Dataset::Ruby
|
|
2
|
+
|
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
|
4
|
+
|
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/Lerobot/Dataset/Ruby`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
|
10
|
+
|
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
TODO: Write usage instructions here
|
|
26
|
+
|
|
27
|
+
## Development
|
|
28
|
+
|
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
30
|
+
|
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
32
|
+
|
|
33
|
+
## Contributing
|
|
34
|
+
|
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/Lerobot-Dataset-Ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/Lerobot-Dataset-Ruby/blob/master/CODE_OF_CONDUCT.md).
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
40
|
+
|
|
41
|
+
## Code of Conduct
|
|
42
|
+
|
|
43
|
+
Everyone interacting in the Lerobot::Dataset::Ruby project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/Lerobot-Dataset-Ruby/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bundler/gem_tasks"
|
|
4
|
+
require "rake/testtask"
|
|
5
|
+
|
|
6
|
+
desc "Compile native extension"
|
|
7
|
+
task :compile do
|
|
8
|
+
so_file = File.expand_path("ext/opencv/libopencv.so", __dir__)
|
|
9
|
+
unless File.exist?(so_file)
|
|
10
|
+
system("cd #{File.expand_path('ext/opencv', __dir__)} && ruby extconf.rb && make", exception: true)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
Rake::TestTask.new(:test) do |t|
|
|
15
|
+
t.libs << "test"
|
|
16
|
+
t.libs << "lib"
|
|
17
|
+
t.test_files = FileList["test/test_*.rb"]
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
task test: :compile
|
|
21
|
+
task default: :test
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
#include <opencv4/opencv2/videoio.hpp>
|
|
2
|
+
#include <opencv4/opencv2/highgui.hpp>
|
|
3
|
+
#include <opencv4/opencv2/core.hpp>
|
|
4
|
+
#include <opencv4/opencv2/imgcodecs.hpp>
|
|
5
|
+
#include <opencv4/opencv2/imgproc.hpp>
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
#include <ruby.h>
|
|
9
|
+
#include <stdlib.h>
|
|
10
|
+
#include <cstdio>
|
|
11
|
+
#include <string>
|
|
12
|
+
#include <vector>
|
|
13
|
+
|
|
14
|
+
cv::VideoCapture camera;
|
|
15
|
+
cv::VideoWriter video_writer;
|
|
16
|
+
cv::Mat frame;
|
|
17
|
+
bool debug_mode = false;
|
|
18
|
+
bool recording = false;
|
|
19
|
+
std::string recording_path;
|
|
20
|
+
double recording_fps = 30.0;
|
|
21
|
+
int camera_width = 0;
|
|
22
|
+
int camera_height = 0;
|
|
23
|
+
|
|
24
|
+
static VALUE open_camera_source(VALUE self, VALUE source, VALUE width, VALUE height) {
|
|
25
|
+
int w = NUM2INT(width);
|
|
26
|
+
int h = NUM2INT(height);
|
|
27
|
+
if (w <= 0 || h <= 0) {
|
|
28
|
+
rb_raise(rb_eArgError, "幅と高さは0より大きい値を指定してください。");
|
|
29
|
+
return Qfalse;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
camera.release();
|
|
33
|
+
camera.open(NUM2INT(source));
|
|
34
|
+
debug_mode = false;
|
|
35
|
+
|
|
36
|
+
if (!camera.isOpened()) {
|
|
37
|
+
rb_raise(rb_eRuntimeError, "カメラの起動に失敗しました。");
|
|
38
|
+
return Qfalse;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
camera.set(cv::CAP_PROP_FRAME_WIDTH, w);
|
|
42
|
+
camera.set(cv::CAP_PROP_FRAME_HEIGHT, h);
|
|
43
|
+
|
|
44
|
+
camera_width = static_cast<int>(camera.get(cv::CAP_PROP_FRAME_WIDTH));
|
|
45
|
+
camera_height = static_cast<int>(camera.get(cv::CAP_PROP_FRAME_HEIGHT));
|
|
46
|
+
if (camera_width <= 0 || camera_height <= 0) {
|
|
47
|
+
camera_width = w;
|
|
48
|
+
camera_height = h;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return Qtrue;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static VALUE open_mp4_source(VALUE self, VALUE path) {
|
|
55
|
+
if (NIL_P(path) || TYPE(path) != T_STRING || StringValueCStr(path)[0] == '\0') {
|
|
56
|
+
rb_raise(rb_eArgError, "MP4のパスを文字列で指定してください。");
|
|
57
|
+
return Qfalse;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
camera.release();
|
|
61
|
+
camera.open(StringValueCStr(path));
|
|
62
|
+
debug_mode = true;
|
|
63
|
+
|
|
64
|
+
if (!camera.isOpened()) {
|
|
65
|
+
rb_raise(rb_eRuntimeError, "MP4動画の起動に失敗しました。");
|
|
66
|
+
return Qfalse;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
camera_width = static_cast<int>(camera.get(cv::CAP_PROP_FRAME_WIDTH));
|
|
70
|
+
camera_height = static_cast<int>(camera.get(cv::CAP_PROP_FRAME_HEIGHT));
|
|
71
|
+
|
|
72
|
+
return Qtrue;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static VALUE start_recording(VALUE self, VALUE path, VALUE fps) {
|
|
76
|
+
if (recording) {
|
|
77
|
+
rb_raise(rb_eRuntimeError, "動画をすでに録画中です。");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (NIL_P(path) || TYPE(path) != T_STRING || StringValueCStr(path)[0] == '\0') {
|
|
81
|
+
rb_raise(rb_eArgError, "保存先パスを文字列で指定してください。");
|
|
82
|
+
return Qfalse;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
recording_path = StringValueCStr(path);
|
|
86
|
+
recording_fps = NUM2DBL(fps);
|
|
87
|
+
if (recording_fps <= 0) {
|
|
88
|
+
rb_raise(rb_eArgError, "FPSは0より大きい値を指定してください。");
|
|
89
|
+
return Qfalse;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (!camera.isOpened()) {
|
|
93
|
+
rb_raise(rb_eRuntimeError, "カメラが開かれていません。");
|
|
94
|
+
return Qfalse;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (camera_width <= 0 || camera_height <= 0) {
|
|
98
|
+
camera_width = static_cast<int>(camera.get(cv::CAP_PROP_FRAME_WIDTH));
|
|
99
|
+
camera_height = static_cast<int>(camera.get(cv::CAP_PROP_FRAME_HEIGHT));
|
|
100
|
+
if ((camera_width <= 0 || camera_height <= 0) && !frame.empty()) {
|
|
101
|
+
camera_width = frame.cols;
|
|
102
|
+
camera_height = frame.rows;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (camera_width <= 0 || camera_height <= 0) {
|
|
107
|
+
rb_raise(rb_eRuntimeError, "カメラの解像度を取得できませんでした。");
|
|
108
|
+
return Qfalse;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (video_writer.isOpened()) {
|
|
112
|
+
video_writer.release();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
int codec = cv::VideoWriter::fourcc('H', '2', '6', '4');
|
|
116
|
+
bool opened = video_writer.open(recording_path, codec, recording_fps, cv::Size(camera_width, camera_height), true);
|
|
117
|
+
if (!opened) {
|
|
118
|
+
rb_raise(rb_eRuntimeError, "動画ファイルを開けませんでした。");
|
|
119
|
+
return Qfalse;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
recording = true;
|
|
123
|
+
return Qtrue;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
static VALUE stop_recording(VALUE self) {
|
|
127
|
+
if (recording) {
|
|
128
|
+
video_writer.release();
|
|
129
|
+
recording = false;
|
|
130
|
+
}
|
|
131
|
+
return Qtrue;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
static VALUE mat2str(cv::Mat mat) {
|
|
135
|
+
if (mat.empty()) {
|
|
136
|
+
return Qnil; // 空のMatオブジェクトはnilを返す
|
|
137
|
+
}
|
|
138
|
+
std::vector<uchar> buf;
|
|
139
|
+
cv::imencode(".jpg", mat, buf);
|
|
140
|
+
return rb_str_new((const char *)buf.data(), buf.size());
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
static cv::Mat str2mat(VALUE data) {
|
|
144
|
+
if (NIL_P(data)) {
|
|
145
|
+
return cv::Mat();
|
|
146
|
+
}
|
|
147
|
+
std::string ruby_str = std::string(RSTRING_PTR(data), RSTRING_LEN(data));
|
|
148
|
+
std::vector<uchar> cppstr(ruby_str.begin(), ruby_str.end());
|
|
149
|
+
cv::Mat ret = cv::imdecode(cppstr, cv::IMREAD_COLOR);
|
|
150
|
+
return ret;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
static VALUE write_frame(VALUE self, VALUE image_data) {
|
|
154
|
+
if (!recording) {
|
|
155
|
+
rb_raise(rb_eRuntimeError, "録画が開始されていません。");
|
|
156
|
+
return Qfalse;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
cv::Mat mat = str2mat(image_data);
|
|
160
|
+
if (mat.empty()) {
|
|
161
|
+
rb_raise(rb_eRuntimeError, "フレームデータのデコードに失敗しました。");
|
|
162
|
+
return Qfalse;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (!video_writer.isOpened()) {
|
|
166
|
+
rb_raise(rb_eRuntimeError, "動画ファイルが開かれていません。");
|
|
167
|
+
return Qfalse;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (camera_width > 0 && camera_height > 0 && (mat.cols != camera_width || mat.rows != camera_height)) {
|
|
171
|
+
cv::resize(mat, mat, cv::Size(camera_width, camera_height));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
video_writer.write(mat);
|
|
175
|
+
return Qtrue;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
static VALUE take_picture(VALUE self) { // self 引数を追加
|
|
179
|
+
bool ret = camera.read(frame);
|
|
180
|
+
if (!ret || frame.empty()) {
|
|
181
|
+
rb_raise(rb_eRuntimeError, "画像の取得に失敗しました。");
|
|
182
|
+
return Qfalse;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return mat2str(frame);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
static VALUE show_movie(VALUE self) {
|
|
189
|
+
cv::imshow("Camera", frame);
|
|
190
|
+
return Qtrue;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
static VALUE waitKey(int delay) {
|
|
194
|
+
return cv::waitKey(delay);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
static VALUE destroy_window(VALUE self) {
|
|
198
|
+
cv::destroyAllWindows();
|
|
199
|
+
return Qtrue;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
extern "C" {
|
|
203
|
+
static VALUE rb_open_mp4(VALUE self, VALUE path) {
|
|
204
|
+
return open_mp4_source(self, path);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
static VALUE rb_cam_open(VALUE self, VALUE source, VALUE width, VALUE height) {
|
|
208
|
+
return open_camera_source(self, source, width, height);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
static VALUE rb_get(VALUE self) {
|
|
212
|
+
return take_picture(self); // 引数の数を修正
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
static VALUE rb_start_recording(VALUE self, VALUE path, VALUE fps) {
|
|
216
|
+
return start_recording(self, path, fps);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
static VALUE rb_stop_recording(VALUE self) {
|
|
220
|
+
return stop_recording(self);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
static VALUE rb_write_frame(VALUE self, VALUE image_data) {
|
|
224
|
+
return write_frame(self, image_data);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
static VALUE rb_show_movie(VALUE self) {
|
|
228
|
+
return show_movie(self);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
static VALUE rb_wait_key(VALUE self, VALUE delay) {
|
|
232
|
+
return waitKey(NUM2INT(delay));
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
static VALUE rb_destroy_window(VALUE self) {
|
|
236
|
+
return destroy_window(self);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
static VALUE rb_set_frame_pos(VALUE self, VALUE pos) {
|
|
240
|
+
if (!camera.isOpened()) {
|
|
241
|
+
rb_raise(rb_eRuntimeError, "カメラまたは動画が開かれていません。");
|
|
242
|
+
return Qfalse;
|
|
243
|
+
}
|
|
244
|
+
int frame_idx = NUM2INT(pos);
|
|
245
|
+
bool ret = camera.set(cv::CAP_PROP_POS_FRAMES, frame_idx);
|
|
246
|
+
return ret ? Qtrue : Qfalse;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
static VALUE rb_get_frame_pos(VALUE self) {
|
|
250
|
+
if (!camera.isOpened()) {
|
|
251
|
+
return INT2NUM(-1);
|
|
252
|
+
}
|
|
253
|
+
double pos = camera.get(cv::CAP_PROP_POS_FRAMES);
|
|
254
|
+
return INT2NUM(static_cast<int>(pos));
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
static VALUE rb_get_total_frames(VALUE self) {
|
|
258
|
+
if (!camera.isOpened()) {
|
|
259
|
+
return INT2NUM(0);
|
|
260
|
+
}
|
|
261
|
+
double count = camera.get(cv::CAP_PROP_FRAME_COUNT);
|
|
262
|
+
return INT2NUM(static_cast<int>(count));
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
extern void Init_libopencv(void) {
|
|
266
|
+
VALUE libopencv = rb_define_module("Libopencv");
|
|
267
|
+
rb_define_singleton_method(libopencv, "cam_open", rb_cam_open, 3);
|
|
268
|
+
rb_define_singleton_method(libopencv, "open_mp4", rb_open_mp4, 1);
|
|
269
|
+
rb_define_singleton_method(libopencv, "start_recording", rb_start_recording, 2);
|
|
270
|
+
rb_define_singleton_method(libopencv, "take_picture", rb_get, 0);
|
|
271
|
+
rb_define_singleton_method(libopencv, "write_frame", rb_write_frame, 1);
|
|
272
|
+
rb_define_singleton_method(libopencv, "stop_recording", rb_stop_recording, 0);
|
|
273
|
+
rb_define_singleton_method(libopencv, "show_movie", rb_show_movie, 0);
|
|
274
|
+
rb_define_singleton_method(libopencv, "wait_key", rb_wait_key, 1);
|
|
275
|
+
rb_define_singleton_method(libopencv, "destroy_window", rb_destroy_window, 0);
|
|
276
|
+
rb_define_singleton_method(libopencv, "set_frame_pos", rb_set_frame_pos, 1);
|
|
277
|
+
rb_define_singleton_method(libopencv, "get_frame_pos", rb_get_frame_pos, 0);
|
|
278
|
+
rb_define_singleton_method(libopencv, "get_total_frames", rb_get_total_frames, 0);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'parquet'
|
|
5
|
+
|
|
6
|
+
module Lerobot
|
|
7
|
+
module Dataset
|
|
8
|
+
module Ruby
|
|
9
|
+
# LeRobotDatasetWriter が生成した v3 データセットを読むクラス。
|
|
10
|
+
class Reader
|
|
11
|
+
attr_reader :root, :info, :tasks
|
|
12
|
+
|
|
13
|
+
def initialize(root)
|
|
14
|
+
@root = File.expand_path(root)
|
|
15
|
+
@info = load_json('meta/info.json')
|
|
16
|
+
@tasks = load_tasks
|
|
17
|
+
@episodes = read_rows(episode_data_path)
|
|
18
|
+
@frames = read_rows(data_path)
|
|
19
|
+
validate_dataset
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def episode_count
|
|
23
|
+
@episodes.length
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def frame_count
|
|
27
|
+
@frames.length
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def episodes
|
|
31
|
+
@episodes.map { |episode| episode_metadata(episode) }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def episode(index)
|
|
35
|
+
episode = find_episode(index)
|
|
36
|
+
from_index = integer_value(episode['dataset_from_index'])
|
|
37
|
+
to_index = integer_value(episode['dataset_to_index'])
|
|
38
|
+
|
|
39
|
+
{
|
|
40
|
+
episode_index: integer_value(episode['episode_index']),
|
|
41
|
+
tasks: Array(episode['tasks']),
|
|
42
|
+
length: integer_value(episode['length']),
|
|
43
|
+
frames: @frames[from_index...to_index].map { |frame| normalize_frame(frame) }
|
|
44
|
+
}
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def each_episode
|
|
48
|
+
return enum_for(__method__) unless block_given?
|
|
49
|
+
|
|
50
|
+
@episodes.each do |episode|
|
|
51
|
+
yield episode_metadata(episode)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def each_frame(episode_index = nil)
|
|
56
|
+
return enum_for(__method__, episode_index) unless block_given?
|
|
57
|
+
|
|
58
|
+
selected = episode_index.nil? ? @frames : episode(episode_index)[:frames]
|
|
59
|
+
selected.each { |frame| yield frame }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# 指定エピソードを OpenCV ウィンドウで再生する。
|
|
63
|
+
# ブロックには [フレームメタデータ, JPEG バイト列] を渡す。
|
|
64
|
+
def play(episode_index, delay: nil)
|
|
65
|
+
begin
|
|
66
|
+
require 'libopencv'
|
|
67
|
+
rescue LoadError
|
|
68
|
+
require_relative '../../../../ext/opencv/libopencv'
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
episode_data = episode(episode_index)
|
|
72
|
+
video_path = File.expand_path(video_relative_path, @root)
|
|
73
|
+
Libopencv.open_mp4(video_path)
|
|
74
|
+
|
|
75
|
+
skip_frames(episode_data[:frames].first&.fetch(:frame_index, 0).to_i)
|
|
76
|
+
frame_delay = delay || (1000.0 / @info.fetch('fps', 30)).round
|
|
77
|
+
|
|
78
|
+
episode_data[:frames].each do |metadata|
|
|
79
|
+
image_bytes = Libopencv.take_picture
|
|
80
|
+
yield metadata, image_bytes if block_given?
|
|
81
|
+
Libopencv.show_movie
|
|
82
|
+
key = Libopencv.wait_key(frame_delay)
|
|
83
|
+
break if key == 27 || key == 113
|
|
84
|
+
end
|
|
85
|
+
ensure
|
|
86
|
+
Libopencv.destroy_window if defined?(Libopencv)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
private
|
|
90
|
+
|
|
91
|
+
def load_json(relative_path)
|
|
92
|
+
JSON.parse(File.read(File.join(@root, relative_path)))
|
|
93
|
+
rescue Errno::ENOENT => error
|
|
94
|
+
raise ArgumentError, "データセットのファイルが見つかりません: #{error.path}"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def load_tasks
|
|
98
|
+
parquet_path = File.join(@root, 'meta', 'tasks.parquet')
|
|
99
|
+
raise ArgumentError, "タスク定義が見つかりません: #{parquet_path}" unless File.file?(parquet_path)
|
|
100
|
+
|
|
101
|
+
Parquet.each_row(parquet_path).map do |row|
|
|
102
|
+
{ 'task_index' => row['task_index'], 'task' => row['task'] }
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def read_rows(relative_path)
|
|
107
|
+
path = File.join(@root, relative_path)
|
|
108
|
+
raise ArgumentError, "Parquet ファイルが見つかりません: #{path}" unless File.file?(path)
|
|
109
|
+
|
|
110
|
+
Parquet.each_row(path).to_a
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def data_path
|
|
114
|
+
@info.dig('data_files', 'data') || 'data/chunk-000/file-000.parquet'
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def episode_data_path
|
|
118
|
+
@info['episode_data_files'] || 'meta/episodes/chunk-000/file-000.parquet'
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def validate_dataset
|
|
122
|
+
declared_frames = @info['total_frames']
|
|
123
|
+
declared_episodes = @info['total_episodes']
|
|
124
|
+
unless declared_frames == @frames.length && declared_episodes == @episodes.length
|
|
125
|
+
raise ArgumentError, 'info.json と Parquet の件数が一致しません'
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
@episodes.each do |episode|
|
|
129
|
+
from_index = integer_value(episode['dataset_from_index'])
|
|
130
|
+
to_index = integer_value(episode['dataset_to_index'])
|
|
131
|
+
length = integer_value(episode['length'])
|
|
132
|
+
unless to_index - from_index == length && @frames[from_index...to_index]&.length == length
|
|
133
|
+
raise ArgumentError, "エピソード #{episode['episode_index']} の範囲が不正です"
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def find_episode(index)
|
|
139
|
+
episode = @episodes.find { |item| integer_value(item['episode_index']) == index }
|
|
140
|
+
raise IndexError, "エピソード #{index} が見つかりません" unless episode
|
|
141
|
+
|
|
142
|
+
episode
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def episode_metadata(episode)
|
|
146
|
+
episode.transform_keys(&:to_sym).merge(
|
|
147
|
+
episode_index: integer_value(episode['episode_index']),
|
|
148
|
+
length: integer_value(episode['length'])
|
|
149
|
+
)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def normalize_frame(frame)
|
|
153
|
+
frame.transform_keys(&:to_sym)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def integer_value(value)
|
|
157
|
+
Integer(value)
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def video_relative_path
|
|
161
|
+
@info.dig('data_files', 'videos', 'rgb') ||
|
|
162
|
+
'videos/rgb/chunk-000/file-000.mp4'
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def skip_frames(count)
|
|
166
|
+
count.times { Libopencv.take_picture }
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# 後方互換性のためのエイリアス
|
|
174
|
+
LeRobotDatasetReader = Lerobot::Dataset::Ruby::Reader
|