1

我正在尝试连接到我的笔记本电脑摄像头作为实时流媒体,但它不起作用。cv2.imshow() 引发错误。我在 Windows 10 上使用 Python 3.6 和 OpenCV 4.1.0。

我试图重建库 GTK+ 2.x 但没有任何改变。

import cv2

cap = cv2.VideoCapture(0)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

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

这是错误:

Traceback (most recent call last):
  File "C:/Users/hiba/PycharmProjects/Python for CV/12-Connecting_to_Camera.py", line 21, in <module>
    cv2.imshow('frame', gray)
cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:627: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'

[ WARN:0] terminating async callback

Process finished with exit code 1
4

1 回答 1

1
pip install opencv-contrib-python
于 2020-12-02T14:04:21.793 回答