본문 바로가기

Study/class note

딥러닝 / 동영상 object detection

# 동영상 오브젝트 디텍션

import cv2
import numpy as np

LABELS = ['gorani','raccoon']
#gorani, raccoon

CONFIDENCE = 0.3
THRESHOLD = 0.3  #NMS(Num Max Suppression)

net = cv2.dnn.readNetFromDarknet('c:\\animal\\yolov4_custom.cfg','c:\\animal\\yolov4_custom_final.weights')

classes =['gorani','raccoon']

#cap = cv2.VideoCapture('D:\\test\\New folder\\test1.mp4')
cap = cv2.VideoCapture('c:\\animal\\test2.mp4') #원본 동영상 위치, 이름 
font = cv2.FONT_HERSHEY_PLAIN  # 디텍션할때 나타나는 폰트
colors = np.random.uniform(0, 255, size=(100, 3))  # 폰트 컬러

# 동영상 프레임의 가로와 세로 사이즈 변수에 담음
frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) 
fourcc = cv2.VideoWriter_fourcc(*'mp4v')  # <-------------------  이 부분 
fps = cap.get(cv2.CAP_PROP_FPS) #동영상을 프레임별로 가져옴

# 결과 동영상 출력
out = cv2.VideoWriter('c:\\animal\\detections.mp4', fourcc, fps, (int(frame_size[0]), int(frame_size[1])))

while True:
    ret, img = cap.read()
    if not ret:
        break
    # height, width, _ = img.shape
    width,height = (cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    

    blob = cv2.dnn.blobFromImage(img, 1/255, (416, 416), (0,0,0), swapRB=True, crop=False)
    net.setInput(blob)
    output_layers_names = net.getUnconnectedOutLayersNames()
    layerOutputs = net.forward(output_layers_names)

    boxes = []
    confidences = []
    class_ids = []

    for output in layerOutputs:
        for detection in output:
            scores = detection[5:]
            class_id = np.argmax(scores)
            confidence = scores[class_id]
            if confidence > 0.2:
                center_x = int(detection[0]*width)
                center_y = int(detection[1]*height)
                w = int(detection[2]*width)
                h = int(detection[3]*height)

                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.2, 0.4)

    if len(indexes)>0:
        for i in indexes.flatten():
            x, y, w, h = boxes[i]
            label = str(classes[class_ids[i]])
            confidence = str(round(confidences[i],2))
            color = colors[i]
            cv2.rectangle(img, (x,y), (x+w, y+h), color, 2)
            cv2.putText(img, label + " " + confidence, (x, y+20), font, 2, (255,255,255), 2)

    cv2.imshow('Image', img)
    
    #cap = cv2.VideoCapture(input_names[0])
    #win_name = 'Video detection'
    #cv2.namedWindow(win_name) 
   
    out.write(img)
    
    key = cv2.waitKey(1)
    if key==27:
        break
        
        
out.release()        
cv2.destroyAllWindows()

 

반응형