关于python:物体检测实战使用-OpenCV-进行-YOLO-对象检测

8次阅读

共计 1968 个字符,预计需要花费 5 分钟才能阅读完成。

loop over frames from the video file stream

while True:

# 从文件中读取下一帧
(grabbed, frame) = vs.read()
# 如果帧没有被抓取,那么曾经到了流的开端
if not grabbed:
    break
# 如果框架尺寸为空,则给他们赋值
if W is None or H is None:
    (H, W) = frame.shape[:2]
# 从输出帧结构一个 blob,而后执行 YOLO 对象检测器的前向传递,失去边界框和相干概率
blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416),
                             swapRB=True, crop=False)
net.setInput(blob)
start = time.time()
layerOutputs = net.forward(outInfo)
end = time.time()

# 别离初始化检测到的边界框、置信度和类 ID 的列表

boxes = []
confidences = []
classIDs = []
# 循环输入
for output in layerOutputs:
    # 遍历每个检测后果
    for detection in output:
        # 提取物体检测的类 ID 和置信度(即概率)scores = detection[5:]
        classID = np.argmax(scores)
        confidence = scores[classID]
         # 过滤精度低的后果
        if confidence > confidence_t:
           # 缩放边界框坐标,计算 YOLO 边界框的核心 (x, y) 坐标,而后是框的宽度和高度
            box = detection[0:4] * np.array([W, H, W, H])
            (centerX, centerY, width, height) = box.astype("int")
            # 应用核心 (x, y) 坐标导出边界框的上角和左角
            x = int(centerX - (width / 2))
            y = int(centerY - (height / 2))
           # 更新边界框坐标、[股指期货](https://www.gendan5.com/ff/sf.html) 置信度和类 ID 列表
            boxes.append([x, y, int(width), int(height)])
            confidences.append(float(confidence))
            classIDs.append(classID)
# 应用非极大值克制来克制弱的、重叠的边界框
idxs = cv2.dnn.NMSBoxes(boxes, confidences, confidence_t,
                        threshold)
# 确保至多存在一个检测
if len(idxs) > 0:
    # 遍历保留的索引
    for i in idxs.flatten():
        # 在图像上绘制一个边界框矩形和标签
        (x, y) = (boxes[i][0], boxes[i][1])
        (w, h) = (boxes[i][2], boxes[i][3])
       # 确保至多存在一个检测
        color = [int(c) for c in COLORS[classIDs[i]]]
        cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
        text = "{}: {:.4f}".format(LABELS[classIDs[i]],
                                   confidences[i])
        cv2.putText(frame, text, (x, y - 5),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
# check if the video writer is None
if writer is None:
    # initialize our video writer
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    writer = cv2.VideoWriter('output.avi', fourcc, 30, (int(frame.shape[1]), int(frame.shape[0])))
    # some information on processing single frame
    if total > 0:
        elap = (end - start)
        print("[INFO] single frame took {:.4f} seconds".format(elap))
        print("[INFO] estimated total time to finish: {:.4f}".format(elap * total))
# write the output frame to disk
writer.write(frame)

release the file pointers

print(“[INFO] cleaning up…”)
writer.release()
vs.release()

正文完
 0