Python OpenCV VideoCapture won't show webcam images
I'm working with Python OpenCV and want to capture webcam images to a frame, but the output was always showing this instead of my webcam images.
The script is as shown below:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Does anyone know the problem? Thanks.
Add a line to write a frame image to a file, JPG or PNG. It works in my PC.
if cv2.waitKey(1) & 0xFF == ord('q'):
cv2.imwrite('capframe.jpg',frame)
break
Did you verify if the image capture of your webcam works by its or other software?
链接地址: http://www.djcxy.com/p/80984.html