Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track players #23

Merged
merged 3 commits into from
Apr 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions CVIFinalProject.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "HumanDetectTest", "HumanDet
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TrackPlayers", "TrackPlayers\TrackPlayers.vcxproj", "{CF51F5D3-A9FC-4FE4-BECC-C5B75034DE31}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FinalEvaluation", "FinalEvaluation\FinalEvaluation.vcxproj", "{C2845CB2-85F5-4158-8FA1-2F980D0D98FC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -41,6 +43,12 @@ Global
{CF51F5D3-A9FC-4FE4-BECC-C5B75034DE31}.Release|Any CPU.ActiveCfg = Release|Win32
{CF51F5D3-A9FC-4FE4-BECC-C5B75034DE31}.Release|Win32.ActiveCfg = Release|Win32
{CF51F5D3-A9FC-4FE4-BECC-C5B75034DE31}.Release|Win32.Build.0 = Release|Win32
{C2845CB2-85F5-4158-8FA1-2F980D0D98FC}.Debug|Any CPU.ActiveCfg = Debug|Win32
{C2845CB2-85F5-4158-8FA1-2F980D0D98FC}.Debug|Win32.ActiveCfg = Debug|Win32
{C2845CB2-85F5-4158-8FA1-2F980D0D98FC}.Debug|Win32.Build.0 = Debug|Win32
{C2845CB2-85F5-4158-8FA1-2F980D0D98FC}.Release|Any CPU.ActiveCfg = Release|Win32
{C2845CB2-85F5-4158-8FA1-2F980D0D98FC}.Release|Win32.ActiveCfg = Release|Win32
{C2845CB2-85F5-4158-8FA1-2F980D0D98FC}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
7 changes: 0 additions & 7 deletions Contributing.md

This file was deleted.

Binary file removed DetectLines/01.jpg
Binary file not shown.
Binary file removed DetectLines/02.jpg
Binary file not shown.
4 changes: 4 additions & 0 deletions DetectLines/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This was an approach to automatically detect all the lines on a rink.
This method works only with a static camera since we need to know all the angles the lines are located at.

This program accepts a video as an argument, detects lines and draws them on top of each frame.
3 changes: 0 additions & 3 deletions DetectLines/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ int main(int argc, char** argv) {
cv::Mat videoFrame;
for (;;) {

if (frameNumber % 5) {
cout << "Frame number is " << frameNumber << endl;
}
cap >> videoFrame;
videoFrame += cv::Scalar(brighter, brighter, brighter); //make the copy brighter
cv::Mat frame; //scaled and processed frame
Expand Down
Empty file removed DetectLines/short.mp4-out.avi
Empty file.
63 changes: 33 additions & 30 deletions Documentation.txt
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
HOCKEY PLAYER DETECTION - DOCUMENTATION AND INSTRUCTIONS
---------------------------------------------------

ImageAnnotator/
---------------
Contains a cpp program to annotate rectangles onto images to build our grund truth dataset.

CSVReader.h: reads in data (filenames) from a csv file for use in the program

Source.cpp: Main file, must pass csv file as argument, this program iterates over images in a passed in image set,
opening each image in a window. The user can then use the mouse to create rectangles over players in
the image to define the ground truth of the image.

Object-Detector/
----------------
Contains a python programs to manually annotate images, create a dataset of the annotated images, and train a machine learning model
to recognize the objects being annotated in these images.

Instructions to to run the annotator are included in README.md

TrackPlayers/
-------------
Contains a cpp program to detect hockey players in a hockey arena

To run the program over an image simply startup the project in Visual Studio 2012, make sure the image to be used is in the
same folder as main.cpp, set debugging arguments to the image filename, and run the program.
To run the program over an image compile the program and run passing a path to an image as an argument. File will detect players, preparing an image before detection and will save a result as filename-out.jpg

Tools.hpp: Contains some simple functions which are used in the main program
drawCircle(): Draws a circle formatted to be placed near the bottom of a drawn rectangle on the image
Expand All @@ -38,9 +21,18 @@ Tools.hpp: Contains some simple functions which are used in the main program

addContrast(): Adds contrast to an image

applyMask(): Applies mask to an image

HogDetector.cpp: Where the player detection takes place
applyMask(): Accepts 3 images, a mask, an image to apply that mask over and the result image pointer.

Detector.hpp:
The main class that has methods to prepare images for the processing.
Has a method MakeMask, that automatically detects the rink area to mask the image and remove the crowd are.
Apply morph opening to remove small details, Blur the image, Dilate to remove some big chunks, Erode to return to get back lost area of the rink, Use bitwise flip to get a mask. Comments are in Detector.hpp.

Method PrepareForProcessing makes players bodies well defined on rink by taking a difference between the original and blurred+dilated image. (pass option 2 to the method)
Detect - detects player on image without pre-processing the image (detect on the image you pass, you can prepare your own image as you like)
DetectWithPreprocessing - detects player on image preparing the image first (mask + contours)

HogDetector.cpp: Wrapper class to easily use HOG detector
hog: instance of a HogDetector with the specified parameters

detect(): runs the hog detector and returns a vector of found bounding rectangles
Expand All @@ -55,14 +47,25 @@ Main.cpp: Main program to run player detection, MUST PASS IN IMAGE FILENAME
DrawDetected(): draws detected rectangles on the source image

save(): saves passed in image to current folder

PrepareForProcessing(): Prepares the current image to be processed by adding contrast, converting color, etc

MakeMaskFromEges(): Creates a mask around the ice surface to be applies later

MakeMask(): Add contrast, Convert to grayscale, Threshhold gray image, Use big kernel size to remove players and noise from the rink
Apply morph opening to remove small details, Blur the image, Dilate to remove some big chunks
Erode to return to get back lost area of the rink, Use bitwise flip to get a mask


MarkDetectedPlayer(): Draws bounding rectangles and circles on the source image




ImageAnnotator/
---------------
Contains a cpp program to annotate rectangles onto images to build our grund truth dataset.

CSVReader.h: reads in data (filenames) from a csv file for use in the program

Source.cpp: Main file, must pass csv file as argument, this program iterates over images in a passed in image set,
opening each image in a window. The user can then use the mouse to put dots on players in
the image to define the ground truth of the image.

Object-Detector/
----------------
Contains a python programs to manually annotate images, create a dataset of the annotated images, and train a machine learning model
to recognize the objects being annotated in these images.

Instructions to run the annotator are included in INSTRUCTIONS.md file inside Object-Detector directory
18 changes: 18 additions & 0 deletions FinalEvaluation/DetectPlayers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <opencv2/opencv.hpp>

#include <iostream>
#include <fstream>
#include "../Shared/CSVReader.h"
#include "../TrackPlayers/HogDetector.hpp"
#include "../TrackPlayers/Detector.hpp"
#include "../TrackPlayers/tools.hpp"

#pragma comment(lib, "vfw32.lib")
#pragma comment(lib, "comctl32.lib" )



using namespace cv;
using namespace std;


80 changes: 80 additions & 0 deletions FinalEvaluation/DetectPlayers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#pragma once
#include <opencv2/opencv.hpp>

#include <iostream>
#include <fstream>
#include "../Shared/CSVReader.h"
#include "../TrackPlayers/HogDetector.hpp"
#include "../TrackPlayers/Detector.hpp"
#include "../TrackPlayers/tools.hpp"

void DrawDetected(Mat& frame, std::vector<Rect>& detected, Scalar color = Scalar(0, 0, 255), int thinkness = 1) {
if (detected.size() > 0) {
for (int gg = 0; gg < detected.size(); gg++) {
rectangle(frame, detected[gg].tl(), detected[gg].br(), color, thinkness, 8);
}
}
}


/*
THIS PIECE OF CODE DETECTS PLAYERS ON ALL IMAGES INSIDE TEST FOLDER
SAVES DETECTED RECTANGLES TO A FILE READY TO BE USED BY EVALUATION CODE
*/

bool DetectOnAllImages(string fileWithImagePaths, string resultsFileName) {

CSVReader fileNamesReader(fileWithImagePaths);
fileNamesReader.readLines();
vector<string> fileNames = fileNamesReader.getLines();

if (fileNames.size() == 0) {
cerr << "File with images cannot be read." << endl;
return false;
}

std::ofstream results;
results.open(resultsFileName);
results << "FileName, x_y_width_height(weight)" << endl;

Detector d;
int totalImages = fileNames.size();
int imgCount = 0;
double progress = 0;
for (string imageName : fileNames) {
imgCount++;

Mat img = cv::imread(imageName);
if (img.empty()) {
cerr << "could not open image named '" << imageName << ",\n";
continue;
}
else {
//DETECT PLAYERS FOR THIS IMAGE
d.DetectWithPreprocessing(img);

//write detected rectangles for this file.
//output file example
// imageName.jpg, x_y_width_height, x_y_width_height, x_y_width_height,
results << imageName << ",";
for (size_t idx = 0; idx < d.detected.size(); idx++)
results << d.detected[idx].x << "_" << d.detected[idx].y << "_" << d.detected[idx].width << "_" << d.detected[idx].height << ",";

cv::Mat out = img.clone();
DrawDetected(out, d.detected);

std::size_t dotIdx = imageName.find_last_of(".");
string outFileNameWithoutExt = imageName.substr(0, dotIdx);
size_t slashIdx = imageName.find_last_of("/");
string outFileName = outFileNameWithoutExt.substr(slashIdx + 1);
string outFileExt = imageName.substr(dotIdx);
outFileName += "-out" + outFileExt;
imwrite(outFileName, out);
results << endl;
}
progress = (double)imgCount / (double)totalImages;
cout << "Progress: " << progress * 100 << "% (" << imgCount << "/" << totalImages << ")" << endl;
}
results.close();
return true;
}
92 changes: 92 additions & 0 deletions FinalEvaluation/FinalEvaluation.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{C2845CB2-85F5-4158-8FA1-2F980D0D98FC}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>FinalEvaluation</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="PropertySheet_Debug.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="PropertySheet_Release.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\Shared\CSVReader.cpp" />
<ClCompile Include="main.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\Shared\CSVReader.h" />
<ClInclude Include="DetectPlayers.h" />
<ClInclude Include="PrecisionCalc.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
36 changes: 36 additions & 0 deletions FinalEvaluation/FinalEvaluation.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Shared\CSVReader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\Shared\CSVReader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="DetectPlayers.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="PrecisionCalc.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
7 changes: 7 additions & 0 deletions FinalEvaluation/FinalEvaluation.vcxproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerCommandArguments>test.txt GTPoints.csv</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>