From 8446139d79c366fc2c44d72c2ddb963817a8ce7d Mon Sep 17 00:00:00 2001 From: bsw215583320 <baoshiwei121@163.com> Date: 星期四, 16 一月 2025 14:38:34 +0800 Subject: [PATCH] 优化上料识别与药材识别 --- model/loading.onnx | 0 test_herb_identify.py | 32 +++ herb_identify.py | 269 +++++++++++++++++++++++++++++ cam_detect.py | 15 model/herb_identify.onnx | 0 camera_onnx.py | 60 +++--- laplacian.py | 124 +++++++++++++ 7 files changed, 466 insertions(+), 34 deletions(-) diff --git a/cam_detect.py b/cam_detect.py index af30f97..922816f 100644 --- a/cam_detect.py +++ b/cam_detect.py @@ -1,6 +1,6 @@ +# 鎽勫儚澶寸敾闈㈡祴璇曠▼搴� import os - import cv2 import time import numpy as np @@ -8,14 +8,14 @@ import win32com.client if __name__ == '__main__': -# 鎽勫儚澶寸储寮曞彿锛岄�氬父涓�0琛ㄧず绗竴涓憚鍍忓ご - camera_index = 1 + # 鎽勫儚澶寸储寮曞彿锛岄�氬父涓�0琛ㄧず绗竴涓憚鍍忓ご + camera_index = 3 # 鎵撳紑鎽勫儚澶� cap = cv2.VideoCapture(camera_index, cv2.CAP_DSHOW) # 璁剧疆鍒嗚鲸鐜� - cap.set(cv2.CAP_PROP_FRAME_WIDTH, 3800) # 瀹藉害 - cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 2100) # 楂樺害 + cap.set(cv2.CAP_PROP_FRAME_WIDTH, 2048) # 瀹藉害 + cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1540) # 楂樺害 # 妫�鏌ユ憚鍍忓ご鏄惁鎴愬姛鎵撳紑 if not cap.isOpened(): print("鏃犳硶鎵撳紑鎽勫儚澶�") @@ -62,8 +62,9 @@ # 灏咶PS缁樺埗鍦ㄥ浘鍍忎笂 cv2.putText(resized_frame, f"FPS: {fps:.2f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA) + resizeframe = cv2.resize(frame, (target_width, target_height)) # 鏄剧ず鐢婚潰 - cv2.imshow("Camera", resized_frame) + cv2.imshow("Camera", resizeframe) # 妫�娴嬫寜閿紝濡傛灉鎸変笅q閿垯閫�鍑哄惊鐜� if cv2.waitKey(1) & 0xFF == ord('q'): @@ -73,4 +74,4 @@ cap.release() # 鍏抽棴鎵�鏈夌獥鍙� - cv2.destroyAllWindows() \ No newline at end of file + cv2.destroyAllWindows() 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 diff --git a/herb_identify.py b/herb_identify.py new file mode 100644 index 0000000..cf049dd --- /dev/null +++ b/herb_identify.py @@ -0,0 +1,269 @@ +# 涓婃枡璇嗗埆锛岃幏鍙栦笂鏂欏浘鐗囷紝璋冪敤鑽潗璇嗗埆妯″瀷锛岃緭鍑鸿瘑鍒粨鏋� + + +import os +import cv2 +import time +import numpy as np +import onnxruntime +import win32com.client + + + +# 璁$畻鎷夋櫘鎷夋柉鍝嶅簲鐨勬柟宸�,鍒ゆ柇鍥惧儚鐨勬竻鏅板害 +def variance_of_laplacian(image): + # 璁$畻杈撳叆鍥惧儚鐨勬媺鏅媺鏂搷搴旂殑鏂瑰樊 + return cv2.Laplacian(image, cv2.CV_64F).var() +# 鑾峰彇鎽勫儚澶村垪琛� +def list_webcams(): + # 鍒涘缓涓�涓猈MI瀹㈡埛绔疄渚� + 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, 2048) # 瀹藉害 + capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1540) # 楂樺害 + # 妫�鏌ユ憚鍍忓ご鏄惁鎴愬姛鎵撳紑 + 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)) + + # 棰勫鐞� + herb_blob = cv2.dnn.blobFromImage(resized_frame2, 1 / 255.0, (640, 640), swapRB=True, crop=False) + + # 妯″瀷鎺ㄧ悊 + herb_outputs = identify_session.run(None, {identify_session.get_inputs()[0].name: herb_blob}) + + herb_probabilities = herb_outputs[0] + + top_five_classes = np.argsort(herb_probabilities, axis=1)[0][-5:][::-1] + name = "" + for i, class_id in enumerate(top_five_classes): + # 淇濈暀涓や綅灏忔暟 + probability = round(herb_probabilities[0][class_id], 2) + + herb_class = herbs[class_id] + name = name + herb_class + "=" + str(probability) + # 鏄剧ず鐢婚潰 + # cv2.imshow('Output2', resized_frame2) + + # 璁$畻鎷夋櫘鎷夋柉鍝嶅簲鐨勬柟宸� + laplacian = variance_of_laplacian(resized_frame2) + # 鐢熸垚淇濆瓨鏂囦欢鍚嶏紝浠ュ綋鍓嶆椂闂村懡鍚� + save_name2 = name +"_["+ str(round(laplacian, 2)) +"]_"+ time.strftime("%Y%m%d%H%M%S", time.localtime()) + ".jpg" + # 鍒ゆ柇鍥惧儚鐨勬竻鏅板害 + # 淇濆瓨璋冩暣灏哄鍚庣殑鍥剧墖 + if laplacian > 1500: + c_ = save_path + "2/c/" + # 鍒ゆ柇鏂囦欢鏄惁瀛樺湪锛屼笉瀛樺湪鍒欏垱寤� + if not os.path.exists(c_): + os.makedirs(c_) + cv2.imwrite(c_ + save_name2, resized_frame2) + else: + n_ = save_path + "2/n/" + # 鍒ゆ柇鏂囦欢鏄惁瀛樺湪锛屼笉瀛樺湪鍒欏垱寤� + if not os.path.exists(n_): + os.makedirs(n_) + cv2.imwrite(n_ + 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") + identify_session = onnxruntime.InferenceSession("model/herb_identify.onnx") + herbs = eval(identify_session.get_modelmeta().custom_metadata_map['names']) + # 鎽勫儚澶寸储寮曞彿锛岄�氬父涓�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 = "姝e湪涓婃枡" + # 姣忛殧3绉掑彇涓�甯у浘鍍� + # 濡傛灉璺濈涓婁竴娆′繚瀛樺凡缁忚繃鍘�1绉掞紝鍒欎繚瀛樺綋鍓嶇敾闈� + if current_time - stime >= 10.0: + save_name = time.strftime("%Y%m%d%H%M%S", time.localtime()) + ".jpg" + # 淇濆瓨璋冩暣灏哄鍚庣殑鍥剧墖 + path_ = save_path + "1/" + if not os.path.exists(path_): + os.makedirs(path_) + cv2.imwrite(path_ + 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}") + # 灏咶PS缁樺埗鍦ㄥ浘鍍忎笂 + 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() diff --git a/laplacian.py b/laplacian.py new file mode 100644 index 0000000..1555d1e --- /dev/null +++ b/laplacian.py @@ -0,0 +1,124 @@ +import os + +import cv2 +from scipy.fftpack import fftshift, fft2 +import numpy as np + + +def variance_of_laplacian(image): + # 璁$畻杈撳叆鍥惧儚鐨勬媺鏅媺鏂搷搴旂殑鏂瑰樊 + return cv2.Laplacian(image, cv2.CV_64F).var() + +def frequency_domain_analysis(gray): + + # 璁$畻鍌呴噷鍙跺彉鎹㈠苟绉诲姩鍒颁腑蹇� + f_transform = fftshift(fft2(gray)) + # 璁$畻棰戣氨骞呭害 + magnitude_spectrum = 20 * np.log(np.abs(f_transform)) + # 璁$畻楂橀鎴愬垎鐨勬瘮渚� + high_frequency_magnitude = np.sum(magnitude_spectrum > np.mean(magnitude_spectrum)) + total_magnitude = magnitude_spectrum.size + ratio = high_frequency_magnitude / total_magnitude + return ratio +def edge_gradient(gray): + + sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3) + sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3) + magnitude = np.sqrt(sobelx**2 + sobely**2) + return np.mean(magnitude) + +def testOne(): + # 鍔犺浇鍥惧儚骞惰浆鎹负鐏板害鍥� + image = cv2.imread('H:/images/1/20250114193113.jpg') + gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) + + # 璁$畻鍥惧儚鐨勬竻鏅板害鍒嗘暟 + score = variance_of_laplacian(gray) + ratio = frequency_domain_analysis(gray) + edge_gradient_score = edge_gradient(gray) + + print("Image clarity score: ", score) + print("High frequency ratio: ", ratio) + print("Edge gradient score: ", edge_gradient_score) + + # 鏍规嵁缁忛獙璁剧疆闃堝�兼潵鍒ゆ柇鍥惧儚鏄惁妯$硦 + threshold = 100 # 杩欎釜闃堝�煎彲浠ユ牴鎹疄闄呮儏鍐佃皟鏁� + if score < threshold: + print("The image is considered blurry.") + else: + print("The image is considered clear.") + + +def batch_score(): + global name, join + # 璇诲彇鏂囦欢澶逛笅鐨勬墍鏈夊浘鍍忓垎鍒绠楀垎鏁� + folder_path = 'H:/images/2' + for filename in os.listdir(folder_path): + if filename.endswith('.jpg') or filename.endswith('.png'): + image_path = os.path.join(folder_path, filename) + image = cv2.imread(image_path) + gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) + score = variance_of_laplacian(gray) + ratio = frequency_domain_analysis(gray) + edge_gradient_score = edge_gradient(gray) + # 鎵�鏈夌粨鏋滀繚鐣欎袱浣嶅皬鏁� + score = round(score, 2) + ratio = round(ratio, 2) + edge_gradient_score = round(edge_gradient_score, 2) + name = "score=" + str(score) + "ratio=" + str(ratio) + "edge_gradient_score=" + str( + edge_gradient_score) + ".jpg" + path_ = folder_path + "/22/" + # 鍒ゆ柇鏂囦欢澶规槸鍚﹀瓨鍦紝涓嶅瓨鍦ㄥ垯鏂板缓 + if not os.path.exists(path_): + os.makedirs(path_) + join = path_ + name + print(join) + cv2.imwrite(join, image) + + +def cam_score(): + # 璋冪敤鎽勫儚澶磋绠楅噰闆嗗埌鍥惧儚鐨勫緱鍒� + camera_index = 2 + print("绗簩涓憚鍍忓ご绱㈠紩锛�" + str(camera_index)) + # 鎵撳紑鎽勫儚澶� + capture = cv2.VideoCapture(camera_index, cv2.CAP_DSHOW) + # 璁剧疆鍒嗚鲸鐜� + capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1921) + capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1081) + # 妫�鏌ユ憚鍍忓ご鏄惁鎴愬姛鎵撳紑 + if not capture.isOpened(): + print("鏃犳硶鎵撳紑鎽勫儚澶�2") + exit() + # 寰幆璇诲彇鐢婚潰 + while True: + ret, frame = capture.read() + if not ret: + print("鏃犳硶璇诲彇鎽勫儚澶寸敾闈�") + break + gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + + score = variance_of_laplacian(gray) + # ratio = frequency_domain_analysis(gray) + # edge_gradient_score = edge_gradient(gray) + # 灏咶PS缁樺埗鍦ㄥ浘鍍忎笂 + cv2.putText(frame, f"score={str(score)}" + , (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA) + # cv2.putText(frame, f"ratio={str(ratio)}" + # , (10, 70), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA) + # cv2.putText(frame, f"edge_gradient_score={str(edge_gradient_score)}" + # , (10, 110), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA) + # 鏄剧ず鐢婚潰 + cv2.imshow("Camera", frame) + # 妫�娴嬫寜閿紝濡傛灉鎸変笅q閿垯閫�鍑哄惊鐜� + if cv2.waitKey(1) & 0xFF == ord('q'): + break + # 鍏抽棴鎽勫儚澶� + capture.release() + # 鍏抽棴鎵�鏈夌獥鍙� + cv2.destroyAllWindows() + + +if __name__ == '__main__': + testOne() + # batch_score() + # cam_score() \ No newline at end of file diff --git a/model/herb_identify.onnx b/model/herb_identify.onnx new file mode 100644 index 0000000..4f61333 --- /dev/null +++ b/model/herb_identify.onnx Binary files differ diff --git a/model/loading.onnx b/model/loading.onnx new file mode 100644 index 0000000..9eb4848 --- /dev/null +++ b/model/loading.onnx Binary files differ diff --git a/test_herb_identify.py b/test_herb_identify.py new file mode 100644 index 0000000..7a5a342 --- /dev/null +++ b/test_herb_identify.py @@ -0,0 +1,32 @@ + +import os +import cv2 +import numpy as np +import onnxruntime + +if __name__ == '__main__': + img = cv2.imread("data/images/2/20250114200010.jpg") + identify_session = onnxruntime.InferenceSession("model/herb_identify.onnx") + herbs = eval(identify_session.get_modelmeta().custom_metadata_map['names']) + # 棰勫鐞� + herb_blob = cv2.dnn.blobFromImage(img, 1 / 255.0, (640, 640), swapRB=True, crop=False) + # 妯″瀷鎺ㄧ悊 + herb_outputs = identify_session.run(None, {identify_session.get_inputs()[0].name: herb_blob}) + herb_probabilities = herb_outputs[0] + + top_five_classes = np.argsort(herb_probabilities, axis=1)[0][-5:][::-1] + name = "" + for i, class_id in enumerate(top_five_classes): + # 淇濈暀涓や綅灏忔暟 + probability = round(herb_probabilities[0][class_id], 2) + + herb_class = herbs[class_id] + name = name + " " + herb_class + ":" + str(probability) + # 灏嗚瘑鍒粨鏋滄墦鍗板埌鍥剧墖涓� + cv2.putText(img, herb_class + ":" + str(probability), (10, (i+1)*40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) + + # 鏄剧ず鍥剧墖 + cv2.imshow("Output", img) + print(name) + # 绛夊緟 + cv2.waitKey(0) -- Gitblit v1.9.3