物體檢測實戰:使用 OpenCV 進行 YOLO 物件檢測

專注的阿熊發表於2022-01-10

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


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2851561/,如需轉載,請註明出處,否則將追究法律責任。

相關文章