OpenCV cannot read from my VGA Webcam

asked 2020-10-30 17:05:24 -0600

Ok I will crack down on the issue fast, as I don't want waste anyone's time:

I have a laptop MODEL: ACER ASPIRE 3(A315-42) with a VGA Camera built into it. Here is the simple code I'm trying to run(in python)

import cv2
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
print(ret)

it seems to only return False.

Now if you want a little more detail I ran another program.

import cv2
import numpy as np

cap = cv2.VideoCapture(0)
while(True):
    ret, frame = cap.read()
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

I receive this error cv2.error: C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:281: error: (-215) size.width>0 && size.height>0 in function cv::imshow

Unfortunately I'm not too great with OpenCV just begun today, so if anyone can help me out I would be so happy as this is required for a project I'm working on.

Thank you to whom who answers to this in advance.

edit retag flag offensive close merge delete

Comments

try another backend, like

cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)

(default is CAP_MSMF, which has some warts)

berak gravatar imageberak ( 2020-10-31 02:56:47 -0600 )edit

@berak No change, the first one still returns False and the second code still spits out cv2.error. Code after change:

import cv2
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
#Tried CAP_MSMF just to make sure on both codes
ret, frame = cap.read()
print(ret)

Further information I went ahead and used OpenCV version 3.1.0.5 as I heard it brings a prompt for the user to select which device. It seems to be able to detect the device just cant read from it. Hope this helps.

YouNoNo gravatar imageYouNoNo ( 2020-10-31 04:12:33 -0600 )edit

Could there be a camera setting restriction or a driver issue? I just checked online for drivers for the webcam in Device Manager as far as it seems its the latest model. I'm out of ideas why this issue will occur.

YouNoNo gravatar imageYouNoNo ( 2020-10-31 04:33:10 -0600 )edit

btw, os ? opencv version ? where's the cv2 from ?

as I heard it brings a prompt for the user to select which device.

that's the old VFW api, but neither opencv nor ms supports it anymore

berak gravatar imageberak ( 2020-10-31 04:33:22 -0600 )edit

btw, almost same machine here (aspire es1-311) and the DSHOW backend runs nicely on win7/win10

reinstall drivers, i guess ... ;(

berak gravatar imageberak ( 2020-10-31 04:36:11 -0600 )edit
1

@berak I'm on the Windows 10 operating system, with the latest OpenCV version. I had only downgraded to 3.1.0.5, to see if OpenCV was able to detect the webcam in the first place.

YouNoNo gravatar imageYouNoNo ( 2020-10-31 04:37:28 -0600 )edit

@berak how would i go about reinstalling my webcam drivers? Do you have a source on which you refer me to.

YouNoNo gravatar imageYouNoNo ( 2020-10-31 05:14:48 -0600 )edit
1

@berak cv2 was installed via cmd line using command "pip install opencv-python".

YouNoNo gravatar imageYouNoNo ( 2020-10-31 07:32:56 -0600 )edit

^^ yea, ok. nothing unusual (we could pick on), unfortunately ;(

berak gravatar imageberak ( 2020-10-31 08:07:43 -0600 )edit

@berak I have reinstalled my webcam drivers and this has not fixed anything. I'm sorry to have bothered you but is there other ways in which I can get my PC via code to get just one frame from a USB camera? If I can at least get one frame I can develop a loop for my project. Which I want to connect 2 webcams to a computer at my workplace these cameras need to get a frame every second or two seconds, i was planning to use pyzbar to scan barcodes and collect data. But this one barrier is preventing me. I wonder if i got external webcams(connected via USB) will it work? If I got frame from each camera every second.

YouNoNo gravatar imageYouNoNo ( 2020-10-31 08:28:50 -0600 )edit