Плотный оптический поток и обнаружение объектов на видео с использованием Python - PullRequest
1 голос
/ 20 октября 2019

Я пытаюсь использовать плотный оптический поток и обнаружение объектов в видео для каждого кадра. В основном, если я использую только Обнаружение объектов с использованием модели Yolo, это не даст ограничивающую рамку для каждого кадра видео. Иногда это упускается. Чтобы сделать его гладким, я хочу использовать плотный оптический поток, чтобы, если для некоторого кадра Йоло не обнаружил объект, то выход оптического потока можно было использовать для создания ограничительной рамки объекта. Ниже приведен код, который я использую для интеграции плотного оптического потока и обнаружения объектов с помощью Yolo.

import numpy as np
import argparse
import imutils
import time
import cv2
import os
import sys
%matplotlib inline
from matplotlib import pyplot as plt
confidence = 0.5
threshold= 0.3
labelsPath = "C:/Users/Akash Jain/Downloads/yolo-object-detection/yolo-coco/coco.names"
LABELS = open(labelsPath).read().strip().split("\n")
np.random.seed(42)
COLORS = np.random.randint(0, 255, size=(len(LABELS), 3),dtype="uint8")
weightsPath = "C:/Users/Akash Jain/Downloads/yolo-object-detection/yolo-coco/yolov3.weights"
configPath = "C:/Users/Akash Jain/Downloads/yolo-object-detection/yolo-coco/yolov3.cfg"
# load our YOLO object detector trained on COCO dataset (80 classes)
print("[INFO] loading YOLO from disk...")
net = cv2.dnn.readNetFromDarknet(configPath, weightsPath)
ln = net.getLayerNames()
ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]
np.set_printoptions(threshold=sys.maxsize)
writer = None
(W, H) = (None, None)
cap = cv2.VideoCapture("C:/Users/Akash Jain/Downloads/yolo-object-detection/videos/overpass.mp4")
try:
    prop = cv2.cv.CV_CAP_PROP_FRAME_COUNT if imutils.is_cv2() else cv2.CAP_PROP_FRAME_COUNT
    total = int(cap.get(prop))    
    print("[INFO] {} total frames in video".format(total))
except:
    print("[INFO] could not determine # of frames in video")
    total = -1
# ret = a boolean return value from getting the frame, first_frame = the first frame in the entire video sequence
ret, first_frame = cap.read()
# Converts frame to grayscale because we only need the luminance channel for detecting edges - less computationally expensive
prev_gray = cv2.cvtColor(first_frame, cv2.COLOR_BGR2GRAY)
# Creates an image filled with zero intensities with the same dimensions as the frame
mask = np.zeros_like(first_frame)
# Sets image saturation to maximum
mask[..., 1] = 255
dir_name='C:/Users/Akash Jain/Documents/ZED/Split'
base_filename='output'
filename_suffix = 'png'
fno=1
while(cap.isOpened()):    
    ret, frame = cap.read()
    if not ret:
        break
    if W is None or H is None:
        (H, W) = frame.shape[:2]
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # Calculates dense optical flow by Farneback method
    # https://docs.opencv.org/3.0-beta/modules/video/doc/motion_analysis_and_object_tracking.html#calcopticalflowfarneback
    flow = cv2.calcOpticalFlowFarneback(prev_gray, gray, None, 0.5, 3, 15, 3, 5, 1.2, 0)
    # Computes the magnitude and angle of the 2D vectors
    magnitude, angle = cv2.cartToPolar(flow[..., 0], flow[..., 1])
    # Sets image hue according to the optical flow direction
    mask[..., 0] = angle * 180 / np.pi / 2
    # Sets image value according to the optical flow magnitude (normalized)
    mask[..., 2] = cv2.normalize(magnitude, None, 0, 255, cv2.NORM_MINMAX)
    # Converts HSV to RGB (BGR) color representation
    rgb = cv2.cvtColor(mask, cv2.COLOR_HSV2BGR) 
    # Opens a new window and displays the output frame
    blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416),swapRB=True, crop=False)
    net.setInput(blob)
    start = time.time()
    layerOutputs = net.forward(ln)
    end = time.time()
    boxes = []
    confidences = []
    classIDs = []    
    for output in layerOutputs:       
        for detection in output:            
            scores = detection[5:]
            classID = np.argmax(scores)           
            confidence = scores[classID]
            if confidence > 0.5:
                box = detection[0:4] * np.array([W, H, W, H])
                (centerX, centerY, width, height) = box.astype("int")
                x = int(centerX - (width / 2))
                y = int(centerY - (height / 2))
                boxes.append([x, y, int(width), int(height)])
                confidences.append(float(confidence))
                classIDs.append(classID)
            idxs = cv2.dnn.NMSBoxes(boxes, confidences, 0.5,0.3)            
            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)

    print("frame",fno) 
    #print(text)
    print("boxes",boxes)
    print("confidence",confidences)
    print("classes",classIDs)
    print("object detection") 
    print(frame.shape)
    #print(frame)
    print("optical")
    #print(rgb)
    print(rgb.shape)
    output = ((1 * frame) + (0.5 * rgb)).astype("uint8")
    newpath= os.path.join(dir_name, str(fno) + "." + filename_suffix)
    figure = plt.figure(figsize=(15,15))
    plt.imshow(output)    
    plt.show() 
    prev_gray = gray
    fno = int(fno)+1
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

Если я запусту этот код, я получу следующий вывод:

[INFO] loading YOLO from disk...
[INFO] 812 total frames in video
frame 1
boxes [[622, 264, 14, 12], [675, 275, 20, 20], [574, 293, 22, 18], [495, 334, 49, 29], [525, 374, 49, 50]]
confidence [0.8653988242149353, 0.6596917510032654, 0.9037992358207703, 0.9345515966415405, 0.8292896747589111]
classes [2, 2, 2, 2, 2]
object detection
(720, 1280, 3)
optical
(720, 1280, 3)

enter image description here

frame 2
boxes [[622, 264, 14, 12], [674, 275, 21, 20], [573, 293, 23, 18], [495, 334, 50, 29], [518, 378, 57, 52]]
confidence [0.8665716648101807, 0.6463424563407898, 0.9210211038589478, 0.8402170538902283, 0.5718783140182495]
classes [2, 2, 2, 2, 2]
object detection
(720, 1280, 3)
optical
(720, 1280, 3)

enter image description here

Любой вход, который может помочь мне использовать вывод плотного оптического потока, чтобы создать ограничивающую рамку дляобъект, если он пропущен моделью обнаружения объекта, так что он обнаруживает объект в каждом кадре. Причина, по которой я это делаю, состоит в том, чтобы сделать описание сценария дороги. Например, на дороге 3 машины, одна неподвижна, а 2 находятся в движении.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...