0

My issue: I am trying to display an external (USB) webcam using OpenCV. However, it breaks a lot. The internal webcam works great with my code and is not laggy at all. However, the webcam is super laggy.

Here is my code:

import cv2

cv2.namedWindow("Camera")
vc = cv2.VideoCapture(1) #VideoCapture(0) uses the internal webcam, VideoCapture(1) uses external webcam

if vc.isOpened(): # try to get the first frame
    rval, frame = vc.read()
    #rval2, frame2 = vc2.read()
else:
    rval = False
    #rval2 = False

while rval:
    frame = cv2.resize(frame, (400,480))
    cv2.imshow("Camera", frame)
    rval, frame = vc.read()
    key = cv2.waitKey(20)
    if key == 27: # exit on hitting ESC
        break

cv2.destroyWindow("Camera")
vc.release()

"Is the webcam itself laggy?" No: I tried to run the external webcam in OBS to see if it would work, and it performed very well. There was no lag whatsoever.

What could be the problem? Does opencv just not perform well with usb cameras? Is there a fix?

Thank you.

  • See [this question](https://stackoverflow.com/a/70253370/8510370), maybe you need to use a different USB port. – Serzhan Akhmetov Oct 21 '22 at 15:24
  • I tried this to no avail. Thank you, though. The camera does work and connect, it is just terribly choppy. – theblum Oct 21 '22 at 15:39
  • Too bad, among other ways to troubleshoot, try 1) a different USB cam; 2) an older (or newer) version of `cv2` package - maybe it's a codec issue; 3) restart PC; 4) try on a linux or wsl2 environment / clean virtualenv; 5) disable the built-in cam in bios. – Serzhan Akhmetov Oct 21 '22 at 15:52
  • Tried these. My only other USB cam does not even work with opencv for some reason. Not sure what the issue could be since this specific camera works great when not being used by opencv. I did test it a bit more with frame rate. It said its 30fps and stable, but i notice that whenever the camera goes choppy, the constant frame rate count also stops for about a quarter of a second. Any ideas? – theblum Oct 21 '22 at 23:05

0 Answers0