opencv 运动追踪
介绍 (Introduction)
In my previous work, I used pre-trained Yolov3 model to detect and used SORT (simple online and realtime tracking) to track football players from video. Then I used OpenCV’s getPerspectiveTransform function to convert the video to bird’s-eye view.
在我以前的工作中,我使用了预先训练的Yolov3模型来检测并使用SORT(简单的在线和实时跟踪)从视频中跟踪足球运动员。 然后,我使用OpenCVgetPerspectiveTransform函数将视频转换为鸟瞰图。
One of the problems of this work is that the model cannot tell the difference between teams. It will be good if the program is able to identify players’ team instead of just detecting ‘person’. To further improve this, I wish to include a function which tells the difference based on the colors of players’ jersey.
这项工作的问题之一是模型无法区分团队之间的差异。 如果程序能够识别玩家的团队而不仅仅是检测“人”,那将是很好的。 为了进一步改善这一点,我希望包含一个功能,该功能根据球员球衣的颜色来区分差异。
Two approaches I can think of now to this problem.
我现在可以想到两种解决这个问题的方法。
- Train an object detection model using custom dataset containing 3 classes — the players of two teams and referees. This approach might not be practical for real application because one has to train a specific model before every match. 使用包含3个类的自定义数据集训练对象检测模型-两个团队的球员和裁判。 这种方法对于实际应用可能不切实际,因为必须在每次比赛之前训练一种特定的模型。
- Use to current object detection model and extract the color information from the detections. Based on that I can identify the color of players’ jersey. 用于当前物体检测模型并从检测中提取颜色信息。 基于此,我可以识别球员球衣的颜色。
I decided to try approach 2 using OpenCV.
我决定尝试使用OpenCV方法2。
足球视频 (Football video)
The stationary football video is downloaded from here.
固定式足球视频可从此处下载。
“T. D’Orazio, M.Leo, N. Mosca, P.Spagnolo, P.L.Mazzeo A Semi-Automatic System for Ground Truth Generation of Soccer Video Sequences, 6th IEEE International Conference on Advanced Video and Signal Surveillance, Genoa, Italy September 2–4 2009”
“T。 D'Orazio,M.Leo,N.Mosca,P.Spagnolo,PLMazzeo,用于足球视频序列地面真相生成的半自动系统,第六届IEEE国际高级视频和信号监视国际会议,意大利热那亚,2009年9月2日至4日”
彩色面膜由OpenCV (Color Mask By OpenCV)
The major difference among teams and referee is jersey color — one team is white, the other is blue and the referee is red. I want to determine the player’s team based on the jersey color.
球队和裁判之间的主要区别是球衣颜色-一个队是白色,另一队是蓝色,而裁判是红色。 我想根据球衣颜色确定球员的球队。
One frame from the video. 视频一帧。First I identified the range for the three colors.
首先,我确定了三种颜色的范围。
color_list=['red','blue','white']boundaries = [
([17, 15, 75], [50, 56, 200]), #red
([43, 31, 4], [250, 88, 50]), #blue
([187,169,112],[255,255,255]) #white
]
Then I applied OpenCV’s inRange function to create masks for three colors.
然后,我使用OpenCVinRange函数为三种颜色创建蒙版。
mask = cv2.inRange(image, lower, upper)
Then use bitwise_and to apply masks to the frame.
然后使用bitwise_and将遮罩应用于帧。
output = cv2.bitwise_and(image, image, mask = mask)
Three colors are extracted now
现在提取三种颜色
Red, blue and white masks applied to the image. 红色,蓝色和白色蒙版应用于图像。Now we need to apply these mask to the detection. I cropped the image based on the bounding box detected then applied the masks.
现在我们需要将这些蒙版应用于检测。 我根据检测到的边界框裁剪图像,然后应用蒙版。
crop_img = frame[ymin:ymax, xmin:xmax]
Masks applied on the detection. The jersey is identified as blue based on the non-black/total pixel ratio. 口罩应用于检测。 根据非黑色/总像素比率,将球衣标识为蓝色。
The way I identify the color is to count the non-black pixels and calculated the ratio (non-black/total pixel) for the output images of the three colors. The highest ratio is the color of the jersey. For the example above, the blue has highest ratio, so the jersey is identified as blue.
我识别颜色的方法是对非黑色像素进行计数,并计算三种颜色的输出图像的比率(非黑色/总像素)。 比例最高的是球衣的颜色。 对于上面的示例,蓝色的比率最高,因此球衣被识别为蓝色。
Now I integrated this function to the player detection script, here are some examples of the masks and player detections.
现在,我将此功能集成到播放器检测脚本中,以下是一些遮罩和播放器检测的示例。
Samples of detections 检测样本Based on the results of jersey color, I can draw the bounding boxes with different colors.
根据球衣颜色的结果,我可以绘制不同颜色的边界框。
Football players tracking. Color of the bounding box represents the color of jersey. 足球运动员跟踪。 边界框的颜色代表球衣的颜色。Then similar to my previous post, by using Opencv’s getPerspectiveTransform, I obtained the bird’s-eye view as shown in the beginning.
然后类似于我以前的文章,通过使用Opencv的getPerspectiveTransform ,我获得了如开始所示的鸟瞰图。
结论 (Conclusion)
There are still some problems:
仍然存在一些问题:
- Some players were not detected at some frames, and also some non-player people were detected. It might be improved by training an object detection model specifically for football players instead of using the pre-trained model. It would be interesting to also track the ball, even through the ball is really small, it is possible to try it or other object detection architecture. 在某些帧处未检测到某些玩家,还检测到一些非玩家人员。 通过训练专门针对足球运动员的对象检测模型而不是使用预先训练的模型,可以改进它。 跟踪球也很有趣,即使通过的球真的很小,也可以尝试使用它或其他物体检测体系结构。
- ID changed when there are player occlusion. Might be helpful to try other tracking algorithm like DeepSort. 发生玩家遮挡时,ID发生了变化。 尝试其他跟踪算法(例如DeepSort)可能会有所帮助。
Some applications for this kind of tracking:
这种跟踪的一些应用程序:
- Track the players and calculate for their speed and distance. 跟踪玩家并计算他们的速度和距离。
- Get the heatmap for the players’ path for both teams 获取双方球队球员路线的热图
- and more 和更多
Thanks for reading, suggestions and feedback are welcome.
感谢您的阅读,欢迎提出建议和反馈。
翻译自: https://towardsdatascience.com/football-players-tracking-identifying-players-team-based-on-their-jersey-colors-using-opencv-7eed1b8a1095
opencv 运动追踪