From f562287b14d6c2f0baf5fdbaf292deff7498e3f3 Mon Sep 17 00:00:00 2001 From: yzqin Date: Sat, 26 Aug 2023 17:36:10 -0700 Subject: [PATCH] [update] update video capture example and readme --- README.md | 17 ++++++++++++++--- example/capture_webcam.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 example/capture_webcam.py diff --git a/README.md b/README.md index 61dd73d..43f29de 100644 --- a/README.md +++ b/README.md @@ -22,9 +22,9 @@ pip3 install -e ".[example]" ## Examples -### Test retargeting with our supplied human video +### Retargeting from human video -1. **Generate the robot joint pose trajectory from a pre-existing video.** +1. **Generate the robot joint pose trajectory from our pre-recorded video.** ```shell python3 example/detect_from_video.py \ @@ -37,7 +37,9 @@ python3 example/detect_from_video.py \ This command will output the joint trajectory as a pickle file at the `output_path`. -The pickle file is a python dictionary with two keys: `meta_data` and `data`. `meta_data`, a dictionary, includes details about the robot, while `data`, a list, contains the robotic joint positions for each frame. For additional options, refer to the help information. +The pickle file is a python dictionary with two keys: `meta_data` and `data`. `meta_data`, a dictionary, includes +details about the robot, while `data`, a list, contains the robotic joint positions for each frame. For additional +options, refer to the help information. ```shell python3 example/detect_from_video.py --help @@ -53,3 +55,12 @@ python3 example/render_robot_hand.py \ ``` This command uses the data saved from the previous step to create a rendered video. + +3. **Record a video of your own hand** + +```bash +python3 example/capture_webcam.py --video-path example/data/my_human_hand_video.mp4 +``` + +This command will access your webcam (which should be connected to your computer) and record the video stream in mp4 +format. To end video recording, press `q` on the keyboard. diff --git a/example/capture_webcam.py b/example/capture_webcam.py new file mode 100644 index 0000000..9b42e65 --- /dev/null +++ b/example/capture_webcam.py @@ -0,0 +1,39 @@ +from pathlib import Path + +from typing import Union +import cv2 +import tyro + + +def main(video_path: str, video_capture_device: Union[str, int] = 0): + """ + Capture video with the camera connected to your computer. Press `q` to end the recording. + + Args: + video_path: The file path for the output video in .mp4 format. + video_capture_device: the device id for your camera connected to the computer in OpenCV format. + + """ + cap = cv2.VideoCapture(video_capture_device) + + width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) + height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) + + path = Path(video_path) + path.parent.mkdir(parents=True, exist_ok=True) + writer = cv2.VideoWriter(str(video_path), cv2.VideoWriter_fourcc(*"mp4v"), 30.0, (width, height)) + + while True: + ret, frame = cap.read() + writer.write(frame) + cv2.imshow("frame", frame) + if cv2.waitKey(1) & 0xFF == 27: + break + + cap.release() + writer.release() + cv2.destroyAllWindows() + + +if __name__ == "__main__": + tyro.cli(main)