物体检测实战:使用 OpenCV 进行 YOLO 对象检测

# 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))

                # 更新边界框坐标、置信度和类 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) =外汇跟单gendan5.com (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()


请使用浏览器的分享功能分享到微信等