Iterate.ai has open-sourced a powerful Weapons Detection AI software. The AI was trained on about 100 live guns, plus 20,000 videos of robberies and threats involving weapons. Our engineers taught the AI to detect guns, knives, kevlar vests, and robbery masks.
version 1.0
WDE - Weapon Dedection Event WEPAPP - Toolkit that uses trained AI models to report the existence of WDEs when dedected via streaming devices, e.g., cameras WEPWEB - Web kit that uses the Weapon Detection Application (WEPAPP) to collect, render and push HTTP-based WDEs WEPHOOK - Webhook that registers for and renders WDEs in realtime
This python-based Web kit uses the Weapon Detection Application (WEPAPP) to collect, render and push HTTP-based Weapon Detection events. See the webapp/README.md file for more information on the Wepapp toolkit
Cameras, along with WEPAPP software, scan for WDEs. The Camera module provides the following:
Processing steps:
Cameras are configured with the following attributes:
Camera configuration example. In the following, 3 cameras are defined for the Science, Library and Residence Hall locations, respectively:
[
{
"name": "Science",
"location": "Science Building",
"video_type": "rtsp",
"video_link": "rtsp://admin:[email protected]",
"frame_skip_size": 100,
"address": {"street": "1234 Science Way"},
"lat": 12.345,
"long": 45.56
},
]
At WEPWEB startup, the Camera module will generate the following six files based on the above:
Afterwhich, three WEPAPP processes will be started using the above sets of files. Each WEPAPP instance will monitor the RTSP link provided in their corresponding configuration files.
See the WEPAPP README file for more details on configuration file content
The WDE module parses the events.json file to create a class object to hold detection event intel provided by the AI software. It contains the following metrics:
A sample WEPAPP event json snippet follows:
{'status': 'Weapon Detected |Gun : 0.87357223', 'video_name': 'Science', 'datetime': '30-06-2023 14:38:44', 'image_path': 'weapon_30-06-2023 14:38:44.jpg'}
The application uses SQLite as the database for storing and managing data. SQLite is a lightweight, serverless, and self-contained database engine, making it suitable for smaller applications and projects.
Database File:
The SQLite database file is included in the project, typically named weapon.db
. You don't need to set up a separate database server.
Schema:
The database schema and structure can be found in the SQL scripts located in the instance
directory. If there are migrations or initialization scripts, provide details on how to run them.
Tables:
Connection in Code: In your application code, you can connect to the SQLite database using a library or ORM compatible with SQLite. Ensure that the necessary configurations, such as the database file path, are correctly set.
import sqlite3
# Connect to the SQLite database
connection = sqlite3.connect('path/to/database.db')
# create virtual environment
virtualenv venv
# activate the 'venv'
source venv/bin/activate
# install all the dependencies
pip install -r requirements.txt
WEPWEB is started from a TTY and binds to port 5000 as follows.
$ python app.py
This will start a master process to oversee camera specific WEBAPP processes discussed below, e.g.:
501 10662 574 0 7:29PM ttys002 0:00.34 python app.py
For the following example, assume the following camera configuration:
"cameras": [
{"name": "Science", "location": "science", "link_type": "rtsp", "link": "rtsp://admin:[email protected]", "frame_skip_size": 100, "address": {"street": "1234 Hargrave Way"}, "lat": -12.345, "long": 45.56},
{"name": "Residence", "location": "residence", "link_type": "rtsp", "link": "rtsp://admin:[email protected]", "frame_skip_size": 100, "address": {"street": "1234 Hargrave Way"}, "lat": -12.345, "long": 45.56}
]
}
Upon execution, the following files are generated by the Camera module and place in the config directory:
And the following WEPAPP processes are started using the above configuration files:
501 10670 10666 0 7:29PM ttys002 0:00.00 /bin/sh ./weprunner.sh config/wep_Residence.ini
501 10671 10665 0 7:29PM ttys002 0:00.00 /bin/sh ./weprunner.sh config/wep_Science.ini
The log files for each camera are maintained in the logs directory. For example, for the above, the following files now exist with camera details logged to corresponding log files:
-rw-r--r-- 1 reed staff 62605 Jul 3 19:41 wep_Science.log
-rw-r--r-- 1 reed staff 62630 Jul 3 19:41 wep_Residence.log
Startup data and event processing is logged to these files, for example:
2023-07-03 19:29:42 : INFO : <module> : 30 - Setup complete (v1.0)
2023-07-03 19:29:42 : INFO : <module> : 39 - No GPU devices found
2023-07-03 19:29:42 : INFO : <module> : 50 - Input request: {'video_link': 'rtsp://admin:[email protected]', 'building': 'science', 'video_type': 'rtsp', 'friendly_name': 'Science', 'file_original_name': None}
2023-07-03 19:29:42 : INFO : <module> : 58 - Detection for video stream starts...
2023-07-03 19:29:42 : INFO : <module> : 62 - GPU Utilization before:
2023-07-03 19:29:42 : INFO : <module> : 23 - imported packages within inference_images_weapon
2023-07-03 19:29:42 : INFO : <module> : 41 - loading weapon model: tf
2023-07-03 19:29:58 : INFO : <module> : 50 - loaded weapon model
2023-07-03 19:29:58 : INFO : detect : 50 - Video source: rtsp://admin:[email protected]
2023-07-03 19:29:58 : INFO : detect : 71 - msg_local_py[payload]: {'video_link': 'rtsp://admin:[email protected]', 'building': 'science', 'video_type': 'rtsp', 'friendly_name': 'Science', 'file_original_name': None}
See the WEPAPP README file for more details
To shutdown WEPWEB, send SIGINT (CTRL+C) or SIGTERM to the master WEPWEB process, e.g.,
501 10662 574 0 7:29PM ttys002 0:00.34 python app.py
As WDEs occur, the corresponding WEPAPP process will record the event to it's log file, e.g.:
2023-07-03 19:51:47 : INFO : do_restful : 25 - post to local wep RESTful service {"status": "Weapon Detected |Gun : 0.94016784", "video_name": "Residence", "datetime": "03-07-2023 19:51:47", "Threat_status": "Threat detected | Weapon:['Gun : 0.94016784']", "image_path": "weapon_03-07-2023 19:51:47.jpg", "weapon_images": ["weapon_03-07-2023 19:51:47.jpg"]}
2023-07-03 19:51:47 : INFO : do_restful : 27 - response: <Response [200]>
2023-07-03 19:51:47 : INFO : do_restful : 29 - post to remote RESTful service {"status": "Weapon Detected |Gun : 0.94016784", "video_name": "Residence", "datetime": "03-07-2023 19:51:47", "Threat_status": "Threat detected | Weapon:['Gun : 0.94016784']", "image_path": "weapon_03-07-2023 19:51:47.jpg", "weapon_images": ["weapon_03-07-2023 19:51:47.jpg"]}, url:http://localhost:5001/consumeevent
Note the message POST. WDE details are POSTed to a RESTful service configured for the WEPAPP instance. See the WEPAPP README for more details. In this case, it's posting the event to the WEAPP, for example:
event_json type: <class 'dict'>
event_json: {'status': 'Weapon Detected |Gun : 0.94016784', 'video_name': 'Residence', 'datetime': '03-07-2023 19:51:47', 'Threat_status': "Threat detected | Weapon:['Gun : 0.94016784']", 'image_path': 'weapon_03-07-2023 19:51:47.jpg', 'weapon_images': ['weapon_03-07-2023 19:51:47.jpg']}
The WDE is recorded in the WEPWEB WDE list, it contains WDEs for all cameras.
The WEBAPP also configures an external RESTful service (binds to port 5001) to send WDEs to - it represents a Webhook. In our case, the WEBAPP config template specifies the following Webhook client URL:
[restful]
enable = True
url = http://localhost:5001/consumeevent
See WEBAPP README for more details
A WEPWEB Webhook is provided, implementing the consumeevent address mentioned above. The code is in the webhook_sample directory, embody in the following two files:
The Webhook consumer is a standalone Flask app that runs on a separate port form WEPWEB. A sample startup session follows:
$ python webhook_consumer.py
Server initialized for eventlet.
* Restarting with watchdog (fsevents)
Server initialized for eventlet.
* Debugger is active!
* Debugger PIN: 143-180-938
(11317) wsgi starting up on http://[::1]:5001
Subsequently, when the webhook_consumer.html is accessed from Web browser, WDEs will be streamed in realtime.
The following shows snippets of web pages described above.
Selecting the Cameras tab will show configured camera info:
Selecting the Events tab will show any WDEs, one per line:
Each WDE will provide the following info:
The following is a sample WDE rendered by the WEPHOOK
Make sure you have the following installed on your machine:
cd frontend
npm install
npm run dev
Open your web browser and navigate to http://localhost:3000. You should see your Next.js application running.
If you encounter issues, refer to the Next.js documentation(https://nextjs.org/docs) for troubleshooting and additional information.
MIT
Balasundram Arunn, Chatura Samarasinghe, Reed McCauley, Janarthan J, Gamsan Vigneswaran, Ramakrishnan Velmakivan