簡體   English   中英

使用 yolo 檢測視頻中的對象 model

[英]Detect objects in a video using a yolo model

我創建了一個簡單的 object 檢測 model 使用 yolo v3 預訓練 model 檢測單個圖像中的對象。下面是 model 的 python 代碼,

import cv2
import numpy as np

# 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()]
layer_names = net.getLayerNames()
output_layers= [layer_names[i-1] for i in net.getUnconnectedOutLayers()]
colors = np.random.uniform(0,255,size=(len(classes),3))

img= cv2.imread("heyyy.jpg")
height,width,channels = img.shape
blob= cv2.dnn.blobFromImage(img,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 = 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)
        cv2.circle(img,(center_x,center_y),10,(0,255,0),2)

        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)
 print(indexes)
 font=cv2.FONT_HERSHEY_PLAIN
 for i in range (len(boxes)):
  if i in indexes:   
    x,y,w,h = boxes[i]
    label = str(classes[class_ids[i]])
    color = colors[i]
    cv2.rectangle(img,(x,y),(x+w,y+h),color,2)
    cv2.putText(img,label,(x,y+30),font,3,color,3)

cv2.imshow("Image",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

對於任何給定的圖像,model 都能完美地識別對象。 如何讓 model 用於視頻 (.mp4) 文件? 請幫忙!

只需逐幀閱讀視頻。 將每一幀饋送到 model 和 plot 結果:

import cv2
import numpy as np
 
# Create a VideoCapture object and read from input file
# If the input is the camera, pass 0 instead of the video file name
cap = cv2.VideoCapture('your/video/path.mp4')
 
# Check if camera opened successfully
if (cap.isOpened()== False): 
  print("Error opening video stream or file")
 
# Read until video is completed
while(cap.isOpened()):
  # Capture frame-by-frame
  ret, frame = cap.read()
  if ret == True:
 
    # Read frame
    img = cv2.imread('Frame',frame)

    # Feed frame to model
    outs = net.forward(img)

    # plot your results...

    # Display frame
    # cv2.imshow('Frame',frame)
 
    # Press Q on keyboard to  exit
    if cv2.waitKey(25) & 0xFF == ord('q'):
      break
 
  # Break the loop
  else: 
    break
 
# When everything done, release the video capture object
cap.release()
 
# Closes all the frames
cv2.destroyAllWindows()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM