Linux 中的 OpenCV 3.1.0 imshow 不适用于网络摄像头 (Python)
OpenCV 3.1.0 imshow in Linux does not work for webcam (Python)
我正在尝试使用官方 openCV 教程中的代码在 Ubuntu/Python 3.6:
中使用 cv2.imshow()
显示来自网络摄像头的视频
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
cv2.imshow()
出现以下错误:
The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon 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
在搜索错误时,我偶然发现了这个 post 作为类似问题的替代答案:
If you installed OpenCV using the opencv-python pip package at any point in time, be aware of the following note, taken from https://pypi.python.org/pypi/opencv-python
IMPORTANT NOTE MacOS and Linux wheels have currently some limitations:
- video related functionality is not supported (not compiled with FFmpeg)
- for example cv2.imshow() will not work (not compiled with GTK+ 2.x or Carbon support)
另请注意,要从其他来源安装,首先必须删除 opencv-python 包
OpenCV error: the function is not implemented
大多数其他 openCV 函数都可以正常工作。
是否有使用标准 anaconda 库的 cv2.imshow()
替代方案,这样我就不必重新编译 openCV 或使用 Python 2.7?
我拼凑了一个使用 matplotlib.animation
的快速而肮脏的片段,类似于 cv2.imshow() 预期对网络摄像头视频所做的事情:
import cv2
import matplotlib.pyplot as plt
import matplotlib.animation as animation
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
# The following is the replacement for cv2.imshow():
fig = plt.figure()
ax = fig.add_subplot(111)
im = ax.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), animated=True)
def updatefig(*args):
ret, frame = cap.read()
im.set_array(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
return im
ani = animation.FuncAnimation(fig, updatefig, interval=10)
plt.show()
我发现这非常有用,因为 matplotlib.animation.FuncAnimation
可以为附加到 ax
对象,只要它们在上面的 updatefig()
函数中更新即可。
编辑:当我在 Jupyter 笔记本上工作时,我添加了以下内容以便在新 window 中观看视频:
import matplotlib
matplotlib.use('qt5agg')
我正在尝试使用官方 openCV 教程中的代码在 Ubuntu/Python 3.6:
中使用cv2.imshow()
显示来自网络摄像头的视频
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
cv2.imshow()
出现以下错误:
The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon 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
在搜索错误时,我偶然发现了这个 post 作为类似问题的替代答案:
If you installed OpenCV using the opencv-python pip package at any point in time, be aware of the following note, taken from https://pypi.python.org/pypi/opencv-python
IMPORTANT NOTE MacOS and Linux wheels have currently some limitations:
- video related functionality is not supported (not compiled with FFmpeg)
- for example cv2.imshow() will not work (not compiled with GTK+ 2.x or Carbon support)
另请注意,要从其他来源安装,首先必须删除 opencv-python 包
OpenCV error: the function is not implemented
大多数其他 openCV 函数都可以正常工作。
是否有使用标准 anaconda 库的 cv2.imshow()
替代方案,这样我就不必重新编译 openCV 或使用 Python 2.7?
我拼凑了一个使用 matplotlib.animation
的快速而肮脏的片段,类似于 cv2.imshow() 预期对网络摄像头视频所做的事情:
import cv2
import matplotlib.pyplot as plt
import matplotlib.animation as animation
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
# The following is the replacement for cv2.imshow():
fig = plt.figure()
ax = fig.add_subplot(111)
im = ax.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), animated=True)
def updatefig(*args):
ret, frame = cap.read()
im.set_array(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
return im
ani = animation.FuncAnimation(fig, updatefig, interval=10)
plt.show()
我发现这非常有用,因为 matplotlib.animation.FuncAnimation
可以为附加到 ax
对象,只要它们在上面的 updatefig()
函数中更新即可。
编辑:当我在 Jupyter 笔记本上工作时,我添加了以下内容以便在新 window 中观看视频:
import matplotlib
matplotlib.use('qt5agg')