import cv2
# 加载预训练模型(例如YOLOv3)
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
# 加载COCO数据集类别标签
with open("coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
# 配置模型的输入和输出
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
# 打开摄像头
cap = cv2.VideoCapture(0)
while True:
# 读取帧
ret, frame = cap.read()
# 对帧进行预处理(缩放、调整色彩空间等)
blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
# 将预处理后的帧输入模型中进行前向传播
net.setInput(blob)
outs = net.forward(output_layers)
# 解析模型的输出并绘制边界框
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence &
标签:视频,layer,ai,模型,cv2,2024,416,names,net
From: https://blog.csdn.net/qq_30373537/article/details/141038415