Skip to content
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

How to remove green background from video? #6

Open
SandhyaSharmaaa opened this issue May 30, 2024 · 2 comments
Open

How to remove green background from video? #6

SandhyaSharmaaa opened this issue May 30, 2024 · 2 comments

Comments

@SandhyaSharmaaa
Copy link

SandhyaSharmaaa commented May 30, 2024

I am trying to implement heygen react SDK, is there any option to remove green background from the avatar streaming
Here is my code to start the streaming session:
await avatar.current.createStartAvatar( { newSessionRequest: { quality: "low", avatarName: avatarId, voice: { voiceId: voiceId }, }, }, setDebug );
I tried the options given here, but none of them works: https://docs.heygen.com/docs/customize-video-background

@Yashism
Copy link

Yashism commented Jun 17, 2024

Nothing yet. Just follow this discussion I guess.

Discussion 7

@24samj
Copy link

24samj commented Jun 20, 2024

Yes, use a canvas to paint the video stream to and use a chroma-key function to remove the green background.
You can code it as follows:

`useEffect(() => {
const ctx = canvasRef.current.getContext("2d", {
willReadFrequently: true,
});

const renderFrame = () => {
  // Set the canvas dimensions to match the video's natural dimensions
  if (canvasRef.current && videoRef.current) {
    canvasRef.current.width = videoRef.current.videoWidth;
    canvasRef.current.height = videoRef.current.videoHeight;
  }

  ctx.drawImage(
    videoRef.current,
    0,
    0,
    canvasRef.current.width,
    canvasRef.current.height
  );
  let frame = ctx.getImageData(
    0,
    0,
    canvasRef.current.width,
    canvasRef.current.height
  );
  let l = frame.data.length / 4;

  for (let i = 0; i < l; i++) {
    let r = frame.data[i * 4 + 0];
    let g = frame.data[i * 4 + 1];
    let b = frame.data[i * 4 + 2];

    if (g > 90 && r < 90 && b < 90) {
      // adjust this condition to match your green color
      frame.data[i * 4 + 3] = 0;
    }
  }
  ctx.putImageData(frame, 0, 0);
  requestAnimationFrame(renderFrame);
};
videoRef.current.addEventListener("loadedmetadata", () => {
  // Metadata has loaded, now we can render the frame
  renderFrame();
});

}, [videoRef, canvasRef]);`

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

No branches or pull requests

3 participants