• Python: 3.8.5
  • Opencv: 4.0.1
  • OS: Windows 10 1909
  • Conda: 4.10.3
  • Pycharm: 2018.1

Code sample

import cv2

capture = cv2.VideoCapture(0)

if capture.isOpened():
    print("Camera is on.")

while capture.isOpened():
    # retval, image
    retval, image = capture.read()

    if retval:
        cv2.imshow("capture", image)

    # If you press the Q key, then exit the loop
    if cv2.waitKey(10) = =ord("q") :break

capture.release()
cv2.destroyAllWindows()

Copy the code

The results

D: \ Develop \ Anaconda3 \ python exe E: / Users/Admin/Desktop/opencv/demo. Py camera is open global [WARN: 1] D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback Process finished with exit code 0Copy the code
  • In the VM +Linux environment:
  • Press the Q key to exit the program, the second time to restart, the program can not read the state of the camera for a long time, can only terminate.
  • The initial judgment is related to the driver of virtual machine and camera, which needs to be further explored.
  • So the code was moved to the Windows environment

Learning to recommend

  • Python documentation – English
  • Python documentation – Chinese
  • Python code PEP
  • Google version of the Python specification
  • opencv
  • Opencv help manual
  • Python source code
  • Python PEP
  • Optimal kirin
  • The nuggets platform
  • Gitee platform


Python is open source, cross-platform, interpretive, interactive, and worth learning. Python’s design philosophy: elegant, unambiguous, simple. Advocate one way, preferably only one way to do one thing. Code should be written in accordance with specifications to facilitate communication and understanding. Every language has its own unique ideas. Beginners need to change their thinking, practice and accumulate.