案例:
import cv2 import numpy as np # 加载预训练的YOLOv3模型 def load_yolo(): net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg") classes = [] with open("coco.names", "r") as f: classes = [line.strip() for line in f.readlines()] return net, classes # 检测函数 def detect_objects(img, net, classes): height, width, _ = img.shape blob = cv2.dnn.blobFromImage(img, 1/255.0, (416, 416), swapRB=True, crop=False) net.setInput(blob) output_layers_names = net.getUnconnectedOutLayersNames() layer_outputs = net.forward(output_layers_names) boxes = [] confidences = [] class_ids = [] for output in layer_outputs: for detection in output: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > 0.5: # 置信度阈值 center_x = int(detection[0] * width) center_y = int(detection[1] * height) w = int(detection[2] * width) h = int(detection[3] * height) x = int(center_x - w / 2) y = int(center_y - h / 2) boxes.append([x, y, w, h]) confidences.append(float(confidence)) class_ids.append(class_id) indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) # 非极大值抑制 return boxes, confidences, class_ids, indexes # 主函数 def main(): net, classes = load_yolo() cap = cv2.VideoCapture(0) # 打开默认摄像头 while True: ret, frame = cap.read() if not ret: break boxes, confidences, class_ids, indexes = detect_objects(frame, net, classes) for i in indexes.flatten(): x, y, w, h = boxes[i] label = str(classes[class_ids[i]]) confidence = confidences[i] color = (0, 255, 0) # 绿色边框 cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2) text = f"{label}: {confidence:.2f}" cv2.putText(frame, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2) cv2.imshow("Real-Time Object Detection", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() if __name__ == "__main__": main()