最近在虚拟机和Linux子系统上折腾usb摄像头调用,无果。本着学习态度,于是写python脚本调用,在Windows上运行。
# -*- coding: utf-8 -*-
# !/usr/bin/env python3.9
import cv2
cap = cv2.VideoCapture(0)
index = 1
while (cap.isOpened()):
ret, frame = cap.read()
cv2.imshow("USB", frame)
k = cv2.waitKey(1) & 0xFF
if k == ord('s'): # 按下s(save)键,进入保存图片操作
cv2.imwrite("C:/Users/inwinic/Desktop/img" + str(index) + ".jpg", frame)
print("save fruit" + str(index) + ".jpg successfuly!")
index += 1
elif k == ord('q'): # 按下q(quit)键,程序退出
break
cap.release()
cv2.destroyAllWindows()