我是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()
答案 0 :(得分:1)
“在(新数据集或任务)Y上执行X的最佳算法”的问题对于学习算法而言是完全无效的,因为它们都是针对我们的应用程序进行调整的,基于我们训练它们的数据,并且没有最优性。 (或可以)保证。
当今的检测算法使用多种机制,从使用深度学习的对象检测,卡尔曼滤波到不同上下文的跟踪和合并以及融合。
您可以问自己关于跟踪和检测之间的区别。尝试找出在几帧中检测(完全)同一对象的对象与使用时间信息跟踪该对象之间的区别。如果您可以完美地找到所有对象并为实例编号,那么现在有什么区别?
我可以为您提供大量研究资料,例如this中等文章,facebook的detectron(基于Mask-RCNN)等等。尝试在Google中使用关键字的各种组合来增强您的结果:“对象检测深度学习”(在2017年及更高版本中进行过滤),学者中的“对象跟踪” ...
祝你好运!
答案 1 :(得分:1)
您可以使用OpenCV进行对象跟踪:
然后训练您自己的OpenCV级联以识别球衣号码:
OpenCV Customized Cascade Training + Facial Landmarks
有关OpenCV安装的更多信息,请参考:
https://www.pyimagesearch.com/和
这是您需要的:
答案 2 :(得分:1)
您可以使用带有预先训练的coco模型的神经网络从虚拟项目开始: https://github.com/Icy3D/hello-coco-py
如果需要更多信息,请学习如何减少要检测的类别(人,汽车,公共汽车,..)的数量。如果您需要更多复杂的解决方案,请训练自己的神经网络或使现有网络适合您的需求(用于转移学习的Google)。
答案 3 :(得分:1)
您可以从最近的论文(https://arxiv.org/pdf/1902.03524.pdf)中确认,百度开发的CNN在图像识别问题上是最先进的。