Skip to content
Advertisement

Opencv-Overlay a Video on Webcam

Guys I am having problem on overlaying my video to webcam. I can open webcam without any issues or errors but my video is not being showed. I am trying to play my video in specific x-y coordinates.I take most of my code from an other stackoverflow question but I cannot find it know so that I cannot mention it here.

So can someone help me to solve this? Why my video is not being played in my webcam? I have following code:

JavaScript

Advertisement

Answer

Let me start analyzing the code step-by-step.

  • Step #1
JavaScript

The above code look fine, but it would be better if you give as a string name

JavaScript
  • Step #2
JavaScript

Now what are width and height variables?

JavaScript

Result is:

JavaScript

It seems you want to set width and height to the dimension (150, 150). It would be better if you initialize them separately

JavaScript
  • Step #3
JavaScript

Why do you initialize cap variable two-times?

  • Step #4
JavaScript

Why do you initialize frame_vid you did not use anywhere in the code?

  • Step #5
JavaScript

The above code is not making any sense, you want to display your video as long as your webcam open. You also did not check whether the current webcam frame returns or not. You also set VideoCapture variable to the array?

JavaScript

Now you are getting frames, as long as your webcam is open, then you need to check whether the webcam frame returns. If the webcam frame returns then you need to start reading the video frames. If the video frame returns successfully resize the video frame to (width, height) then set it to the frame.

JavaScript
  • Step #6

Make sure close img variable after the execution.

JavaScript

Please change img variable to something that makes sense. Like rename the img variable to video_capture and cap to the webcam_capture.

When video stops then webcam stacks. But I want to continue infinitive. and video should start again. But video does not starts from beggining.and webcam freezes

Update


This issue was mentioned in the Playback loop option in OpenCV videos

If you look at the answer, the problem was solved by counting the video frames. When video frames equal to the capture frame count (CAP_PROP_FRAME_COUNT) set to counter and CAP_PROP_FRAME_COUNT to 0.

First initialize the frame counter.

JavaScript

and when webcam opens, get the frame. If frame returns, increase the counter by 1.

JavaScript

If counter equals to the capture class frame count, then initialize both variable to 0.

JavaScript

Code:


JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement