208 lines
20 KiB
HTML
208 lines
20 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en-us" dir="ltr">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width">
|
|
<title>WGU Capstone Setup Instructions | Nickiel's Static Pages</title>
|
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
|
|
<script type="text/javascript" src="/navbar.js"></script>
|
|
<link rel="stylesheet" href="/catppuccin.css">
|
|
<link rel="stylesheet" href="/light_mode.css">
|
|
<link rel="stylesheet" href="/dark_mode.css">
|
|
|
|
<link rel="stylesheet" href="/css/main.min.13352547759ac6940d329309b455c1cc5e8b56036fb2faddc77c09b4ee1d3b31.css" integrity="sha256-EzUlR3WaxpQNMpMJtFXBzF6LVgNvsvrdx3wJtO4dOzE=" crossorigin="anonymous">
|
|
|
|
|
|
<script src="/js/main.a7cf2e93b3de95a03979d12ae93a1f43be4422a446bff4f7ccf71c285ccc2f95.js" integrity="sha256-p88uk7PelaA5edEq6TofQ75EIqRGv/T3zPccKFzML5U=" crossorigin="anonymous"></script>
|
|
|
|
|
|
</head>
|
|
<body>
|
|
<header>
|
|
|
|
<nav>
|
|
<div class="nav-list">
|
|
<a href="/">Nick's Static Pages</a>
|
|
<a href="/">Home</a>
|
|
<a aria-current="true" class="ancestor" href="/posts/">Posts</a>
|
|
<a href="/tags/">Tags</a>
|
|
<div class="nav-dropdown">
|
|
Themes
|
|
<div class="nav-dropdown-content">
|
|
<a href="#" onclick="light_mode()">Light Mode</a>
|
|
<a href="#" onclick="sunset_mode()">Sunset Mode</a>
|
|
<a href="#" onclick="dark_mode()">Dark Mode</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
</nav>
|
|
|
|
|
|
</header>
|
|
<main>
|
|
|
|
<h1 id="step-0-clone-the-repository">Step 0: Clone the repository</h1>
|
|
<p>Before you can run this project, you will need to clone the git repository with the following command:</p>
|
|
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>git clone https://git.nickiel.net/Nickiel/WGU-Capstone
|
|
</span></span></code></pre></div><p>See <a href="#project-structure">Project Structure</a> for more information on the repository you just cloned.</p>
|
|
<p>See <a href="#step-1---prerequisites">Step 1 - Prerequisites</a> on what is required before you can run this project.</p>
|
|
<h1 id="project-structure">Project Structure</h1>
|
|
<p>Below you can find the default project folder structure after cloning it:</p>
|
|
<pre tabindex="0"><code>WGU-Capstone
|
|
├.gitignore
|
|
├Main.py
|
|
├README.md
|
|
├WGU-Capstone-User-Guide.html
|
|
├requirements.txt
|
|
├shell.nix
|
|
├cascades
|
|
│ ├ cascade_1.xml
|
|
│ ├ cascade_2.xml
|
|
│ ├ cascade_5.xml
|
|
│ └ cascade_10.xml
|
|
├training_data
|
|
│ ├ positives
|
|
│ └ training_data_setup.py
|
|
└validation
|
|
├ TestVideo.mp4
|
|
├ compare_to_gt.py
|
|
├ create_ground_truth.py
|
|
└ ground_truth.txt
|
|
</code></pre><p><a href="#step-1---prerequisites">Click here to skip the detailed file structure explaination</a></p>
|
|
<h2 id="top-level">Top Level</h2>
|
|
<p>In the top-level of the cloned repository, you will find most of the files required for the core-fuctionality.</p>
|
|
<h4 id="gitignore">.gitignore</h4>
|
|
<p>This file excludes files we don’t want to check into git - such as the training data.
|
|
These files continue to exist on machine, but they are not uploaded to the remote git repository. This is very helpful to keep the clone sizes down, and upload/download speeds up.</p>
|
|
<h4 id="mainpy">Main.py</h4>
|
|
<p>The main file of interest for this project. This file contains all of the code for the finished product.
|
|
As long as this file is in the same folder as the <code>./cascades</code> folder, it can be copied and run anywhere with the prerequisites installed.</p>
|
|
<h4 id="readmemd">README.md</h4>
|
|
<p>The file you are reading in a format that most git hosting servers automatically render as the home-page.</p>
|
|
<h4 id="wgu-capstone-user-guidehtml">WGU-Capstone-User-Guide.html</h4>
|
|
<p>The html version of the README.md file that was bundled with CSS and hyper-links.</p>
|
|
<h4 id="requirementstxt">requirements.txt</h4>
|
|
<p>The file that contains all of the python pip requiremnts to run. This packages in this file can either be installed by hand (e.g. <code>pip install opencv-python</code>), or can all be installed at once with <code>pip install -r requirements.txt</code> which will install all of the modules needed to run this project that are not included in the standard library.</p>
|
|
<h4 id="shellnix">shell.nix</h4>
|
|
<p>A file that can be used on <a href="https://nixos.org/">Nix and NixOS</a> systems to create a reproducable environement with all of the requirements to run the <code>Main.py</code> file.</p>
|
|
<h2 id="cascades">./Cascades</h2>
|
|
<p>This folder contains the final trained models created by this project in the model training step.
|
|
For more information on how they were created, see <a href="#training-your-own-haar-file">Training your own Haar file</a> below.</p>
|
|
<p>This folder needs to be in the same directory as the <code>Main.py</code> file for the <code>Main.py</code> file to be able to run.</p>
|
|
<h2 id="traning_data">./Traning_data</h2>
|
|
<p>This folder contains all of the requirements for creating a new model from a few un-catagorized positive images, and a large dataset of negatives.</p>
|
|
<p>NOTE: Before anything in this folder can be run, please see <a href="#training-your-own-haar-file">the section on training the haar files</a> for several prerequisites.</p>
|
|
<h4 id="training_data_setuppy">./Training_data_setup.py</h4>
|
|
<p>This python file takes a large data-set of negative images from the <code>./training_data/negatives</code> folder and creates .vec files that can be passed as an arguement to the utility that trains the final Haar file.</p>
|
|
<h4 id="positives">./Positives</h4>
|
|
<p>This folder contains the 10 images that were used to create the cascade files included in this project. These files were included because the 10 images are a very small dataset in comparison to the required negatives.</p>
|
|
<h2 id="validation">./Validation</h2>
|
|
<p>The folder contains all of the scripts and files used to measure the performance and accuracy of the generated models.</p>
|
|
<h4 id="testvideomp4">TestVideo.mp4</h4>
|
|
<p>This minute-long video was used to test the trained models.</p>
|
|
<h4 id="compare_to_gtpy">Compare_to_gt.py</h4>
|
|
<p>This file compares the output of a <code>--validate</code> output file generated by <code>Main.py</code> of a run with the provided <code>ground_truth.txt</code> file.
|
|
The output of this file is a .csv file that describes the average deviation from the boxes described by the <code>ground_truth.txt</code> file. See <a href="#validation-and-testing">Validation and Testing</a> for more information on this process.</p>
|
|
<h4 id="create_ground_truthpy">Create_ground_truth.py</h4>
|
|
<p>This is the file used to create the <code>ground_truth.txt</code> file from the provided <code>TestVideo.mp4</code>.</p>
|
|
<h1 id="step-1---prerequisites">Step 1 - Prerequisites</h1>
|
|
<p>Before you can run this project, you need a python environment with the required packages installed.</p>
|
|
<p>If you are using Nix or NixOS, simply run <code>nix shell</code> in the <code>WGU-Capstone</code> folder, and all of the packages required to run <code>Main.py</code> will be installed for that shell session.</p>
|
|
<p>However, if you are not on a Nix system, continue reading.</p>
|
|
<p>The steps below detail how to set up a virtual environment that can be used to run this project, but a system-wide install of python with the packages detailed in <code>requirements.txt</code> installed will also suffice.</p>
|
|
<h3 id="set-up-virtual-environment">Set up virtual environment</h3>
|
|
<p>This project was created with python 3.11, and other versions are not garunteed to work. So to ensure the project works as designed, install python 3.11 from the official python download page.</p>
|
|
<p>Once you have python 3.11 installed on your system, navigate to the cloned repository’s root directory, and run the following command to create a new virtual environement:</p>
|
|
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>python <span style="color:#f92672">-</span>m venv <span style="color:#f92672">./</span>venv
|
|
</span></span></code></pre></div><p>You can now run the following commands to enter the virtual environment, and any python commands will be run inside the virtual environment instead of your system-wide installation.</p>
|
|
<p>On windows run the following if you are using a cmd prompt:</p>
|
|
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>.<span style="color:#ae81ff">\v</span>env<span style="color:#ae81ff">\S</span>cripts<span style="color:#ae81ff">\a</span>ctivate.bat
|
|
</span></span></code></pre></div><p>On windows in powershell:</p>
|
|
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>.<span style="color:#ae81ff">\v</span>env<span style="color:#ae81ff">\S</span>cripts<span style="color:#ae81ff">\A</span>ctivate.ps1
|
|
</span></span></code></pre></div><p>If you are on a linux based operating system, enter the virtual environment with:</p>
|
|
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>.<span style="color:#ae81ff">\v</span>env<span style="color:#ae81ff">\S</span>cripts<span style="color:#ae81ff">\a</span>ctivate
|
|
</span></span></code></pre></div><h3 id="install-requirements">Install requirements</h3>
|
|
<p>Now that you have activated the virtual environment, install the non-standard library requirements with the below command:</p>
|
|
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>pip install -r ./requirements.txt
|
|
</span></span></code></pre></div><h1 id="step-2---running-the-project">Step 2 - Running the project</h1>
|
|
<p>Now that the pre-requisites have been installed, you can run the project. For a full list of command-line arguements, run <code>python Main.py --help</code>.</p>
|
|
<p>Run the project with the dashboard enabled with the following command from the root of the project directory:</p>
|
|
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>python Main.py -d
|
|
</span></span></code></pre></div><p>You should see the web-cam of your computer turn on, and a window appear showing the view of the webcam, with boxes around any detected faces.</p>
|
|
<p>To display the calculated adjustment amounts generated by this project, enable the print-to-stoud feature with the <code>-o</code> flag:</p>
|
|
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>python Main.py -d -o
|
|
</span></span></code></pre></div><p>This command will output the calculated output commands for every detected face, and also show the summary statistics.</p>
|
|
<h1 id="additional-flags">Additional flags</h1>
|
|
<p>This section will describe, in greater depth, the available feature flags shown by the <code>--help</code> screen.</p>
|
|
<h2 id="help">Help</h2>
|
|
<p><code>-h</code> or <code>--help</code></p>
|
|
<p>Displays all of the available parameters and a quick description</p>
|
|
<h2 id="version">Version</h2>
|
|
<p><code>-v</code> or <code>--version</code></p>
|
|
<p>Prints the version of the program and exits</p>
|
|
<h2 id="show-dashboard">Show Dashboard</h2>
|
|
<p><code>-d</code> or <code>--dasbboard</code></p>
|
|
<p>Display the run-summary statistics, these are off by default.</p>
|
|
<h2 id="output-adjustment-instructions">Output Adjustment Instructions</h2>
|
|
<p><code>-o</code> or <code>--output</code></p>
|
|
<p>Print the calculated adjustment instructions generated by the program. This output demonstrates the generated values that will be sent to the motorized camera platform.</p>
|
|
<h2 id="use-video-file">Use Video File</h2>
|
|
<p><code>-f <file_path></code> or <code>--file <file_path></code></p>
|
|
<p>Use a video file (such as ./validation/TestVideo.mp4) instead of the computer’s webcam. Useful for generating validation files and on machines without a working webcam.</p>
|
|
<h2 id="headless-mode">Headless Mode</h2>
|
|
<p><code>-s</code> or <code>--no-screen</code></p>
|
|
<p>Run the program without the window displaying processed video frames.</p>
|
|
<h2 id="save-frames-for-training-data">Save Frames for Training Data</h2>
|
|
<p><code>-t</code> or <code>--training-data</code></p>
|
|
<p>Save frames where faces were found to <code>./output</code> as .jpg files, and save the located face’s location to a .csv file.
|
|
This feature will be used to generate positive images automatically for training future models.</p>
|
|
<h2 id="generate-validation-file">Generate Validation File</h2>
|
|
<p><code>--validate</code></p>
|
|
<p>Outputs all discovered boxes, the frame they were found on, and the box coordinates so the model can be validated against the ground truth. See <a href="#validation-and-testing">validation and testing</a> for more information on this process.</p>
|
|
<h1 id="training-your-own-haar-file">Training Your Own Haar File</h1>
|
|
<p>This project contains the scripts required to train your own Haar cascade files, but it does not contain several of the dependencies.</p>
|
|
<p>NOTE: These steps only apply to Windows devices.</p>
|
|
<h2 id="prerequisites">Prerequisites</h2>
|
|
<p>The first requirement needed before you can train your own Haar file, is a large number of negative images. For this project, I used <a href="https://www.kaggle.com/datasets/arnaud58/landscape-pictures/">this Kaggle dataset of landscape images</a> as my negatives datasource. After downloading this file, unzip it and deposit all of the raw images into the <code>./training_data/negatives</code> folder - create it if needed.</p>
|
|
<p>Next we need to download the windows OpenCV binary distributable and put it in our training_data folder.</p>
|
|
<p>You can download the 3.4.15 binary executable <a href="https://sourceforge.net/projects/opencvlibrary/files/3.4.15/opencv-3.4.15-vc14_vc15.exe/download">here</a>. (You can also go <a href="https://opencv.org/releases/">here</a> and find the 3.4.15 release and choose “windows” to get to the same page).</p>
|
|
<p>After the .exe file has downloaded, open it and go through the steps to unzip it. After it has been unzipped, copy the folder to <code>./training_data/opencv</code>. So you should be able to run this from the training_data directory:</p>
|
|
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>.<span style="color:#ae81ff">\o</span>pencv<span style="color:#ae81ff">\b</span>uild<span style="color:#ae81ff">\x</span>64<span style="color:#ae81ff">\v</span>c15<span style="color:#ae81ff">\b</span>in<span style="color:#ae81ff">\o</span>pencv_createsamples.exe
|
|
</span></span></code></pre></div><p>If you do not get an error running the above command, then it was installed correctly.</p>
|
|
<h2 id="generating-positive-images">Generating positive images</h2>
|
|
<p>Now that we have the create_samples utility provided by OpenCV (they stopped distributing executables of it after 3.4.15) and the negatives folder full of negative images, we can use the <code>training_data_setup.py</code> file to create several different sized datasets ready for training Haar cascade files on.</p>
|
|
<p>The python file will run the create_samples tool for every positive image in <code>./positives</code>, creating many positive images.
|
|
The script will do all of the steps up through creating the .vec files that the train_cascade exectuable requires.</p>
|
|
<p>Before exiting, training_data_setup outputs the commands that need to be run to train the models. Run these commands from the training_data folder, and after they have finished training, you can use the generated Haar cascades instead of the ones provided.</p>
|
|
<h1 id="validation-and-testing">Validation and Testing</h1>
|
|
<p>The following describes the process I used to test the precision and accuracy of the generated cascade files.</p>
|
|
<h2 id="generate-the-ground-truth-file">Generate the Ground Truth file</h2>
|
|
<p>I have included a generated <code>ground_truth.txt</code> file, so you don’t need to do this step. But if you would like to generate the ground truth file from the provided test video, navigate to root of the project, and run the create ground truth script:</p>
|
|
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>python create_ground_truth.py
|
|
</span></span></code></pre></div><p>A window will open and display the process as it creates the file. This script does not utilize Haar files, but the MIL tracking algorithm, which results in much more accurate results, but a slower processing speed for the video.</p>
|
|
<p>All of these settings have been hard-coded so it will always output the same ground truth file.</p>
|
|
<h2 id="getting-the-model-validation-file">Getting the model validation file</h2>
|
|
<p>Now that we have the ground truth for our Test Video, we need to generate the same file with our trained model.</p>
|
|
<p>To do this, edit the <code>Main.py</code> file so that it uses the new cascade, then run the python file with the <code>--validate</code> option set, and the test video passed to the <code>-f</code> flag. The command used to generate the statistics with the test video provided is this:</p>
|
|
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>python ./Main.py -d -f ./validation/TestVideo.mp4 --validate
|
|
</span></span></code></pre></div><p>(Notice that we can still display the dashboard while it outputs validation info)</p>
|
|
<p>This will create a new file in the <code>./validation</code> folder describing the faces and locations found in each frame.</p>
|
|
<h2 id="comparing-it-to-the-ground-truth">Comparing it to the ground truth</h2>
|
|
<p>I have created a script to automatically compare a validation file with a ground truth file, and output the average absolute deviation in adjustment instructions.
|
|
It requires two arguements, and has one optional output. You can see the options with the <code>--help</code> flag, but I will demonstrate all of the options below.</p>
|
|
<p>You can use <code>./validation/compare_to_gt.py</code> like this:</p>
|
|
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>cd ./validation
|
|
</span></span><span style="display:flex;"><span>python compare_to_gt ./ground_truth.txt ./20231012-081235.txt ./output.csv --faces_count_file ./faces_count_output.csv
|
|
</span></span></code></pre></div><p>This script will then take the generated test validation file, and get what the generated adjustment output would be, and gets the absolute difference between it and the ground truth, then it adds together all results for each frame - this last part penalizes false positives. We can then take the generated output file, and open it in Excel. We can take the average of it to see what the average deviation from the ground truth would be. The generated faces_count_output file contains the number of faces found in each frame, and can be used to measure the number of false positives.</p>
|
|
|
|
|
|
|
|
|
|
</main>
|
|
<footer>
|
|
<p>Copyright 2023. All rights reserved.</p>
|
|
|
|
</footer>
|
|
</body>
|
|
</html>
|