Skip to content

Add camera-controlled Panda demo with YOLOv8 person detection#2

Open
ChadliaJerad wants to merge 10 commits into
lf-lang:mainfrom
ChadliaJerad:main
Open

Add camera-controlled Panda demo with YOLOv8 person detection#2
ChadliaJerad wants to merge 10 commits into
lf-lang:mainfrom
ChadliaJerad:main

Conversation

@ChadliaJerad

Copy link
Copy Markdown

This PR:

  • Adds PandaDemoCamCtrl.lf: Panda robot that pauses automatically when ≥2 people are detected by a YOLOv8 webcam feed, and resumes when the area is clear
  • Migrates video library from YOLOv5 to YOLOv8 (faster on CPU, simpler API)
  • Adds a dataset to PandaDemoParameters.csv with joint limits tuned to keep the arm above the floor
  • Updates source README and adds src/lib/video/* files with setup and run instructions

@hokeun hokeun requested a review from edwardalee June 16, 2026 20:02
@hokeun

hokeun commented Jun 16, 2026

Copy link
Copy Markdown
Member

@ChadliaJerad @edwardalee I was explaining this demo to my student today and noticed that this was not merged yet. Could we merge this PR into main soon?

@ChadliaJerad

Copy link
Copy Markdown
Author

Sure! How about Edward doing the review, me working on the comments he will give, and you proceeding with the merge?

@hokeun

hokeun commented Jun 16, 2026

Copy link
Copy Markdown
Member

Sure! How about Edward doing the review, me working on the comments he will give, and you proceeding with the merge?

Sounds good to me, thanks!

@edwardalee edwardalee left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am unable to get this working. See comments. I'm not crazy about the duplication of the YOLO code with the playground repo. It would be better to find a package manager solution so that both playground and this could import the video library. Or maybe this should go into the playground, which should import this mujoco-py library.

Comment thread src/lib/video/README.md
Comment on lines +11 to +21
**If you have an NVIDIA GPU**, select the correct CUDA version on the installation page and follow the generated command, for example:

```bash
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121
```

**If you have no GPU (CPU only)**, install the CPU builds explicitly. Using the generic PyPI packages will cause a version mismatch and a segfault at runtime:

```bash
pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
```

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not auto-detect whether there is a NVDIA GPU, Apple GPU, or CPU? Here is a possible template:

pip install torch torchvision

Then the following code will detect the device and use it in a test:

import torch

def get_compute_device() -> torch.device:
    """
    Detects and returns the best available computing device.
    Prioritizes CUDA, then Apple MPS, and falls back to CPU.
    """
    if torch.cuda.is_available():
        device_name = "cuda"
    elif torch.backends.mps.is_available():
        device_name = "mps"
    else:
        device_name = "cpu"
        
    print(f"🚀 Device selected: {device_name.upper()}")
    return torch.device(device_name)

if __name__ == "__main__":
    # 1. Initialize the device
    device = get_compute_device()
    
    # 2. Test tensor creation directly on the active device
    try:
        x = torch.randn(3, 3, device=device)
        y = torch.randn(3, 3, device=device)
        z = torch.matmul(x, y)
        
        print("\n✅ Verification Test Passed!")
        print(f"Matrix multiplication result shape: {z.shape}")
        print(f"Tensor is successfully hosted on: {z.device}")
        
    except Exception as e:
        print(f"\n❌ Error during tensor operations: {e}")

Comment thread src/lib/video/README.md
pip install -r requirements.txt
```

> **Note:** `requirements.txt` intentionally omits `torch` and `torchvision` so that the correct build variant (CPU or CUDA) can be chosen in the step above.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we auto-detect, then requirements.txt should include these.

Comment thread src/lib/video/README.md

```bash
lfc src/PandaDemoCamCtrl.lf
bin/PandaDemoCamCtrl

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a mac, I had to do this:

mjpython src-gen/PandaDemoCamCtrl/PandaDemoCamCtrl.py

Then, the MuJoCo window opens, but then I get this error:

ERROR: FATAL: Calling reaction _display.reaction_function_1 failed.
Traceback (most recent call last):
  File "/Users/edwardlee/git/Chadlia/mujoco-py/src-gen/PandaDemoCamCtrl/PandaDemoCamCtrl.py", line 575, in reaction_function_1
    cv2.imshow("frame", frame.value)
cv2.error: Unknown C++ exception from OpenCV code
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'API misuse: modification of a menu's items on a non-main thread when the menu is part of the main menu. Main menu contents may only be modified from the main thread.'

This error should be avoided by the single-threaded: true target directive, but perhaps there is some incompatibility with mjpython?

Comment on lines +4 to +12
# NOTE: torch and torchvision are NOT listed here.
# Install them separately BEFORE running pip install -r requirements.txt.
# See README.md for the correct command depending on whether you have a GPU.
#
# CPU (no GPU):
# pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
#
# NVIDIA GPU (example for CUDA 12.1):
# pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, why not auto-detect?

ipywidgets
ipython
ipykernel
matplotlib>=3.2.2

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you actually using matplotlib? This seems like a lot of requirements.

Comment thread src/lib/video/Video.lf
@@ -0,0 +1,96 @@
/**
* Copied from: https://github.com/lf-lang/playground-lingua-franca/blob/main/examples/Python/src/YOLOv5/Video.lf

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not update the existing example rather than copy it here?

@@ -0,0 +1,84 @@
/**
* Copied from: https://github.com/lf-lang/playground-lingua-franca/blob/main/examples/Python/src/YOLOv5/VideoAsync.lf

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I don't think we should copy this here.

Comment thread src/lib/MuJoCoBase.lf
Comment on lines +5 to +8
// If you are using `mjpython`, then uncomment the following lines
// cmake-args: {
// Python_EXECUTABLE: "mjpython"
// }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this may be connected with the error I'm seeing. However, if I uncomment it, it still fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants