| | |
| | | |
| | | # 打开摄像头 |
| | | cap = cv2.VideoCapture(camera_index) |
| | | |
| | | # 设置分辨率 |
| | | # cap.set(cv2.CAP_PROP_FRAME_WIDTH, 3840) # 宽度 |
| | | # cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 2160) # 高度 |
| | | # 检查摄像头是否成功打开 |
| | | if not cap.isOpened(): |
| | | print("无法打开摄像头") |
| | | exit() |
| | | |
| | | width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) |
| | | height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) |
| | | print("摄像头分辨率:", width, "x", height) |
| | | # 图片保存路径 |
| | | save_path = "captured_images/" |
| | | |
| | |
| | | print("无法读取摄像头画面") |
| | | break |
| | | |
| | | # 裁剪图像 |
| | | # cropped_frame = frame[750:1230, 1650:2290] |
| | | # 调整图像尺寸 |
| | | resized_frame = cv2.resize(frame, (target_width, target_height)) |
| | | |
| | | # 获取当前时间 |
| | | current_time = time.time() |
| | | |
| | | # 如果距离上一次保存已经过去1秒,则保存当前画面 |
| | | if current_time - start_time >= 1.0: |
| | | # 生成保存文件名,以当前时间命名 |
| | | save_name = time.strftime("%Y%m%d%H%M%S", time.localtime()) + ".jpg" |
| | | # 保存调整尺寸后的图片 |
| | | cv2.imwrite(save_path + save_name, resized_frame) |
| | | print("保存图片:", save_name) |
| | | # 重置计时器 |
| | | start_time = time.time() |
| | | #如果距离上一次保存已经过去1秒,则保存当前画面 |
| | | # if current_time - start_time >= 3.0: |
| | | # # 生成保存文件名,以当前时间命名 |
| | | # save_name = time.strftime("%Y%m%d%H%M%S", time.localtime()) + ".jpg" |
| | | # # 保存调整尺寸后的图片 |
| | | # cv2.imwrite(save_path + save_name, frame) |
| | | # print("保存图片:", save_name) |
| | | # # 重置计时器 |
| | | # start_time = time.time() |
| | | |
| | | # 显示画面 |
| | | cv2.imshow("Camera", resized_frame) |
| | | cv2.imshow("Camera", frame) |
| | | |
| | | # 检测按键,如果按下q键则退出循环 |
| | | if cv2.waitKey(1000) & 0xFF == ord('q'): |
| | | if cv2.waitKey(1) & 0xFF == ord('q'): |
| | | break |
| | | |
| | | # 关闭摄像头 |