Use python to call computer camera under Mac


I’m currently working on the registration function of the company’s website, and I wanted to add a user portrait selfie function.

Not much nonsense, just go to the code (you can run it directly)

First remember to install the package

# 我的版本 opencv-python              4.1.1.26   
pip3 install opencv-python

Press the s keyboard to save, and the image path is an absolute path, which can be based on your needs. 

import cv2

cap = cv2.VideoCapture(0)
i = 0
while (1):
    ret, frame = cap.read()
    k = cv2.waitKey(1)
    if k == 27:
        break
    elif k == ord('s'):
        # 注意修改保持路径
        cv2.imwrite('/Users/zuichudemuyang/Desktop/' + str(i) + '.jpg', frame)
        i += 1
    cv2.imshow("capture", frame)
cap.release()
cv2.destroyAllWindows()

 

Guess you like

Origin blog.csdn.net/zhang_8626/article/details/95362329