-
Notifications
You must be signed in to change notification settings - Fork 24
Renderer add recording / video export #25 #71
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
Open
alislaiman496
wants to merge
11
commits into
rp-itmo:master
Choose a base branch
from
alislaiman496:Renderer-Add-Recording-/-Video-Export_#25
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c490bb1
feat: create video_recorder.py in src/simulator and add .gitignore
alislaiman496 ddca0b3
feat(simulator): add VideoRecorder class with factory function
alislaiman496 9ef086e
feat: Add Coordinates physics and rendering for World class
alislaiman496 e016194
feat: Integrate video recording into renderer
alislaiman496 9069603
feat: Integrate video recording into renderer
alislaiman496 afa166e
fix: update simulator main entry point
EAhmadAhmad 2bfd1d5
fix: correct simulator package imports and dynamics scalar output
EAhmadAhmad 7ab19a0
fix: Add matplotlib renderer and update the main and se and ab_algori…
alislaiman496 3298d1e
fix: Move rendering parameters to Renderer
alislaiman496 211b08b
fix: Move rendering parameters to Renderer
alislaiman496 4c2a948
feat: add REAFME.md file
alislaiman496 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,51 @@ | ||
| *__pycache__* | ||
| *.pyc | ||
| *.pyc | ||
| # VS Code files | ||
| .vscode/ | ||
| *.code-workspace | ||
| .vscode-test/ | ||
|
|
||
| # Python environment | ||
| venv/ | ||
| env/ | ||
| ENV/ | ||
| .venv/ | ||
| virtualenv/ | ||
| env.bak/ | ||
| venv.bak/ | ||
|
|
||
| # Python virtual environment files | ||
| lib/ | ||
| include/ | ||
| bin/ | ||
| share/ | ||
| pyvenv.cfg | ||
| pip-log.txt | ||
| pip-delete-this-directory.txt | ||
|
|
||
| # Log files | ||
| *.log | ||
| logs/ | ||
| *.log.* | ||
| log.txt | ||
| debug.log | ||
| error.log | ||
|
|
||
| # OS generated files | ||
| .DS_Store | ||
| .DS_Store? | ||
| ._* | ||
| .Spotlight-V100 | ||
| .Trashes | ||
| ehthumbs.db | ||
| Thumbs.db | ||
| *.swp | ||
| *.swo | ||
|
|
||
| # Python cache | ||
| __pycache__/ | ||
| *.py[cod] | ||
| *$py.class | ||
| *.so | ||
| .Python | ||
|
|
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,149 @@ | ||
| # Robot Simulator | ||
|
|
||
| A Python-based robot arm simulator with video recording capability. | ||
|
|
||
|
|
||
|
|
||
| ## Problem: How to Record Robot Simulations? | ||
|
|
||
| ### The Challenge | ||
|
|
||
| We needed to add video recording capability to a matplotlib-based robot simulator. The requirement was to: | ||
| 1. Capture each frame of the simulation | ||
| 2. Save it as a video file (MP4/AVI) | ||
| 3. Use OpenCV for future extensibility (robotics + AI projects) | ||
|
|
||
| ### Our Solution | ||
|
|
||
| We used **OpenCV** (`cv2.VideoWriter`) to capture and encode video frames: | ||
|
|
||
| 1. **Frame Capture**: After matplotlib renders each frame, we capture it from the canvas buffer | ||
| 2. **Color Conversion**: matplotlib gives RGBA format, OpenCV needs BGR - we convert using `cv2.cvtColor()` | ||
| 3. **Video Encoding**: OpenCV's `VideoWriter` handles MP4/AVI encoding efficiently | ||
| 4. **Auto Folder**: Recordings automatically save to `recording_simulation/` folder | ||
|
|
||
| ### Why OpenCV Instead of imageio? | ||
|
|
||
| | Feature | OpenCV | imageio | | ||
| |---------|--------|---------| | ||
| | Computer Vision | ✅ Yes | ❌ No | | ||
| | Real-time camera support | ✅ Yes | ❌ No | | ||
| | ML/AI integration | ✅ Yes | ❌ No | | ||
| | Industry standard for robotics | ✅ Yes | ❌ No | | ||
|
|
||
| OpenCV is the better choice for robotics + AI projects since you may later want to: | ||
| - Process video with ML models | ||
| - Add real camera feeds | ||
| - Do object detection (YOLO, etc.) | ||
|
|
||
| ## Files Added/Modified | ||
|
|
||
| ### New Files | ||
| - **`src/simulator/video_recorder.py`** - OpenCV-based video recording class | ||
| - **`src/simulator/spatial/__init__.py`** - Module initialization for relative imports | ||
|
|
||
| ### Modified Files | ||
| - **`src/simulator/renderer.py`** - Added frame capture and recording to `update()` method | ||
| - **`src/simulator/main.py`** - Added CLI arguments for recording control | ||
| - **`src/simulator/world.py`** - Simplified recording setup | ||
| - **`src/simulator/dynamics/ab_algorithm.py`** - Fixed scalar extraction bug | ||
| - **`pyproject.toml`** - Added `opencv-python` dependency | ||
|
|
||
| ## How to Run | ||
|
|
||
| ### Prerequisites | ||
|
|
||
| Install dependencies: | ||
| ```bash | ||
| pip install opencv-python matplotlib scipy numpy | ||
| ``` | ||
|
|
||
| ### Basic Usage | ||
|
|
||
| ```bash | ||
| # Run from the simulator-master folder | ||
| cd simulator-master | ||
|
|
||
| # Run simulation with default robot (CartPole) | ||
| python src/simulator/main.py --steps 300 | ||
|
|
||
| # Record to video (saved in recording_simulation/ folder) | ||
| python src/simulator/main.py --record my_robot.mp4 --steps 200 --fps 15 | ||
| ``` | ||
|
|
||
| ### Command Line Options | ||
|
|
||
| | Option | Description | Default | | ||
| |--------|-------------|---------| | ||
| | `--robot` | Robot type: `cartpole`, `two-link`, `tree7`, `robot-tree` | `cartpole` | | ||
| | `--steps` | Number of simulation steps | `1000` | | ||
| | `--record` | Output filename for recording | (no recording) | | ||
| | `--fps` | Frames per second for video | `30.0` | | ||
| | `--format` | Video format: `mp4`, `avi` | (auto-detected) | | ||
|
|
||
| ### Examples | ||
|
|
||
| ```bash | ||
| # Record CartPole (default robot) | ||
| python src/simulator/main.py --record cartpole_demo.mp4 --steps 300 --fps 20 | ||
|
|
||
| # Record Two-Link robot | ||
| python src/simulator/main.py --robot two-link --record two_link.mp4 --steps 200 | ||
|
|
||
| # Record Tree7 robot with custom FPS | ||
| python src/simulator/main.py --robot tree7 --record tree7_sim.mp4 --fps 30 | ||
|
|
||
| # Record with custom path | ||
| python src/simulator/main.py --record /path/to/my_video.mp4 --steps 150 | ||
| ``` | ||
|
|
||
| ## Output Location | ||
|
|
||
| Videos are saved in the `recording_simulation/` folder (automatically created): | ||
|
|
||
| ``` | ||
| TEAM-005/ | ||
| ├── recording_simulation/ | ||
| │ ├── my_robot.mp4 | ||
| │ ├── cartpole_demo.mp4 | ||
| │ └── tree7_sim.mp4 | ||
| └── ... | ||
| ``` | ||
|
|
||
| ## Robot Models Available | ||
|
|
||
| 1. **CartPole** - Simple pole on a cart (default) | ||
| 2. **TwoLink** - Two-segment arm | ||
| 3. **Tree7** - 7-DOF tree structure | ||
| 4. **RobotTree** - Custom tree with configurable DOF | ||
|
|
||
| ## Technical Details | ||
|
|
||
| ### Dynamics Computation | ||
| - Uses **Articulated Body Algorithm (ABA)** from Featherstone's Rigid Body Dynamics | ||
| - Integrates equations of motion using `scipy.integrate.solve_ivp` | ||
| - Supports gravity, joint torques, and external forces | ||
|
|
||
| ### Rendering | ||
| - Matplotlib-based 2D visualization | ||
| - LineCollection for efficient link drawing | ||
| - Scattered points for joints | ||
| - World frame axes displayed (red=X, green=Y) | ||
|
|
||
| ### Recording Process | ||
| 1. Each simulation step renders the robot | ||
| 2. `renderer.update()` captures the canvas as RGBA numpy array | ||
| 3. `VideoRecorder.add_frame()` converts RGBA→BGR and writes to file | ||
| 4. On completion, `VideoRecorder.stop()` finalizes the video file | ||
|
|
||
| ## Future Extensions | ||
|
|
||
| With OpenCV installed, you can now: | ||
| - Add real camera feeds to the simulation | ||
| - Implement ML-based controllers | ||
| - Use computer vision for feedback control | ||
| - Integrate object detection for target tracking | ||
| - Add image processing pipelines | ||
|
|
||
|
|
||
|
|
Binary file not shown.
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
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.
Uh oh!
There was an error while loading. Please reload this page.