5 lines of Python code to call the computer camera

The purpose is the same.

Make sure that the opencv-python module is installed in python. If it is not installed, you can refer to: https://pypi.org/project/opencv-python/ for installation.

Not much, just go to the code:

import cv2
cap = cv2.VideoCapture(0)
f, frame = cap.read() #此刻拍照
cv2.imwrite("example.png", frame) # 将拍摄内容保存为png图片
cap.release() # 关闭调用的摄像头

(The code block can slide left and right)

The results are as follows:

5 lines of Python code to call the computer camera

In addition, you can also remove the color of the photo and turn it into a black and white photo.

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

The effect is as follows:

5 lines of Python code to call the computer camera

When you execute the above command step by step, you will see that the indicator light of the computer camera is always on, which can indicate that your camera is being called. Depending on the specific hardware device, some devices can directly call the camera without turning on the light. In this case, you are unaware.

Of course, even if you cannot control the indicator light of the camera, you can call the camera silently. For example, if the above code is not executed step by step, but is executed as a block in the form of a script, the indicator light of the camera will only flash by, and you may not notice it in many cases. But in fact, the camera has been called to take pictures.

===== THE END ====

5 lines of Python code to call the computer camera

Guess you like

Origin blog.51cto.com/15069450/2577364
Recommended