拍摄照片
import cv2
import numpy
import time
'''
实现了拍摄OK或者NG图片q
按O拍摄OK的照片
按N拍摄NG的照片
按q退出
'''
# 调整参数实现读取视频或者调用摄像头
cap = cv2.VideoCapture(0) # 调用摄像头
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920) # 分辨率
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080) # 分辨率
cap.set(cv2.CAP_PROP_FPS, 20) # 帧数
index_ok = 0
index_ng = 0
# current_day = time.strftime('%Y-%m-%d')
current_day = "20190925"
print(current_day)
cv2.namedWindow('Frame',cv2.WINDOW_FULLSCREEN)
while(cap.isOpened()):
# 从摄像头读取一帧,ret是表明成功与否
ret, frame = cap.read()
if ret:
cv2.imshow('Frame', frame)
else:
break
input = cv2.waitKey(1) & 0xFF
# 按O保存OK图片
if input == ord('o'):
cv2.imwrite("./OK/ok-%s-%s.jpg" % (current_day, index_ok), frame)
index_ok += 1
print("已拍摄%s张OK图片,%s张OK图片" % (index_ok, index_ng))
# 按N保存NG图片
elif input == ord('n'):
cv2.imwrite("./NG/ngq-%s-%s.jpg" % (current_day, index_ng), frame)
index_ng += 1
print("已拍摄%s张OK图片,%s张NG图片" % (index_ok, index_ng))
# 监听键盘,按下q键退出
if input == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
拍摄并保存视频
import cv2
'''
实现了拍摄视频的功能
可以自定义分辨率、帧率
'''
# 调整参数实现读取视频或者调用摄像头
cap = cv2.VideoCapture(1) # 调用摄像头
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920) # 分辨率
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080) # 分辨率
cap.set(cv2.CAP_PROP_FPS, 20) # 帧数
fourcc = cv2.VideoWriter_fourcc("I", "4", "2", "0")
out = cv2.VideoWriter("./video/test.mp4", fourcc, 20, (1920, 1080))
cv2.namedWindow('Frame', cv2.WINDOW_FULLSCREEN)
while (cap.isOpened()):
# 从摄像头读取一帧,ret是表明成功与否
ret, frame = cap.read()
if ret:
out.write(frame)
cv2.imshow('Frame', frame)
else:
break
input = cv2.waitKey(1) & 0xFF
# 监听键盘,按下q键退出
if input == ord('q'):
break
cap.release()
cv2.destroyAllWindows()