使用摄像头实例:

    1. #coding:utf-8
    2. import cv2
    3. def video_capture():
    4. camera = cv2.VideoCapture(0)
    5. while (True):
    6. ret, frame = camera.read()
    7. if ret == True:
    8. image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    9. cv2.imshow("image", image)
    10. if cv2.waitKey(1) & 0xFF == ord('q'):
    11. break
    12. else:
    13. print("Camera read return None")
    14. break
    15. if camera.isOpened() == False:
    16. print("Can not Open Camera")
    17. camera.release()
    18. cv2.destroyAllWindows()
    19. if __name__ == '__main__':
    20. video_capture()

    系统未安装python-opencv的情况下,

    1. import cv2

    会出现ImportError,但是如果使用如下操作安装opencv-python(opencv API)

    1. pip install opencv-python

    引入cv2时不会出现错误,但是摄像头会出问题,具体问题体现如下:

    • read()返回的ret永远为False,frame永远为None
    • camera.isOpened()返回永远为False
    • cvtColor,imshow等错误

    总结:opencv使用摄像头时,tensorflow环境需要卸载opencv-python并安装python-opencv

    1. pip uninstall opencv-python
    2. sudo apt install python-opencv

    https://blog.csdn.net/fangzhengshu/article/details/78530230