System params: Windows 8.1, Python 2.7.13, OpenCV 3.2.0.7
This code works fine. I tried .avi and .mp4 videos:
import numpy as np
import cv2
import time
cap = cv2.VideoCapture('tmp.avi')
while(True):
ret, frame = cap.read()
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
But when I try to get picture from web camera I always see black screen though ret is True:
import numpy as np
import cv2
import time
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
if not ret: continue
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
I've tried to use grab and retrieve methods instead of read method, so grab returns True, but retrieve returns ret=False:
import numpy as np
import cv2
import time
cap = cv2.VideoCapture(0)
while(True):
if not cap.grab(): break
ret, frame = cap.retrieve()
if not ret: continue
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
Last code works fine for videos.
Windows Camera soft works fine, so webcam is OK.
I've tried to reinstall OpenCV, it didn't help.
What is the problem? Why retrieve method returns False though read method returns True?
I also had this problem today on windows using gocv.io library.
I removed and reinstalled the webcam driver and the problem solved.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.