import cv2 import numpy as np import yaml from openvino.runtime import Core class SAFETY_DETECT: def __init__(self, path, conf_thres=0.35, iou_thres=0.5): self.conf_threshold = conf_thres self.iou_threshold = iou_thres # Initialize model self.initialize_model(path) self.color_palette = [(np.random.randint(0, 255), np.random.randint(0, 255), np.random.randint(0, 255)) for _ in range(100)] def __call__(self, image): return self.detect_objects(image) def read_config(self, path): file_path = path+'/metadata.yaml' with open(file_path, 'r', encoding="utf-8") as file: config = yaml.safe_load(file) return config def initialize_model(self, path): model_path = path + '/best.xml' # Initialize OpenVINO Runtime self.core = Core() # Load the model self.model = self.core.read_model(model=model_path) # Compile the model self.compiled_model = self.core.compile_model(model=self.model, device_name="CPU") # Get input and output layers self.input_layer = self.compiled_model.input(0) self.output_layer = self.compiled_model.output(0) # Get class names self.class_names = CLASSES = self.read_config(path)['names'] def detect_objects(self, frame): # 获得模型输出节点 ir = self.compiled_model.create_infer_request() [height, width, _] = frame.shape length = max((height, width)) image = np.zeros((length, length, 3), np.uint8) image[0:height, 0:width] = frame self.scale = length / 640 blob = cv2.dnn.blobFromImage(image, scalefactor=1 / 255, size=(640, 640), swapRB=True) outputs = ir.infer(blob)[self.output_layer] outputs = np.array([cv2.transpose(outputs[0])]) rows = outputs.shape[1] boxes = [] scores = [] class_ids = [] for i in range(rows): classes_scores = outputs[0][i][4:] (minScore, maxScore, minClassLoc, (x, maxClassIndex)) = cv2.minMaxLoc(classes_scores) if maxScore >= 0.25: box = [outputs[0][i][0] - (0.5 * outputs[0][i][2]), outputs[0][i][1] - (0.5 * outputs[0][i][3]), outputs[0][i][2], outputs[0][i][3]] boxes.append(box) scores.append(maxScore) class_ids.append(maxClassIndex) return boxes, scores, class_ids def draw_bounding_box(self, img, class_id, confidence, x, y, x_plus_w, y_plus_h): label = f'{self.class_names[class_id]} ({confidence:.2f})' color = self.color_palette[class_id] cv2.rectangle(img, (x, y), (x_plus_w, y_plus_h), color, 2) cv2.putText(img, label, (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2) def draw_detections(self, frame, class_ids, scores, boxes): result_boxes = cv2.dnn.NMSBoxes(boxes, scores, 0.25, 0.45, 0.5) for i in range(len(result_boxes)): index = result_boxes[i] box = boxes[index] self.draw_bounding_box(frame, class_ids[index], scores[index], round(box[0] * self.scale), round(box[1] * self.scale), round((box[0] + box[2]) * self.scale), round((box[1] + box[3]) * self.scale)) return frame