{"id":24409525,"url":"https://github.com/kdwaterman/tanknottank","last_synced_at":"2025-04-23T12:05:10.320Z","repository":{"id":221051015,"uuid":"752802238","full_name":"kdwaterman/TankNotTank","owner":"kdwaterman","description":"Military object detection on edge devices.","archived":false,"fork":false,"pushed_at":"2024-04-13T08:44:29.000Z","size":26420,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-23T12:04:49.407Z","etag":null,"topics":["autonomous-weapon-system","fpv-drones","object-detection","raspberry-pi","tank","tanks"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kdwaterman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-02-04T20:50:16.000Z","updated_at":"2024-09-15T16:35:36.000Z","dependencies_parsed_at":"2024-04-13T09:46:40.915Z","dependency_job_id":"a271bf0e-e977-4f65-857b-83aa2152cf2f","html_url":"https://github.com/kdwaterman/TankNotTank","commit_stats":null,"previous_names":["kdwaterman/tanknottank"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kdwaterman%2FTankNotTank","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kdwaterman%2FTankNotTank/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kdwaterman%2FTankNotTank/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kdwaterman%2FTankNotTank/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kdwaterman","download_url":"https://codeload.github.com/kdwaterman/TankNotTank/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250430586,"owners_count":21429324,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["autonomous-weapon-system","fpv-drones","object-detection","raspberry-pi","tank","tanks"],"created_at":"2025-01-20T05:58:29.019Z","updated_at":"2025-04-23T12:05:10.286Z","avatar_url":"https://github.com/kdwaterman.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TankNotTank\n\n## Introduction\nWelcome to TankNotTank! This project was the result of a simple curiosity - how easy would it be to train and deploy a tank finding algorithm on an edge computing device? This isn't a normal curiosity, admittedly, but watching FPV drones flying around Ukraine did make me think - how easy would it be to make these autonomously track tanks? This project doesn't quite go in to that detail, but it does provide a minimal proof-of-concept, complete with a trained model and the underlying training data. It should go without saying that this project is **strictly for educational purposes only**. \n\nAs you will see, whilst it demonstrates an ability to accomplish the task in a basic sense, to get this right, reliable, and 'safe', would require a fair bit of extra effort. I've tested it on cars, trucks, and vans, but I'm a little short of tanks in my neighbourhood, however pointing it at images of them works well enough:\n\n![Testing against an image of a T-90](images/tnt.gif \"Testing against an image of a T-90\")\n\n**EDIT - it turns out it works pretty well on real tanks. Altough it got a little confused with a Swedish S tank with its anti-HEAT screen up...**\n\nThis project draws very heavily on [tensorflow examples](https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection/raspberry_pi). However it makes a number of subtle changes that resolve some compatability issues that appear to have arisen since the original tensorflow examples were published. Indeed, if you take a look at the latest comments in this [explainer video](https://www.youtube.com/watch?v=mNjXEybFn98\u0026t=173s), you'll find most of the them from keen, but disappointed, hobbyists struggling to get the project working. Whilst these issues were not technically complex to fix, they were a pain to find, so at minimum I hope this might save someone else some time!\n\n## Requirements\n### Hardware \nTo get started with TankNotTank, you will need the following hardware:\n\n1. Either a Raspberry Pi Zero 2 W or a Raspberry Pi 4 \n2. Raspberry Pi 5MP Camera \n\nOptionally, you may also want to add a touchscreen to make the setup portable. In this case, I'd recommend the `onboard` keyboard as opposed to the `matchbox-keyboard`, as is not optimally configured for the OS we will use. My setup looks like this:\n\n![Hardware setup](images/hardware_setup.jpg \"Hardware setup\")\n\nIt is worth noting that the Pi Zero runs rather a lot slower than the Pi 4, offering an average of ~2 FPS rather than ~5.5 FPS. However, the size differential between the two is considerable, which means the Pi Zero would probably be a better choice for a companion computer on a drone. \n\n### OS \nThis project uses the legacy 32-bit Raspberry Pi OS, Debian Bullseye. The latest OS, Bookworm, appears to have a whole host of compatability issues with the required Python packages, as well as with the main camera application software used by Raspberry Pi OS. Reverting to this legacy, 32-bit OS resolved all of these issues for me. \n\n## Getting Setup\nIf you've worked with the tensorflow examples before, the setup below will be familiar, but it is worth noting the 'libatlas-base-dev' install. This was not in the original instructional video I used to begin this project, so it led me astray for little while whilst I figured it out. In essence, libatlas is a linear algebra library which is integral to the machine learning functionality being deployed, and missing from default Raspberry Pi OS builds.  \n\n\n```\nsudo apt-get update\nsudo apt-get upgrade\nsudo apt-get install libatlas-base-dev\n\npython3 -m pip install --upgrade pip\npython3 -m pip install virtual env\n\n\n\n```\n\nNext we make a virtual environment in which to clone the repo and install the required python packages. \n\n```\npython3 -m venv tnt\nsource tnt/bin/activate\ngit clone https://github.com/kdwaterman/TankNotTank.git\ncd TankNotTank\nsh setup.sh\n\n```\n\nThere is a small chance this might not work, and if it doesn't you'll need to install each dependency separately. In the original tensorflow example I had to make some changes to the requirements in order to get all the dependencies to sync up. For reference, here is the original compared to what is in this project. \n\n| Current      | Original |\n|:----|:----|\n| `argparse`      | `argparse`    |\n| `numpy==1.20.0`    | `numpy\u003e=1.20.0 `      |\n| `opencv-python==4.5.3.56 `    | `opencv-python~=4.5.3.56  `   |\n| `tflite-support==0.4.3 `  | `tflite-support\u003e=0.4.2  `   |\n| `protobuf\u003e=3.18.0,\u003c4 `| `protobuf\u003e=3.18.0,\u003c4 `  |\n\n## Getting The Model Running\nSo, now you have everything downloaded, how do we get the `TankNotTank.tflite` model running? Unsurprisingly for a Pi project, it begins with a further bit of configuration. In this case, we need to enable the legacy camera module. This can be done via `raspi-config` and following the relevant menu options. Now we're ready to actually use the model. Thankfully, this is the simple bit, and can be accomplished by running the following command in the TankNotTank directory:\n\n```\npython3 detect.py --model TankNotTank.tflite\n```\n\nIf everything works, this should start up a new window with our camera output. If pointed at something vaguely related to the classes listed below, a bounding box with class name and probability of match should appear. You'll note I've also added a small extra bit of functionality which marks the center of the detected object and the center of the camera's field-of-view, with a link between them. This doesn't serve a real purpose, but it lays some groundwork for extra functionality I may get around to adding. Similarly, it should also provide others with an easy starting point too. \n\n## Training Your Own Model\nTo train your own tank detecting model, you'll need to use your laptop, rather than the Pi. For the `TankNotTank.tflite` model I used this [Colab Notebook](https://colab.research.google.com/github/EdjeElectronics/TensorFlow-Lite-Object-Detection-on-Android-and-Raspberry-Pi/blob/master/Train_TFLite2_Object_Detction_Model.ipynb) put together by *EdjeElectronics* (if you haven't checked out [their stuff](https://github.com/EdjeElectronics), you should, it's great). Essentially, the notebook takes a copy of the `ssd-mobilenet-v2-fpnlite-320` model and fine tunes it to your uploaded dataset. The training data I used can be found [here](https://data.mendeley.com/datasets/njdjkbxdpn/1), but it required a little manipulation and cleaning before I could use it properly, so my folder structure, and some of the data, differs from the original subtlety. This data may not be perfect, but it did the job, and was very kindly made public by the paper's authors.[^1] \n\nThe data has the following object classes:\n\n| Class              |\n|:---                |\n| civilian aircraft  | \n| civilian car       |\n| military aircraft  | \n| military helicopter| \n| military tank      | \n| military truck     | \n\nAs this is a proof-of-concept, I only trained my model on free GPU time provided by Colab, so there is plenty of room for refinement. The end product is a quantized object detection model that does a pretty good job of classifying objects. However, as with many simple, quantized models, it makes some pretty dramatic mistakes and is easy to spoof. So, enjoy! But don't be surprised when your face is occasionally mistaken for an airliner... \n\n### Configuring Our New Model\nAs you may have guessed, it is now time for some extra configuration. In our case, it transpires that the process above does not produce a model with the metadata required by the specific OpenCV implementation used. However, this is easy to rectify, and all it requires is the latest version of `tflite-support`. Aside from that, it is straightforward:\n\n1. Take a copy of the [metadata](/metadata) folder from this project and place the `TankNotTank.tflite` model within it.\n2. Update `add_metadata.py` to edit in your own model name\n3. Call `python3 add_metadata.py`\n\nThis will add the relevant metadata to the `.tflite` model you've trained, and it should now be ready to transfer to the Pi for use. Just place it in the main working directory and run the following:\n\n```\npython3 detect.py --model \u003cYourModelNameHere\u003e.tflite\n```\n\n\n[^1]: Gupta, Priyanka; Pareek, Bhavya; Singal, Gaurav; Rao, D Vijay (2021), “Military and Civilian Vehicles Classification”, Mendeley Data, V1, doi: 10.17632/njdjkbxdpn.1\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkdwaterman%2Ftanknottank","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkdwaterman%2Ftanknottank","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkdwaterman%2Ftanknottank/lists"}