- OPenVINO2022.1OPenVINO2022.1环境搭建-优快云博客
- TensorRT8.4 TensorRT环境搭建-优快云博客
- ONNXRUNTIME-GPU1.7~1.12Visual stdio2022 opencv cude pytroch与yolov8/可视化工具的环境搭建,不搞VIP,我也要当雷锋-优快云博客
- OPenCV DNN4.6以上
在ptcharm部署 openvino来实现对象检测
001
output_video
源码
import cv2 as cv import time import numpy as np import openvino as ov def load_classes(): try: # 请根据实际情况修改为 classes.txt 的绝对路径 file_path = "C:/python/yolov8/classes.txt" with open(file_path, "r") as f: class_list = [cname.strip() for cname in f.readlines()] return class_list except FileNotFoundError: print(f"未找到 {file_path} 文件,请检查文件路径是否正确。") return [] def format_yolov8(frame): row, col, _ = frame.shape _max = max(col, row) result = np.zeros((_max, _max, 3), np.uint8) result[0:row, 0:col] = frame return result class_list = load_classes() if not class_list: print("由于未找到类别文件,程序无法正常运行。") exit(1) colors = [(255, 255, 0), (0, 255, 0), (0, 255, 255), (255, 0, 0)] # 初始化 OpenVINO 核心 core = ov.Core() available_devices = core.available_devices print("可用设备:", available_devices) try: # 读取模型 model = core.read_model(model="C:/python/yolov8/yolov8n.onnx") # 请根据实际情况修改为模型的绝对路径 # 编译模型 compiled_model = core.compile_model(model=model, device_name="CPU") # 获取输出层 output_layer = compiled_model.output(0) except Exception as e: print(f"模型加载或编译失败: {e}") exit(1) # 打开视频文件 capture = cv.VideoCapture("C:/python/yolov8/001.mp4") if not capture.isOpened(): print("无法打开视频文件,请检查文件路径是否正确。") exit(1) # 获取视频的帧率、宽度和高度 fps = capture.get(cv.CAP_PROP_FPS) width = int(capture.get(cv.CAP_PROP_FRAME_WIDTH)) height = int(capture.get(cv.CAP_PROP_FRAME_HEIGHT)) # 指定保存视频的文件夹和文件名 output_folder = "C:/python/yolov8/2/" # 请根据实际情况修改为你要保存的文件夹路径 output_filename = output_folder + "output_video.mp4" # 创建 VideoWriter 对象 fourcc = cv.VideoWriter_fourcc(*'mp4v') out = cv.VideoWriter(output_filename, fourcc, fps, (width, height)) while True: ret, frame = capture.read() if not ret or frame is None: print("视频流结束或读取帧失败。") break bgr = format_yolov8(frame) img_h, img_w, img_c = bgr.shape start = time.time() image = cv.dnn.blobFromImage(bgr, 1 / 255.0, (640, 640), swapRB=True, crop=False) try: res = compiled_model([image])[output_layer] # 1x84x8400 except Exception as e: print(f"推理过程中出现错误: {e}") break rows = np.squeeze(res, 0).T class_ids = [] confidences = [] boxes = [] x_factor = img_w / 640 y_factor = img_h / 640 for r in range(rows.shape[0]): row = rows[r] classes_scores = row[4:] _, _, _, max_indx = cv.minMaxLoc(classes_scores) class_id = max_indx[1] if classes_scores[class_id] > 0.25: confidences.append(classes_scores[class_id]) class_ids.append(class_id) x, y, w, h = row[0].item(), row[1].item(), row[2].item(), row[3].item() left = int((x - 0.5 * w) * x_factor) top = int((y - 0.5 * h) * y_factor) width = int(w * x_factor) height = int(h * y_factor) box = np.array([left, top, width, height]) boxes.append(box) indexes = cv.dnn.NMSBoxes(boxes, confidences, 0.25, 0.45) for index in indexes: box = boxes[index] color = colors[int(class_ids[index]) % len(colors)] cv.rectangle(frame, tuple(box[:2]), (box[0] + box[2], box[1] + box[3]), color, 2) cv.rectangle(frame, (box[0], box[1] - 20), (box[0] + box[2], box[1]), color, -1) cv.putText(frame, class_list[class_ids[index]], (box[0], box[1] - 10), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0)) end = time.time() inf_end = end - start fps = 1 / inf_end fps_label = "FPS: %.2f" % fps cv.putText(frame, fps_label, (20, 45), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) cv.imshow("YOLOv8+OpenVINO2022.x Object Detection", frame) # 将处理后的帧写入输出视频文件 out.write(frame) cc = cv.waitKey(1) if cc == 27: break # 释放资源 cv.destroyAllWindows() capture.release() out.release()