Ask Your Question
0

VideoCapture freezes immediately after calling in code | Python 3

asked 2019-01-26 19:26:33 -0500

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.

edit retag flag offensive close merge delete

Comments

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

not what you want.

berak gravatar imageberak ( 2019-01-26 19:40:13 -0500 )edit

os ? opencv version ? how did you install it ?

berak gravatar imageberak ( 2019-01-26 20:09:11 -0500 )edit

Ubuntu 18.04 OpenCV 4.0.0 installed via pip

Deep Spacito gravatar imageDeep Spacito ( 2019-01-26 20:26:15 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-01-27 10:08:07 -0500

supra56 gravatar image

updated 2019-01-27 21:20:27 -0500

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
edit flag offensive delete link more

Comments

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

supra56 gravatar imagesupra56 ( 2019-01-27 11:39:16 -0500 )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 ( 2019-01-27 17:28:33 -0500 )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 ( 2019-01-27 21:23:26 -0500 )edit

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

supra56 gravatar imagesupra56 ( 2019-01-28 08:14:54 -0500 )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 ( 2019-01-28 11:25:28 -0500 )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 ( 2019-01-28 11:28:01 -0500 )edit

Which link did you downloaded opencv with unbuntu?

supra56 gravatar imagesupra56 ( 2019-01-28 13:10:00 -0500 )edit
supra56 gravatar imagesupra56 ( 2019-01-28 13:12:18 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-01-26 19:26:33 -0500

Seen: 122 times

Last updated: Jan 27