为了方便准备审校数据集,开发拍照工具,将之前手写修改的审校内容拍摄成图片,提供给后续流程来产生数据集。

# coding:utf-8
import cv2
import time
import os

# 打开摄像头,参数为0时调用本地摄像头,参数为1时调用外接摄像头
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)

# 设置保存的分辨率
save_width = 2480  # 3508 # 宽度
save_height = 3508 # 2480 # 高度
cap.set(cv2.CAP_PROP_FRAME_WIDTH, save_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, save_height)

# 设置显示的分辨率
display_width = 1000 # 宽度
display_height = 600 # 高度

# 创建保存目录
save_dir = "D:/yiyu/photo/"
if not os.path.exists(save_dir):
    os.makedirs(save_dir)

# 检查摄像头是否成功打开
if not cap.isOpened():
    print("Error: Could not open camera")
    exit()

while True:
    ret, frame = cap.read()
    if not ret:
        print("Error: Failed to capture frame")
        break
        
    # 调整显示帧的大小
    display_frame = cv2.resize(frame, (display_width, display_height))
    cv2.imshow("Capture_Paizhao", display_frame)
    
    k = cv2.waitKey(1) & 0xFF
    if k == ord('s'): # 按下s键,进入下面的保存图片操作
        current_time = time.time()
        milliseconds = int(round(current_time * 1000))
        filename = save_dir + str(milliseconds) + ".jpg"
        # 旋转图像90度逆时针
        rotated_frame = cv2.rotate(frame, cv2.ROTATE_90_COUNTERCLOCKWISE)
        success = cv2.imwrite(filename, rotated_frame)
        if success:
            print("save " + str(milliseconds) + ".jpg successfully!")
        else:
            print("Error: Failed to save image")
        print("-------------------------")
    elif k == ord('q'): # 按下q键,程序退出
        break

# 释放摄像头和销毁窗口
cap.release()
cv2.destroyAllWindows()