-
Notifications
You must be signed in to change notification settings - Fork 4
chore: bump opencv-python to >=4.10.0 and add cv2 tests #274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0454c52
chore: bump opencv-python to >=4.10.0 and add cv2 tests
38c3ea8
ci: split lint/test jobs and run pytest on python 3.9/3.12
rikunosuke 30ef08f
ci: expand test matrix to python 3.8-3.14
rikunosuke 8e73695
ci: drop python 3.8 from test matrix
rikunosuke 819957f
ci: drop python 3.13 and 3.14 from test matrix
rikunosuke 60f5d87
chore: drop unused aiohttp dependency
rikunosuke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,14 +16,14 @@ dependencies = [ | |
| "geojson>=2.0.0,<4.0", | ||
| "xmltodict==0.12.0", | ||
| "Pillow>=10.0.0,<11.0.0", | ||
| "opencv-python>=4.0.0,<5.0.0", | ||
| "aiohttp>=3.8.5" | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aiohttp は使わなくなっていたので削除しています。 |
||
| "opencv-python>=4.10.0,<5.0.0" | ||
| ] | ||
|
|
||
| dynamic = ["version"] | ||
|
|
||
| [project.optional-dependencies] | ||
| robotics = ["pandas>=2.0.0", "pyarrow>=14.0.0"] | ||
| dev = ["pytest>=7.0.0"] | ||
|
|
||
| [tool.setuptools] | ||
| include-package-data = true | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| from pathlib import Path | ||
|
|
||
| import cv2 | ||
| import numpy as np | ||
| import pytest | ||
|
|
||
|
|
||
| def _write_synthetic_video( | ||
| path: Path, | ||
| num_frames: int = 10, | ||
| width: int = 64, | ||
| height: int = 48, | ||
| fps: int = 10, | ||
| fourcc_code: str = "mp4v", | ||
| ) -> Path: | ||
| fourcc = cv2.VideoWriter_fourcc(*fourcc_code) | ||
| writer = cv2.VideoWriter(str(path), fourcc, fps, (width, height)) | ||
| if not writer.isOpened(): | ||
| pytest.skip(f"cv2.VideoWriter could not open {path} with codec {fourcc_code}") | ||
| try: | ||
| for i in range(num_frames): | ||
| frame = np.full((height, width, 3), i * 20 % 255, dtype=np.uint8) | ||
| writer.write(frame) | ||
| finally: | ||
| writer.release() | ||
| if not path.exists() or path.stat().st_size == 0: | ||
| pytest.skip("Synthetic video could not be created (codec unavailable).") | ||
| return path | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def synthetic_video(tmp_path): | ||
| def _factory( | ||
| name: str = "video.mp4", | ||
| num_frames: int = 10, | ||
| width: int = 64, | ||
| height: int = 48, | ||
| fps: int = 10, | ||
| fourcc_code: str = "mp4v", | ||
| ) -> Path: | ||
| return _write_synthetic_video( | ||
| tmp_path / name, | ||
| num_frames=num_frames, | ||
| width=width, | ||
| height=height, | ||
| fps=fps, | ||
| fourcc_code=fourcc_code, | ||
| ) | ||
|
|
||
| return _factory |
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. converters.py の opencv 関連のロジックのテスト |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import os | ||
|
|
||
| import cv2 | ||
| import pytest | ||
|
|
||
| from fastlabel import converters | ||
| from fastlabel.exceptions import FastLabelInvalidException | ||
|
|
||
|
|
||
| class TestVideoCapture: | ||
| def test_yields_open_capture_and_releases(self, synthetic_video): | ||
| video_path = synthetic_video(name="sample.mp4", num_frames=5) | ||
|
|
||
| with converters.VideoCapture(str(video_path)) as cap: | ||
| assert cap.isOpened() | ||
| ret, frame = cap.read() | ||
| assert ret is True | ||
| assert frame is not None | ||
|
|
||
| def test_releases_capture_on_exit(self, synthetic_video): | ||
| video_path = synthetic_video(name="sample.mp4") | ||
|
|
||
| with converters.VideoCapture(str(video_path)) as cap: | ||
| captured = cap | ||
|
|
||
| # After release, reading should fail or return falsy ret | ||
| ret, _ = captured.read() | ||
| assert ret is False | ||
|
|
||
| def test_releases_capture_on_exception(self, synthetic_video): | ||
| video_path = synthetic_video(name="sample.mp4") | ||
|
|
||
| captured = None | ||
| with pytest.raises(RuntimeError): | ||
| with converters.VideoCapture(str(video_path)) as cap: | ||
| captured = cap | ||
| raise RuntimeError("boom") | ||
|
|
||
| ret, _ = captured.read() | ||
| assert ret is False | ||
|
|
||
|
|
||
| class TestExportImageFilesForVideoFile: | ||
| def test_writes_one_jpg_per_frame(self, synthetic_video, tmp_path): | ||
| num_frames = 7 | ||
| video_path = synthetic_video(name="sample.mp4", num_frames=num_frames) | ||
| output_dir = tmp_path / "frames" | ||
|
|
||
| names = converters._export_image_files_for_video_file( | ||
| file_path=str(video_path), | ||
| output_dir_path=str(output_dir), | ||
| basename="sample", | ||
| ) | ||
|
|
||
| assert len(names) == num_frames | ||
| for name in names: | ||
| assert name.endswith(".jpg") | ||
| assert name.startswith("sample_") | ||
| assert (output_dir / name).is_file() | ||
|
|
||
| def test_zero_padding_matches_total_frame_digits(self, synthetic_video, tmp_path): | ||
| num_frames = 12 | ||
| video_path = synthetic_video(name="sample.mp4", num_frames=num_frames) | ||
| output_dir = tmp_path / "frames" | ||
|
|
||
| names = converters._export_image_files_for_video_file( | ||
| file_path=str(video_path), | ||
| output_dir_path=str(output_dir), | ||
| basename="vid", | ||
| ) | ||
|
|
||
| # 12 frames -> 2 digit zero padding ("00".."11") | ||
| assert names[0] == "vid_00.jpg" | ||
| assert names[-1] == f"vid_{num_frames - 1:02d}.jpg" | ||
|
|
||
| def test_written_frames_are_readable_images(self, synthetic_video, tmp_path): | ||
| video_path = synthetic_video( | ||
| name="sample.mp4", num_frames=3, width=64, height=48 | ||
| ) | ||
| output_dir = tmp_path / "frames" | ||
|
|
||
| names = converters._export_image_files_for_video_file( | ||
| file_path=str(video_path), | ||
| output_dir_path=str(output_dir), | ||
| basename="frame", | ||
| ) | ||
|
|
||
| for name in names: | ||
| img = cv2.imread(str(output_dir / name)) | ||
| assert img is not None | ||
| assert img.shape == (48, 64, 3) | ||
|
|
||
| def test_unopenable_file_raises(self, tmp_path): | ||
| bogus = tmp_path / "not_a_video.mp4" | ||
| bogus.write_bytes(b"not a real video") | ||
|
|
||
| with pytest.raises(FastLabelInvalidException): | ||
| converters._export_image_files_for_video_file( | ||
| file_path=str(bogus), | ||
| output_dir_path=str(tmp_path / "frames"), | ||
| basename="x", | ||
| ) | ||
|
|
||
| def test_creates_output_directory_if_missing(self, synthetic_video, tmp_path): | ||
| video_path = synthetic_video(name="sample.mp4", num_frames=2) | ||
| output_dir = tmp_path / "does" / "not" / "exist" | ||
|
|
||
| assert not output_dir.exists() | ||
| converters._export_image_files_for_video_file( | ||
| file_path=str(video_path), | ||
| output_dir_path=str(output_dir), | ||
| basename="frame", | ||
| ) | ||
| assert output_dir.is_dir() | ||
| assert len(os.listdir(output_dir)) == 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import cv2 | ||
| import pytest | ||
|
|
||
| from fastlabel.exceptions import FastLabelInvalidException | ||
| from fastlabel.lerobot import v3 | ||
|
|
||
|
|
||
| class TestExtractVideoSegment: | ||
| def test_extracts_requested_number_of_frames(self, synthetic_video, tmp_path): | ||
| source = synthetic_video(name="src.mp4", num_frames=20, width=64, height=48) | ||
| output = tmp_path / "segment.mp4" | ||
|
|
||
| v3._extract_video_segment( | ||
| video_path=source, | ||
| start_frame=5, | ||
| num_frames=8, | ||
| output_path=output, | ||
| ) | ||
|
|
||
| assert output.is_file() | ||
| cap = cv2.VideoCapture(str(output)) | ||
| try: | ||
| count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) | ||
| width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) | ||
| height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) | ||
| finally: | ||
| cap.release() | ||
|
|
||
| assert count == 8 | ||
| assert (width, height) == (64, 48) | ||
|
|
||
| def test_stops_when_source_ends(self, synthetic_video, tmp_path): | ||
| source = synthetic_video(name="src.mp4", num_frames=10) | ||
| output = tmp_path / "segment.mp4" | ||
|
|
||
| v3._extract_video_segment( | ||
| video_path=source, | ||
| start_frame=8, | ||
| num_frames=50, | ||
| output_path=output, | ||
| ) | ||
|
|
||
| cap = cv2.VideoCapture(str(output)) | ||
| try: | ||
| count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) | ||
| finally: | ||
| cap.release() | ||
|
|
||
| assert count == 2 | ||
|
|
||
| def test_unopenable_file_raises(self, tmp_path): | ||
| bogus = tmp_path / "not_a_video.mp4" | ||
| bogus.write_bytes(b"garbage") | ||
|
|
||
| with pytest.raises(FastLabelInvalidException): | ||
| v3._extract_video_segment( | ||
| video_path=bogus, | ||
| start_frame=0, | ||
| num_frames=1, | ||
| output_path=tmp_path / "out.mp4", | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3.13/3.14 は Pillow のバージョンが低すぎてインストールできずにエラーとなるため、このPRには含めない