I wanted to keep this short but it’s detailed enough you should have my workflow down and this workflow will suit Windows/Linux users
Setting up Darknet
Use the popular AlexeyAB fork of Darknet, it’s fairly easy to set up on Windows and Linux
If you have an Nvidia RTX 30 series card right now you may have some issues with installing OpenCV with CUDA 11 support although I assume that may be fixed now - DM me if you have issues I have a workaround
https://github.com/artynet/darknet-alexeyAB#how-to-compile-on-windows-using-vcpkg
Using Darknet to train image detection
There are 100’s of tutorials on this online and I was a total noob doing it with no previous experience, it’s just a case of labelling things you want Darknet to detect using Yolo in images and running training. It was simpler than I thought
As I am using Windows, I use Windows to run Darknet for training, and use WSL2 for running Linux to run Darkmark for labeling images and creating the config .cfg files for Darknet which saves me HOURS!
I used this WSL2 hack to see Linux GUI in windows so I could have windows and linux running simultaneously
https://gist.github.com/tdcosta100/385636cbae39fc8cd0937139e87b1c74
Darkmark is super useful for labeling images and generating the .cfg config file for Darknet. You can use an early trained object detection model to actually help you label images faster when your model can recognise things in images that you are labeling (so you can scale up and train 1000’s of images faster and improve accuracy)
https://www.ccoderun.ca/darkmark/Summary.html#DarkMarkDarknet
The creator of Darkmark made a Discord group who are very helpful too, feel free to join I’m very active there too for questions
https://github.com/AlexeyAB/darknet/issues/6455
I liked this tutorial personally, it showed me the whole process
https://www.youtube.com/watch?v=zJDUhGL26iU
Converting trained Darknet weights to ONNX
Converting Yolo v4 / v4 tiny and Yolo v3 / v3 Tiny to ONNX works from here https://github.com/jkjung-avt/tensorrt_demos#demo-5-yolov4
-
Make sure you have your trained .cfg and .weights files from darknet
-
Follow the naming conventions here from the link below, keep them identical for the .cfg and .weights files, take note to try and use this naming convention for future models you want to convert. The naming conventions are very strict but they make sense and will help you identify what your files are
e.g. “yolov3-tiny-multipersons-384x288.cfg” and “yolov3-tiny-multipersons-384x288.weights” where ‘multipersons’ is just whatever you want to label the file as, for my scenario I wrote ‘test’ instead of ‘multipersons’
More info on the naming needed:
https://jkjung-avt.github.io/trt-yolov3-custom/
-
Clone this repo and install dependencies for it, in our case Demo #4 and Demo #5: requires TensorRT 6.x+, you may need some things like make, I tested this all on Linux and sussed it out despite being a total ML/Linux noob
https://github.com/jkjung-avt/tensorrt_demos
-
Follow setup instructions here up until step 4, you have the converted .onnx file when you have ran the python3 yolo_to_onnx.py -m yolov4-416
** if you are using a custom model with custom class number, use --category_num 2 at the end of the function, e.g. for 2 classes, use:
python3 yolo_to_onnx.py -m yolov3-tiny-darkmarktest-416 --category_num 2
Feel free to continue the steps if you want to test the onnx graph in TF, or open it in Netron graph viewer web version to compare the structure to your original .cfg
https://github.com/jkjung-avt/tensorrt_demos#demo-5-yolov4
(it’s all set to run with Python 3 and I tested on Ubuntu 20.04 LTS)
More reading from the repo creator here, he talks about how the scripts all work:
https://jkjung-avt.github.io/tensorrt-yolov3/
Finally… using it
Assuming you are using the repo here https://github.com/derenlei/Unity_Detection2AR
Bring in your .onnx file and make sure to import your names list file as a .txt file for unity to read
-
Upload the model and label to Assets/Models. Use inspector to update your model settings in Scene: Detect → Game Object: Detector Yolo2-tiny / Detector Yolo3-tiny. Update anchor info in the DetectorYolo script here or here.
-
On the PhoneARCamera script in the scene, choose Yolo v2 or v3 from the dropdown, it should be on a gameobject like ‘Camera’ or ‘phonecamera’
I hope that is helpful to you, any questions please let me know
I trust in your case you will want NPCs or something in the game to send the camera feed to the Yolo model for object detection? You can see what we are doing with the PhoneARCamera script which currently sends the AR CPU image of what’s in the phone camera directly to Unity Barracuda using the Yolo v3 tiny model.