First time here? Check out the FAQ!

Ask Your Question
0

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

asked Oct 26 '17

ovg gravatar image

updated Oct 27 '17

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

Preview: (hide)

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 (Nov 3 '17)edit

2 answers

Sort by » oldest newest most voted
0

answered Oct 31 '17

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()

Preview: (hide)
0

answered Oct 27 '17

supra56 gravatar image
    cv2.imshow('img', img)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break
Preview: (hide)

Comments

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

supra56 gravatar imagesupra56 (Oct 27 '17)edit

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

ovg gravatar imageovg (Oct 27 '17)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 (Oct 29 '17)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 (Oct 29 '17)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 (Oct 29 '17)edit

@MachineLearner. Can u post code?

supra56 gravatar imagesupra56 (Oct 30 '17)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 (Nov 1 '17)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 (Nov 1 '17)edit

Question Tools

1 follower

Stats

Asked: Oct 26 '17

Seen: 8,219 times

Last updated: Oct 31 '17