最佳物体检测和跟踪算法[关闭]

问题描述 投票:-5回答:4

我是Opencv的新手。我想创建一个跟踪足球运动员的物体检测算法。我想知道那个球员是谁的球衣号码。我想知道找到它的最好方法。我应该使用哪种算法。我做了一个跟踪用户颜色范围的项目,我将每个视频图像转换为hsv。但是我遇到的挑战是,在检测到球员后我怎样才能找到球衣号码。

这是我的代码 -

#Import libraries
import cv2
import os
import numpy as np

# import the necessary packages
from collections import deque
import numpy as np
import cv2
import imutils
import time


#Reading the video
vidcap = cv2.VideoCapture('football.mp4')
success,image = vidcap.read()

count = 0
success = True
idx = 0

#Read the video frame by frame
while success:
    #converting into hsv image
    hsv = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
    #green range
    lower_green = np.array([40,40, 40])
    upper_green = np.array([70, 255, 255])
    #blue range
    lower_blue = np.array([110,50,50])
    upper_blue = np.array([130,255,255])

    #Red range
    lower_red = np.array([0,31,255])
    upper_red = np.array([176,255,255])

    #white range
    lower_white = np.array([0,0,0])
    upper_white = np.array([0,0,255])

    #Define a mask ranging from lower to uppper
    mask = cv2.inRange(hsv, lower_green, upper_green)
    #Do masking
    res = cv2.bitwise_and(image, image, mask=mask)

    #convert to hsv to gray
    res_bgr = cv2.cvtColor(res,cv2.COLOR_HSV2BGR)
    res_gray = cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)

    #Defining a kernel to do morphological operation in threshold image to 
    #get better output.
    kernel = np.ones((13,13),np.uint8)
    thresh = cv2.threshold(res_gray,127,255,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
    thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)


    #find contours in threshold image     
    im2,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

    prev = 0
    font = cv2.FONT_HERSHEY_SIMPLEX

    for c in contours:
        x,y,w,h = cv2.boundingRect(c)
        #Detect players
        if(h>=(1.5)*w):
            if(w>15 and h>= 15):
                idx = idx+1
                player_img = image[y:y+h,x:x+w]
                player_hsv = cv2.cvtColor(player_img,cv2.COLOR_BGR2HSV)
                #If player has blue jersy
                mask1 = cv2.inRange(player_hsv, lower_blue, upper_blue)
                res1 = cv2.bitwise_and(player_img, player_img, mask=mask1)
                res1 = cv2.cvtColor(res1,cv2.COLOR_HSV2BGR)
                res1 = cv2.cvtColor(res1,cv2.COLOR_BGR2GRAY)

                nzCount = cv2.countNonZero(res1)
                #If player has red jersy
                mask2 = cv2.inRange(player_hsv, lower_red, upper_red)
                res2 = cv2.bitwise_and(player_img, player_img, mask=mask2)
                res2 = cv2.cvtColor(res2,cv2.COLOR_HSV2BGR)
                res2 = cv2.cvtColor(res2,cv2.COLOR_BGR2GRAY)
                nzCountred = cv2.countNonZero(res2)

                if(nzCount >= 20):
                    #Mark blue jersy players as france
                    cv2.putText(image, 'France', (x-2, y-2), font, 0.8, (255,0,0), 2, cv2.LINE_AA)
                    cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),3)
                else:
                    pass
                if(nzCountred>=20):
                    #Mark red jersy players as belgium
                    cv2.putText(image, 'Belgium', (x-2, y-2), font, 0.8, (0,0,255), 2, cv2.LINE_AA)
                    cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),3)
                else:
                    pass
        if((h>=1 and w>=1) and (h<=30 and w<=30)):
            player_img = image[y:y+h,x:x+w]

            player_hsv = cv2.cvtColor(player_img,cv2.COLOR_BGR2HSV)
            #white ball  detection
            mask1 = cv2.inRange(player_hsv, lower_white, upper_white)
            res1 = cv2.bitwise_and(player_img, player_img, mask=mask1)
            res1 = cv2.cvtColor(res1,cv2.COLOR_HSV2BGR)
            res1 = cv2.cvtColor(res1,cv2.COLOR_BGR2GRAY)
            nzCount = cv2.countNonZero(res1)


            if(nzCount >= 3):
                # detect football
                cv2.putText(image, 'football', (x-2, y-2), font, 0.8, (0,255,0), 2, cv2.LINE_AA)
                cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),3)


    cv2.imwrite("./Cropped/frame%d.jpg" % count, res)
    # print('Read a new frame: ', success)    # save frame as JPEG file 
    count += 1
    cv2.imshow('Match Detection',image)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    success,image = vidcap.read()

vidcap.release()
cv2.destroyAllWindows()
opencv tensorflow deep-learning
4个回答
1
投票

您可以使用带有预先训练过的coco模型的神经网络开始虚拟项目:https://github.com/Icy3D/hello-coco-py

Detected objects: person and car

如果您需要更多学习如何减少要检测的类的数量(人,汽车,公共汽车,......)。如果你需要更多的sophicsticated解决方案训练自己的神经网络或适应现有的网络满足您的需求(谷歌转移学习)。

无论如何你应该get familiar with tensorflow


1
投票

“在(新数据集或任务)Y上执行X的最佳算法”的问题对于学习算法来说是完全无效的,因为它们都是针对我们的应用程序调整的,基于我们训练它们的数据并且没有最优性(或者可以保证。

现在的检测算法使用各种各样的机制,从使用深度学习的对象检测,卡尔曼滤波到跟踪和结合不同的上下文和融合。

您可以询问自己跟踪和检测之间的区别。尝试找出在几个帧中检测(完美)对象(同一实例)与使用时间信息跟踪它之间的差异。如果您能够完美地找到所有对象,并对这些实例进行一致编号,那么现在有什么区别?

我可以推荐给你研究它的众多来源,例如this中文文章,facebook的detectron(基于Mask-RCNN)等等。在谷歌尝试各种关键字组合来增强你的结果:“对象检测深度学习”(2017年及以上过滤它),学者中的“对象跟踪”......

祝好运!


1
投票

您可以使用OpenCV进行对象跟踪:

Object Tracking OpenCV

然后训练您自己的OpenCV级联来识别球衣号码:

OpenCV Customized Cascade Training + Facial Landmarks

有关OpenCV安装的更多信息,请参阅:

https://www.pyimagesearch.com/

https://www.learnopencv.com/

这是你可能需要的:

https://www.youtube.com/watch?v=qA1dYQSANO8


1
投票

您可以从最近的这篇论文(https://arxiv.org/pdf/1902.03524.pdf)中查看百度开发的CNN是图像识别问题的最新技术。

© www.soinside.com 2019 - 2024. All rights reserved.