| | |
| | | # æå头ç»é¢æµè¯ç¨åº |
| | | |
| | | import os |
| | | |
| | | import cv2 |
| | | import time |
| | | import numpy as np |
| | |
| | | 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("æ æ³æå¼æå头") |
| | |
| | | # å°FPSç»å¶å¨å¾åä¸ |
| | | 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'): |
| | |
| | | cap.release() |
| | | |
| | | # å
³éææçªå£ |
| | | cv2.destroyAllWindows() |
| | | cv2.destroyAllWindows() |
| | |
| | | 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] |
| | | # è°æ´å¾å尺寸 |
| | | 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 |
¶Ô±ÈÐÂÎļþ |
| | |
| | | # 䏿è¯å«ï¼è·å䏿å¾çï¼è°ç¨è¯æè¯å«æ¨¡åï¼è¾åºè¯å«ç»æ |
| | | |
| | | |
| | | 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(): |
| | | # å建ä¸ä¸ª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, 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 = "æ£å¨ä¸æ" |
| | | # æ¯é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}") |
| | | # å°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() |
¶Ô±ÈÐÂÎļþ |
| | |
| | | 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) |
| | | # å°FPSç»å¶å¨å¾åä¸ |
| | | 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() |
¶Ô±ÈÐÂÎļþ |
| | |
| | | |
| | | 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) |