diff --git a/005查看yolo格式目标检测框.py b/005查看yolo格式目标检测框.py index e368cf8..58e8b0c 100644 --- a/005查看yolo格式目标检测框.py +++ b/005查看yolo格式目标检测框.py @@ -3,11 +3,11 @@ import cv2 import numpy as np # 总的检测根目录 -path_root_labels = '/home/admin-root/haotian/python哈汽锻8安全帽识别/安全帽头数据集20250428/Safty/train/labels_raw' +path_root_labels = '/home/admin-root/haotian/python哈汽锻8安全帽识别/安全帽头数据集20250428/HardHatsV1/train/labels_helmet_head_shoe' # 总的检测根目录 -path_root_imgs = '/home/admin-root/haotian/python哈汽锻8安全帽识别/安全帽头数据集20250428/Safty/train/images' +path_root_imgs = '/home/admin-root/haotian/python哈汽锻8安全帽识别/安全帽头数据集20250428/HardHatsV1/train/images' -path_root_save = '/home/admin-root/haotian/python哈汽锻8安全帽识别/安全帽头数据集20250428/Safty/train/show' +path_root_save = '/home/admin-root/haotian/python哈汽锻8安全帽识别/安全帽头数据集20250428/HardHatsV1/train/show' remove_path = 'fire-8/remove/valid.txt' @@ -24,8 +24,10 @@ for ii in os.walk(path_root_imgs): label_name = j[:-4] + type_object path_label = os.path.join(path_root_labels, label_name) # print(path_label) - f = open(path_label, 'r+', encoding='utf-8') - if os.path.exists(path_label) == True: + + if os.path.exists(path_label): + + f = open(path_label, 'r+', encoding='utf-8') img = cv2.imdecode(np.fromfile(path_img, dtype=np.uint8), -1) w = img.shape[1] @@ -52,5 +54,8 @@ for ii in os.walk(path_root_imgs): break # cv2.imshow("show", img_tmp) # c = cv2.waitKey(0) - cv2.imwrite(f'{path_root_save}/{j}', img_tmp) + try: + cv2.imwrite(f'{path_root_save}/{j}', img_tmp) + except: + print(f"写入图片失败: {j}") # print(f'写入文件{j}') diff --git a/008yolov8预测.py b/008yolov8预测.py index 5cdcc26..9d119b0 100644 --- a/008yolov8预测.py +++ b/008yolov8预测.py @@ -7,11 +7,13 @@ from ultralytics import YOLO ''' -model = YOLO('/home/admin-root/haotian/python哈汽锻8安全帽识别/HeadShoes/trainshoeV2/weights/best.pt') +model = YOLO('/home/admin-root/haotian/python哈汽锻8安全帽识别/HelmetHeadBAC/trainHardHatsV1HelmetHeadShoe2/weights/best.pt') -images_path = '/home/admin-root/haotian/锻8/tensorrtx/yolov8/images' - -save_path = '/home/admin-root/haotian/python哈汽锻8安全帽识别/output/outputShoeV2' +# images_path = '/home/admin-root/haotian/锻8/tensorrtx/yolov8/images' +# images_path = '/home/admin-root/haotian/锻8/tensorrtx/yolov8/images_20250623' +images_path = '/home/admin-root/haotian/python哈汽锻8安全帽识别/test_images/duan8_real' +# save_path = '/home/admin-root/haotian/python哈汽锻8安全帽识别/output/outputHardHatsV1HelmetHeadShoe2_20250623' +save_path = '/home/admin-root/haotian/python哈汽锻8安全帽识别/output/outputHardHatsV1HelmetHeadShoe2_20250623_1' all_images = [os.path.join(images_path, t) for t in os.listdir(images_path)] diff --git a/026保留指定类别的标签.py b/026保留指定类别的标签.py index 8405d06..9d1aa8d 100644 --- a/026保留指定类别的标签.py +++ b/026保留指定类别的标签.py @@ -2,12 +2,9 @@ import os # 类别名称列表 -class_names = ['badaoxie', 'banxie', 'baomihuaxie', 'heiyaoshixie', 'huoxingxie', 'jianzhenxie', 'maozhuaxie', 'mianhuatangxie', 'mofangxie', 'moshutiexie', 'qidianxie', 'ranshaoxie', 'shuinixie', 'wangmianxie', 'xiaobaixie', 'xiaochujuxie', 'xiaomaixie', 'yingtangxie', 'yinsuxie', 'yuanyangxie', 'yucixie'] # 替换为你的类别名称 -target_class = ['badaoxie', 'banxie', 'baomihuaxie', 'heiyaoshixie', 'huoxingxie', 'jianzhenxie', 'maozhuaxie', 'mianhuatangxie', 'mofangxie', 'moshutiexie', 'qidianxie', 'ranshaoxie', 'shuinixie', 'wangmianxie', 'xiaobaixie', 'xiaochujuxie', 'xiaomaixie', 'yingtangxie', 'yinsuxie', 'yuanyangxie', 'yucixie'] # 目标类别 -target_class_id = {'badaoxie': 2, 'banxie':2, 'baomihuaxie':2, 'heiyaoshixie':2, 'huoxingxie':2, - 'jianzhenxie':2, 'maozhuaxie':2, 'mianhuatangxie':2, 'mofangxie':2, 'moshutiexie':2, - 'qidianxie':2, 'ranshaoxie':2, 'shuinixie':2, 'wangmianxie':2, 'xiaobaixie':2, - 'xiaochujuxie':2, 'xiaomaixie':2, 'yingtangxie':2, 'yinsuxie':2, 'yuanyangxie':2, 'yucixie':2} # 目标类别的新 ID +class_names = ['head', 'helmet', 'shoe'] # 替换为你的类别名称 +target_class = ['head', 'helmet', 'shoe'] # 目标类别 +target_class_id = {'head': 1, 'helmet':0, 'shoe':2} # 目标类别的新 ID def filter_and_rename_labels(label_directory, output_label_directory): if not os.path.exists(output_label_directory): @@ -35,6 +32,6 @@ def filter_and_rename_labels(label_directory, output_label_directory): file.writelines(filtered_lines) # 示例用法 -label_directory = '/home/admin-root/haotian/python哈汽锻8安全帽识别/Shoes/shoe.v2i.yolov8/valid/labels_raw' # 替换为你的标签文件夹路径 -output_label_directory = '/home/admin-root/haotian/python哈汽锻8安全帽识别/Shoes/shoe.v2i.yolov8/valid/labels' # 替换为你希望保存过滤后标签的路径 +label_directory = '/home/admin-root/haotian/python哈汽锻8安全帽识别/DatasetHelmetHeadShoe/labels_raw' # 替换为你的标签文件夹路径 +output_label_directory = '/home/admin-root/haotian/python哈汽锻8安全帽识别/DatasetHelmetHeadShoe/labels' # 替换为你希望保存过滤后标签的路径 filter_and_rename_labels(label_directory, output_label_directory) diff --git a/030将yolo预测结果写为label_studio_json格式2.py b/030将yolo预测结果写为label_studio_json格式2.py index f7561e6..3dcbdad 100644 --- a/030将yolo预测结果写为label_studio_json格式2.py +++ b/030将yolo预测结果写为label_studio_json格式2.py @@ -62,6 +62,9 @@ if __name__ == "__main__": # 初始化模型 # model_0 = YOLO('/home/admin-root/haotian/python哈汽锻8安全帽识别/HelmetHeadBAC/trainHardHatsV1/weights/best.pt') + # 头,安全帽 + model_0 = YOLO('/home/admin-root/haotian/python哈汽锻8安全帽识别/HelmetHeadBAC/trainHardHatsV12/weights/best.pt') + # 检测鞋的模型 model_1 = YOLO('/home/admin-root/haotian/劳保鞋识别/jingzhu_245_1112/tensorrtx-master/yolov8/build_1113/best.pt') @@ -74,12 +77,11 @@ if __name__ == "__main__": "shoe", ] - # 头,安全帽 - model_0 = YOLO('/home/admin-root/haotian/python哈汽锻8安全帽识别/HelmetHeadBAC/trainHardHatsV1/weights/best.pt') + - images_path = '/home/admin-root/haotian/python哈汽锻8安全帽识别/安全帽头数据集20250428/HardHatsV1/train/images' + images_path = '/home/admin-root/haotian/劳保鞋识别/dataset/new_all/train/images' - save_path = '/home/admin-root/haotian/python哈汽锻8安全帽识别/output/outputShoeV2' + # save_path = '/home/admin-root/haotian/python哈汽锻8安全帽识别/output/outputShoeV2' image_names = os.listdir(images_path) @@ -91,13 +93,13 @@ if __name__ == "__main__": # conf 置信度在. results_0 = model_0(all_images[i], - conf = 0.2, device=0) + conf = 0.5, device=0) predictions_0 = yolo_to_labelstudio(results_0, CLASS_NAMES_0) results_1 = model_1(all_images[i], - conf=0.2, device=0) + conf=0.5, device=0) predictions_1 = yolo_to_labelstudio(results_1, CLASS_NAMES_1) @@ -105,18 +107,19 @@ if __name__ == "__main__": t_d = dict() - t_d["data"] = {"image": f"/data/local-files/?d=HardHatsV1/train/images/{image_names[i]}"} + # "劳保鞋识别/dataset/new_all/train/images/dataset2_-01-15-2-2-2-2-25_jpg.rf.267991c87898d90733d5c7623941a999.jpg" + t_d["data"] = {"image": f"/data/local-files/?d=劳保鞋识别/dataset/new_all/train/images/{image_names[i]}"} t_d["predictions"] = list() t_d["predictions"].append(predictions_0) json_list.append(t_d) - if i == 10: - break + # if i == 10: + # break -with open("test_json.json", "w", encoding="utf-8") as f: +with open("label_studio_new_all.json", "w", encoding="utf-8") as f: json.dump(json_list,f, indent=4, # 4空格缩进 diff --git a/032测试post请求.py b/032测试post请求.py new file mode 100644 index 0000000..5f871b9 --- /dev/null +++ b/032测试post请求.py @@ -0,0 +1,74 @@ +import datetime +import requests +import json +import yaml +import uuid +import time + + +with open('config.yaml', 'r') as file: + configData = yaml.safe_load(file) + +tokenResult = {} +getTokenUrl = configData['dataConfig']['getTokenUrl'] +vod_channelNo = configData['video_config']['v1_channelNo'] +# 告警信息url +putMessageUrl = configData['dataConfig']['putMessageUrl'] + +def get_token(tokenResult): + if 'token' in tokenResult and 'current_time' in tokenResult: + token_time = datetime.datetime.strptime(tokenResult['current_time'], + "%Y-%m-%d %H:%M:%S") + current_time = datetime.datetime.now() + time_diff = current_time - token_time + if time_diff.total_seconds() > 20 * 60: + # 过期重新请求 token + # print("token 已过期") + response = requests.post(getTokenUrl) + if response.status_code == 200: + data = json.loads(response.text) + if 'retCode' in data and data['retCode'] == '200': + token = data['responseBody']['token'] + current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + tokenResult['token'] = token + tokenResult['current_time'] = current_time + else: + tokenResult['error'] = data['errorDesc'] + else: + tokenResult['error'] = response.status_code + token = tokenResult['token'] + return token + + +def send_post_request(url, token, msg, picUrl, videoUrl): + payload = { + "tenantCode": "8", + "channelNo": vod_channelNo, + "alarmContent": msg, + "alarmTime": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + "picInfo": [ + {"url": picUrl} + ], + "videoInfo": [ + {"url": videoUrl} + ] + } + headers = { + 'X-Access-Token': token, + 'Content-Type': 'application/json' + } + # print(url) + # print(headers) + # print(payload) + response = requests.post(url, headers=headers, data=json.dumps(payload)) + print(response) + + +if __name__ == '__main__': + token = get_token() + print("token: ", token) + + uuid_str = str(uuid.uuid4())[:6] + str(int(time.time())) # 生成UUID的前6位 + + upload_http_url_img = configData['minioConfig']['bucket_name'] + f'/{uuid_str}_{vod_channelNo}_.jpg' + send_post_request(putMessageUrl, token, "消息内容", upload_http_url_img, '') \ No newline at end of file diff --git a/033读取mp4帧保存成图片.py b/033读取mp4帧保存成图片.py new file mode 100644 index 0000000..4a6aeb4 --- /dev/null +++ b/033读取mp4帧保存成图片.py @@ -0,0 +1,37 @@ + +# url = + +import cv2 + +# 打开视频文件 +# video_path = "/home/admin-root/haotian/锻8/tensorrtx/yolov8/video/180.50.13.253_07_2025042215303747.mp4" +video_path = "/home/admin-root/haotian/锻8/tensorrtx/yolov8/video/测试视频20250621.mp4" +cap = cv2.VideoCapture(video_path) + +# 检查是否成功打开视频 +if not cap.isOpened(): + print("Error: Could not open video.") + exit() + + +n = 0 + +# 逐帧读取视频 +while True: + # 读取一帧,ret为是否读取成功,frame为帧数据 + ret, frame = cap.read() + + # 如果读取失败(如视频结束),退出循环 + if not ret: + print("Reached end of video.") + break + + # 在此处处理帧数据(例如显示、保存、分析等) + cv2.imwrite(f"./images_20250623/mp4_{n}.jpg", frame) + print(f"保存图片{n}") + n += 1 + + +# 释放资源 +cap.release() +# cv2.destroyAllWindows() \ No newline at end of file