| | |
| | | import os |
| | | # 上料识别,获取上料图片,保存到data/images文件夹下 |
| | | |
| | | |
| | | import os |
| | | import cv2 |
| | | import time |
| | | import numpy as np |
| | |
| | | for device in webcams: |
| | | name = getattr(device, 'Name', None) |
| | | pnp_class = getattr(device, 'PNPClass', None) |
| | | if name is not None and 'PC Camera' in name: |
| | | if name is not None and cam1 in name: |
| | | # 将设备名字和索引添加到字典中 |
| | | webcam_dict['PC Camera'] = index |
| | | webcam_dict[cam1] = index |
| | | index += 1 |
| | | elif name is not None and "USB Camera" in name: |
| | | elif name is not None and cam2 in name: |
| | | # 将设备名字和索引添加到字典中 |
| | | webcam_dict['USB Camera'] = index |
| | | webcam_dict[cam2] = index |
| | | index += 1 |
| | | return webcam_dict |
| | | |
| | | |
| | | # 调用另一个长焦镜头,拍摄清晰的局部药材图片 |
| | | def get_image(): |
| | | camera2_index = webcams.get('PC Camera') |
| | | camera2_index = webcams.get(cam2) |
| | | print("第二个摄像头索引:" + str(camera2_index)) |
| | | # 打开摄像头 |
| | | capture = cv2.VideoCapture(camera2_index, cv2.CAP_DSHOW) |
| | | # 设置分辨率 |
| | | capture.set(cv2.CAP_PROP_FRAME_WIDTH, 3800) # 宽度 |
| | | capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 2150) # 高度 |
| | | capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1920) # 宽度 |
| | | capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080) # 高度 |
| | | # 检查摄像头是否成功打开 |
| | | if not capture.isOpened(): |
| | | print("无法打开摄像头2") |
| | |
| | | print("摄像头2分辨率:", width2, "x", height2) |
| | | # 循环读取摄像头画面 |
| | | # Shadows name 'width' from outer scope |
| | | count = 0 |
| | | while True: |
| | | ret2, frame2 = capture.read() |
| | | if not ret2: |
| | | print("无法读取摄像头画面") |
| | | break |
| | | |
| | | # 显示画面 |
| | | # cv2.imshow('Output2', frame2) |
| | | count += 1 |
| | | # 1920*1080的图像,中心裁剪640*480的区域 |
| | | a2 = int(height2 / 2 - target_height / 2) |
| | | b2 = int(height2 / 2 + target_height / 2) |
| | | c2 = int(width2 / 2 - target_width / 2) |
| | | d2 = int(width2 / 2 + target_width / 2) |
| | | cropped_frame2 = frame2[a2:b2, c2:d2] |
| | | # a2 = int(height2 / 2 - target_height / 2) |
| | | # b2 = int(height2 / 2 + target_height / 2) |
| | | # c2 = int(width2 / 2 - target_width / 2) |
| | | # d2 = int(width2 / 2 + target_width / 2) |
| | | # cropped_frame2 = frame2[a2:b2, c2:d2] |
| | | if count == 2: |
| | | # 调整图像尺寸 |
| | | resized_frame2 = cv2.resize(cropped_frame2, (target_width, target_height)) |
| | | resized_frame2 = cv2.resize(frame2, (target_width, target_height)) |
| | | # 显示画面 |
| | | # cv2.imshow('Output2', resized_frame2) |
| | | # 生成保存文件名,以当前时间命名 |
| | | save_name = time.strftime("%Y%m%d%H%M%S", time.localtime()) + ".jpg" |
| | | save_name2 = time.strftime("%Y%m%d%H%M%S", time.localtime()) + ".jpg" |
| | | # 保存调整尺寸后的图片 |
| | | cv2.imwrite(save_path + "2/" + save_name, resized_frame2) |
| | | cv2.imwrite(save_path + "2/" + save_name2, resized_frame2) |
| | | # cv2.imshow("Camera", resized_frame2) |
| | | print("保存图片:", save_name) |
| | | print("保存图片:", save_name2) |
| | | break |
| | | # 结束线程 |
| | | capture.release() |
| | | return frame2 |
| | | |
| | | |
| | | if __name__ == '__main__': |
| | | |
| | | cam1 = "USB Camera" |
| | | cam2 = "PC Camera" |
| | | webcams = list_webcams() |
| | | print(webcams) |
| | | target_width = 1024 |
| | | target_height = 768 |
| | | save_path = "data/images/" |
| | | # 加载ONNX模型 |
| | | session = onnxruntime.InferenceSession("model/best.onnx") |
| | | session = onnxruntime.InferenceSession("model/loading.onnx") |
| | | # 摄像头索引号,通常为0表示第一个摄像头 |
| | | camera_index = webcams.get('USB Camera') |
| | | camera_index = webcams.get(cam1) |
| | | print("第一个摄像头索引:" + str(camera_index)) |
| | | modelmeta = session.get_modelmeta() |
| | | metadata_map = modelmeta.custom_metadata_map |