简体   繁体   中英

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?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM