使用Python的opencv-python调用摄像头

先上代码,注意使用python3运行

#!/usr/bin/env python3
# coding=utf-8
from cv2 import cv2
cap = cv2.VideoCapture(0)
while True:
    ret, frame = cap.read()
    cv2.imshow('frame', frame)
    c = cv2.waitKey(1)
    if c == ord('q'):
        break
cap.release()
cv2.DestroyAllWindows()

注意依赖安装顺序:

1.先安装

sudo apt-get install libgtk2.0-dev
sudo apt-get install pkg-config

2.再安装

pip3 install opencv-python

如果已经安装过,并且提示错误,则使用以下命令安装

pip3 uninstall opencv-python

pip3 --no-cache-dir install opencv-python

如果没有进行第一步安装,直接安装opencv-python会出现以下错误:

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

 

posted @ 2020-12-04 17:35  广林  阅读(865)  评论(0编辑  收藏  举报