pip install opencv-python
调用外接摄像头实现拍照功能,并保存至指定的文件下。
按 s 拍照并保存,按 q 退出程序
# coding:utf-8
import cv2
cap = cv2.VideoCapture(1, cv2.CAP_DSHOW)
flag = cap.isOpened()
index = 1
while (flag):
ret, frame = cap.read()
cv2.imshow("Capture_Paizhao", frame)
k = cv2.waitKey(1) & 0xFF
if k == ord('s'): # 按下s键,进入下面的保存图片操作
cv2.imwrite("E:/PyCharm Workspaces/" + str(index) + ".jpg", frame)
print("save" + str(index) + ".jpg successfuly!")
print("-------------------------")
index += 1
elif k == ord('q'): # 按下q键,程序退出
break
cap.release() # 释放摄像头
cv2.destroyAllWindows()# 释放并销毁窗口
参考博客
https://blog.csdn.net/Clannad_niu/article/details/83747092?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.edu_weight&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.edu_weight
1、一般获取摄像头采用以下方法:
# 参数为0时调用本地摄像头;参数为1时调用外接摄像头
cap = cv2.VideoCapture(0)
当运行之后会报出错误警告,如下:
[ WARN:1] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-52oirelq\opencv\modules\videoio\src\cap_msmf.cpp (435) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
网上查阅资料之后,在调用上做一下修改则可以解决该问题
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
该问题的解决参考了以下博客
https://blog.csdn.net/skye_fly/article/details/108974819
2、图片连续保存
index = 1
while (1):
cv2.imwrite("PATH" + str(index) + ".jpg", frame)
# PATH为保存路径
print("save" + str(index) + ".jpg successfuly!")
index += 1