First time here? Check out the FAQ!

Ask Your Question
0

VideoCapture freezes immediately after calling in code | Python 3

asked Jan 27 '19

I'm trying to simply open up a USB Camera but cannot for the life of me figure out what in the world is going on. It seems to be freezing immediately after calling VideoCapture(1)

import numpy as np
import cv2

print(1)
cap = cv2.VideoCapture(1) #This is opening the camera stream and assigning it to cap

while (True):
    print(2)
    ret, frame = cap.read() #Assigning ret and frame
    print(3)
    if not ret:
        break
    print(4)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #Changing caps color
    print(5)

    cv2.imgshow('frame', gray) #Display it
    print(6)
    if cv2.waitKey(30) & 0xFF == ord('1'):
        print(7)
        break

cap.release()
print(8)
cv2.destroyAllWindows()
print(9)

I've tried changing

cv2.waitKey(30)

to

cv2.waitKey(0)

and

cv2.waitKey(1)

none of which had any sort of success

I'm pretty sure any more of this will cause me to rip my hair out, thanks for any help.

Preview: (hide)

Comments

waitKey(0) or waitKey() will wait "forever".

not what you want.

berak gravatar imageberak (Jan 27 '19)edit

os ? opencv version ? how did you install it ?

berak gravatar imageberak (Jan 27 '19)edit

Ubuntu 18.04 OpenCV 4.0.0 installed via pip

Deep Spacito gravatar imageDeep Spacito (Jan 27 '19)edit

1 answer

Sort by » oldest newest most voted
0

answered Jan 27 '19

supra56 gravatar image

updated Jan 28 '19

There is nothing wrong with ur code. I tested raspberry pi 3B+ using linux. Also typo error too: change this:

cv2.imgshow('frame', gray) #Display it
to 
cv2.imshow('frame', gray) #Display it
Preview: (hide)

Comments

You can add this cv2.imshow('frame', frame) #Display it

supra56 gravatar imagesupra56 (Jan 27 '19)edit

For some reason it doesn't work on 0

VIDEOIO ERROR: V4L: can't open camera by index 0

Only has any sort of change on 1, also when it hangs

Deep Spacito gravatar imageDeep Spacito (Jan 27 '19)edit

I don't get any error such as VIDEOIO ERROR: V4L: can't open camera by index 0. Did you changed to cv2.imshow?

supra56 gravatar imagesupra56 (Jan 28 '19)edit

I modified it and it worked. Just an minor changed.

supra56 gravatar imagesupra56 (Jan 28 '19)edit

Hmm, that's weird, still not getting any success on my end, which leads me to believe it is something to do with my installation. Is it of concern that all uses of cv2.<anything> in my code shows up as an error with the linter saying that module doesn't exist?

Deep Spacito gravatar imageDeep Spacito (Jan 28 '19)edit

@supra56 I tried copy and pasting the code here: https://docs.opencv.org/3.0-beta/doc/...

With no success, getting the same can't open camera by index

Deep Spacito gravatar imageDeep Spacito (Jan 28 '19)edit

Which link did you downloaded opencv with unbuntu?

supra56 gravatar imagesupra56 (Jan 28 '19)edit
supra56 gravatar imagesupra56 (Jan 28 '19)edit

Question Tools

1 follower

Stats

Asked: Jan 27 '19

Seen: 4,643 times

Last updated: Jan 27 '19