Ask Your Question
0

Python Opencv VideoCapture::read returns None only in a virtual environment

asked 2017-10-26 16:11:46 -0500

ovg gravatar image

updated 2017-10-27 06:15:04 -0500

The code below runs fine, except when it is put in my virtual env.

I've tried building opencv from source with a new env and no difference...

cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)

while True:
    ret, img = cap.read()
    print(ret)  # False in venv
    print(img)  # None in venv

    cv2.imshow('img', img)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

Why might this be happening? Can I get access to any logs (None isn't really helpful)

Is it possible that some of the scripts in the virtual env don't have the correct permissions for binaries in my opencv install?

Python 3.6.2, virtualenvwrapper, Fedora 25

edit retag flag offensive close merge delete

Comments

Ok, this thread got completely hijacked, but I solved my problem compiling the newest version from source ;) Turns out it was Python 3 as a whole that wasn't working...

ovg gravatar imageovg ( 2017-11-03 13:20:03 -0500 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2017-10-31 17:35:24 -0500

Supra56, thank you for the additional interest:

import cv2 import numpy as np

cap = cv2.VideoCapture(0) cv2.startWindowThread() while(1): _, frame = cap.read() cv2.imshow('frame',frame) k = cv2.waitKey(1) if k == 27: break

cap.release() cv2.destroyAllWindows()

The issue is I can not halt the video feed, must kill via closing the command line editor.

The second example must halt via closing the image, pressing keyboard has no effect.

import cv2 import numpy as np

img_rgb = cv2.imread('opencv-template-matching-python-tutorial.jpg') img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)

template = cv2.imread('opencv-template-for-matching.jpg',0) w, h = template.shape[::-1]

res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED) threshold = 0.8 loc = np.where( res >= threshold)

for pt in zip(*loc[::-1]): cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)

cv2.imshow('Detected',img_rgb) cv2.waitKey(0) cv2.destroyAllWindows()

edit flag offensive delete link more
0

answered 2017-10-26 22:10:26 -0500

supra56 gravatar image
    cv2.imshow('img', img)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break
edit flag offensive delete link more

Comments

Btw, If ur pc is 64-bit, u don't need 0xff

supra56 gravatar imagesupra56 ( 2017-10-26 22:12:11 -0500 )edit

cool, but not sure if this helps me...

ovg gravatar imageovg ( 2017-10-27 06:15:55 -0500 )edit

Thank you for your suggestion. This did not fix my issue - Using Windows 10, Python 3.6.3 OpenCV3, and suggestions?

MachineLearner gravatar imageMachineLearner ( 2017-10-28 22:19:28 -0500 )edit

@MachineLearner. Sorry, I didn't see your comment. What's OpenCV version you have? I'm using OpenCV 3.3.1

supra56 gravatar imagesupra56 ( 2017-10-29 09:56:18 -0500 )edit

Supra56, thank for replying - I have python 3.6.1 and opencv 3.3.1 loaded, windows 10. New to Python and OpenCV. I am having issues getting simple examples to work. I.e this thread for one example.

MachineLearner gravatar imageMachineLearner ( 2017-10-29 12:03:55 -0500 )edit

@MachineLearner. Can u post code?

supra56 gravatar imagesupra56 ( 2017-10-30 03:45:02 -0500 )edit

@MachineLearner. Sorry for delay, because of halloween. Don't cv2.startWindowThread() Never put inside looping. cap.release() cv2.destroyAllWindows()

here is code:

import cv2
cap = cv2.VideoCapture(0)
while(1):
    _, frame = cap.read()
    cv2.imshow('frame',frame)        
    k = cv2.waitKey(1)
    if k == 27: break

cap.release()
cv2.destroyAllWindows()
supra56 gravatar imagesupra56 ( 2017-11-01 08:20:19 -0500 )edit

For second code. The programming didn't added waikey(0) Here is code:

import cv2
import numpy as np

img_rgb = cv2.imread('opencv-template-matching-python-tutorial.jpg')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)

template = cv2.imread('opencv-template-for-matching.jpg',0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.7 
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
    cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,25), 2)

    cv2.imshow('Detected',img_rgb)
cv2.waitKey(0)
supra56 gravatar imagesupra56 ( 2017-11-01 08:34:21 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-10-26 16:11:46 -0500

Seen: 3,834 times

Last updated: Oct 31 '17