bsw215583320
2025-01-16 8446139d79c366fc2c44d72c2ddb963817a8ce7d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 上料识别,获取上料图片,保存到data/images文件夹下
 
 
import os
import cv2
import time
import numpy as np
import onnxruntime
import win32com.client
 
 
def list_webcams():
    # 创建一个WMI客户端实例
    wmi = win32com.client.GetObject("winmgmts:")
    webcams = wmi.InstancesOf("Win32_PnPEntity")
    # 创建摄像头和索引字典
    webcam_dict = {}
    index = 0
    for device in webcams:
        name = getattr(device, 'Name', None)
        pnp_class = getattr(device, 'PNPClass', None)
        if name is not None and cam1 in name:
            # 将设备名字和索引添加到字典中
            webcam_dict[cam1] = index
            index += 1
        elif name is not None and cam2 in name:
            # 将设备名字和索引添加到字典中
            webcam_dict[cam2] = index
            index += 1
    return webcam_dict
 
 
# 调用另一个长焦镜头,拍摄清晰的局部药材图片
def get_image():
    camera2_index = webcams.get(cam2)
    print("第二个摄像头索引:" + str(camera2_index))
    # 打开摄像头
    capture = cv2.VideoCapture(camera2_index, cv2.CAP_DSHOW)
    # 设置分辨率
    capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)  # 宽度
    capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)  # 高度
    # 检查摄像头是否成功打开
    if not capture.isOpened():
        print("无法打开摄像头2")
        exit()
    width2 = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
    height2 = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
    print("摄像头2分辨率:", width2, "x", height2)
    # 循环读取摄像头画面
    # Shadows name 'width' from outer scope
    count = 0
    while True:
        ret2, frame2 = capture.read()
        if not ret2:
            print("无法读取摄像头画面")
            break
 
        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]
        if count == 2:
            # 调整图像尺寸
            resized_frame2 = cv2.resize(frame2, (target_width, target_height))
            # 显示画面
            # cv2.imshow('Output2', resized_frame2)
            # 生成保存文件名,以当前时间命名
            save_name2 = time.strftime("%Y%m%d%H%M%S", time.localtime()) + ".jpg"
            # 保存调整尺寸后的图片
            cv2.imwrite(save_path + "2/" + save_name2, resized_frame2)
            # cv2.imshow("Camera", resized_frame2)
            print("保存图片:", save_name2)
            break
    # 结束线程
    capture.release()
 
 
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/loading.onnx")
    # 摄像头索引号,通常为0表示第一个摄像头
    camera_index = webcams.get(cam1)
    print("第一个摄像头索引:" + str(camera_index))
    modelmeta = session.get_modelmeta()
    metadata_map = modelmeta.custom_metadata_map
    classes = eval(metadata_map['names'])
    # 打开摄像头
    cap = cv2.VideoCapture(camera_index, cv2.CAP_DSHOW)
    # 设置分辨率
    # 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)
 
    # 目标图像尺寸
 
    # 计时器
    frame_count = 0
    start_time = time.time()
    stime = time.time()
 
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    class_old = "1"
    count = 0
    status = "没有上料"
    # 循环读取摄像头画面
    while True:
        # 睡眠100毫秒
        time.sleep(0.1)
 
        ret, frame = cap.read()
 
        if not ret:
            print("无法读取摄像头画面")
            break
 
        resized_frame = frame
        if height > target_height and width > target_width:
            # 1920*1080的图像,中心裁剪640*480的区域
            a = int(height / 2 - target_height / 2)
            b = int(height / 2 + target_height / 2)
            c = int(width / 2 - target_width / 2)
            d = int(width / 2 + target_width / 2)
            print(a, b, c, d)
            cropped_frame = frame[a:b, c:d]
            # 调整图像尺寸
            resized_frame = cv2.resize(cropped_frame, (target_width, target_height))
 
        # 获取当前时间
        current_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()
 
        # 预处理
        blob = cv2.dnn.blobFromImage(resized_frame, 1 / 255.0, (640, 640), swapRB=True, crop=False)
 
        # 模型推理
        outputs = session.run(None, {session.get_inputs()[0].name: blob})
        #
        # print(outputs)
        # 应用softmax函数
        probabilities = outputs[0]
 
        # 找到最大概率的类别
        predicted_class = np.argmax(probabilities, axis=1)[0]
        max_probability = np.max(probabilities, axis=1)[0]
        class_ = classes[predicted_class]
 
        # 计算类型重复的次数,类别更换之后重新计数
        if class_ != class_old:
            count = 0
        else:
            count += 1
        class_old = class_
        print(f"{class_}:{count}: {max_probability}")
        # 判断是否上料并且上料次数大于10次
        if class_ == "shangliao" and count > 10:
            status = "正在上料"
            # 每隔3秒取一帧图像
            # 如果距离上一次保存已经过去1秒,则保存当前画面
            if current_time - stime >= 10.0:
                save_name = time.strftime("%Y%m%d%H%M%S", time.localtime()) + ".jpg"
                # 保存调整尺寸后的图片
                cv2.imwrite(save_path + "1/" + save_name, resized_frame)
                # 重置计时器
                stime = time.time()
                get_image()
        else:
            status = "没有上料"
 
        print(status)
        # # 找到概率较高的前十个类别
        # top_ten_classes = np.argsort(probabilities, axis=1)[0][-1:]
        #
        # # 输出前十个类别
        # print("Top 5 Classes:")
        # for i in top_ten_classes:
        #     print(f"{classes[i]}: {probabilities[0][i]}")
 
        # 计算帧速率
        frame_count += 1
        end_time = time.time()
        elapsed_time = end_time - start_time
        fps = frame_count / elapsed_time
        # print(f"FPS: {fps:.2f}")
        # 将FPS绘制在图像上
        cv2.putText(resized_frame, f"FPS: {fps:.2f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2,
                    cv2.LINE_AA)
        # 显示画面
        cv2.imshow("Camera", resized_frame)
 
        # 检测按键,如果按下q键则退出循环
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
 
    # 关闭摄像头
    cap.release()
 
    # 关闭所有窗口
    cv2.destroyAllWindows()