How could I connect the webcam?
I'd like to connect the webcam to process its feed. I've tried to process images rather than a video and it worked, but I struggle with the connection of the webcam, I'm not that familiar with Unity or Sentis.
Hello srsrgio.
If you want to process input from the camera instead of processing a video clip, you can achieve this by making the following minimal changes to the provided script:
- Declare a
WebCamTexture
field (nearby the originally declaredVideoPlayer
):
private WebCamTexture webcamTexture;
- In the
SetupInput()
function start the camera:
webcamTexture = new WebCamTexture();
webcamTexture.Play();
- In the
ExecuteML()
function blit thewebcamTexture
instead of thevideo.texture
:
Graphics.Blit(webcamTexture, targetRT, new Vector2(1f / aspect, 1), new Vector2(0, 0));
Now when you start the game the camera input should appear instead of the previously displayed video clip (assuming that Unity has sufficient permissions set in the operating system to access the camera).
Hi Alex, thanks for such a quick reply.
I've tried what you mentioned, as you can see in my code, but I get a screen view rather than the webcam view, as if I was screen recording rather than getting the feed from my webcam, even so, the small led next to my webcam turns on...
I couldn't attach the whole code so I uploaded to drive: https://drive.google.com/file/d/1o4iwqOXz7gjKVjeCUtHMxok5qTkyEHu5/view?usp=sharing
Thanks in advance.
Hello srsrgio.
You are almost there, just a few more things to make it work:
- You still need to change the texture you blit from, as described in the point
3
above. Specifically at line192
:
Graphics.Blit(webcamTexture, targetRT, new Vector2(1f / aspect, 1), new Vector2(0, 0));
while your script still uses video.texture
as the source texture.
- You can take the aspect ratio directly from the camera texture instead of the video texture (I forgot to mention it above because the script would still work somehow if any video was loaded). Specifically at line
191
:
float aspect = webcamTexture.width * 1f / webcamTexture.height;
After these changes your camera code should work properly.
Additional notes:
- As you are using the first available camera, you don't need to explicitly specify the device name, and the default constructor mentioned above should be sufficient:
webcamTexture = new WebCamTexture();
- The
Graphics.Blit
parameters in the provided script are picked solely for demonstration purposes. You might want to pad/crop/mirror the camera texture according to your use case, just keep in mind that YOLO input texture is supposed to be square.