From 8446139d79c366fc2c44d72c2ddb963817a8ce7d Mon Sep 17 00:00:00 2001
From: bsw215583320 <baoshiwei121@163.com>
Date: 星期四, 16 一月 2025 14:38:34 +0800
Subject: [PATCH] 优化上料识别与药材识别

---
 camera_onnx.py |   60 +++++++++++++++++++++++++++++++++---------------------------
 1 files changed, 33 insertions(+), 27 deletions(-)

diff --git a/camera_onnx.py b/camera_onnx.py
index cba18f3..5db1439 100644
--- a/camera_onnx.py
+++ b/camera_onnx.py
@@ -1,5 +1,7 @@
-import os
+# 涓婃枡璇嗗埆锛岃幏鍙栦笂鏂欏浘鐗囷紝淇濆瓨鍒癲ata/images鏂囦欢澶逛笅
 
+
+import os
 import cv2
 import time
 import numpy as np
@@ -17,26 +19,26 @@
     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")
@@ -46,45 +48,49 @@
     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]
-        # 璋冩暣鍥惧儚灏哄
-        resized_frame2 = cv2.resize(cropped_frame2, (target_width, target_height))
-        # 鐢熸垚淇濆瓨鏂囦欢鍚嶏紝浠ュ綋鍓嶆椂闂村懡鍚�
-        save_name = time.strftime("%Y%m%d%H%M%S", time.localtime()) + ".jpg"
-        # 淇濆瓨璋冩暣灏哄鍚庣殑鍥剧墖
-        cv2.imwrite(save_path + "2/" + save_name, resized_frame2)
-        # cv2.imshow("Camera", resized_frame2)
-        print("淇濆瓨鍥剧墖:", save_name)
-        break
+        # 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()
-    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

--
Gitblit v1.9.3