一.最简单的调用笔记本内置相机
import cv2
#引入库
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow("Video", frame)
#读取内容
if cv2.waitKey(10) == ord("q"):
break
#随时准备按q退出
cap.release()
cv2.destroyAllWindows()
#停止调用,关闭窗口
import cv2
import os
#引入库
print("=============================================")
print("= 热键(请在摄像头的窗口使用): =")
print("= z: 更改存储目录 =")
print("= x: 拍摄图片 =")
print("= q: 退出 =")
print("=============================================")
#提醒用户操作字典
class_name = input("请输入存储目录:")
while os.path.exists(class_name):
class_name = input("目录已存在!请输入存储目录:")
os.mkdir(class_name)
#存储
index = 1
cap = cv2.VideoCapture(0)
width = 640
height = 480
w = 360
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
crop_w_start = (width-w)//2
crop_h_start = (height-w)//2
print(width, height)
#设置特定值
while True:
ret, frame = cap.read()
frame = frame[crop_h_start:crop_h_start+w, crop_w_start:crop_w_start+w]
#没理解?
frame = cv2.flip(frame,1,dst=None)
#镜像显示
cv2.imshow("capture", frame)
#显示