# Towards an Indoor Gunshot Detection and Notification System Using Deep Learning
## 3 Materials and Methods
### Firmware  
The Jetson Nano board is equipped with a 64 GB SD card, hosting Bionic Beaver, which is a specialized version of the Ubuntu 18.04 operating system. The application software is developed using the Python language, and all required packages, including JetPack 4.6.3, are installed on the system. Three Python programs--to configure Wi-Fi, detect gunshots,  
Figure 4: Block diagram of the gunshot detector device hardware. and access the recorded sounds--run in parallel in separate threads after the system boots. They are briefly described below. Configure Wi-Fi: The purpose of this program is to configure the Wi-Fi connection of the device using the user's smartphone. After the booting, this program enables the Bluetooth advertisement [52] of the Jetson Nano, so that the device is visible to the user's smartphone when scanning for nearby Bluetooth devices. Here, the Jetson Nano works as a Bluetooth server, and the smartphone as a Bluetooth client. The program then waits for a Bluetooth connection from the client using a socket [53]. A timeout is used that will close the socket, disable Bluetooth advertising, and terminate the program if there is no connection request within 30 min after the boot. This shortening of the advertisement duration by timeout will prevent unwanted access to the device using Bluetooth. Once the smartphone connects with the device, the Bluetooth advertising is disabled and it waits to receive commands from the smartphone. The smartphone needs to know the Wi-Fi service set identifier (SSIDs) that are nearby to the device. When the smartphone sends a command to the device requesting the list of nearby SSIDs, the device generates the list using the nmcli tool for Linux devices [54] and sends the list to the smartphone. In the smartphone, the user can choose the desired Wi-Fi SSID the device should connect and enter the password. The smartphone then sends a command, which includes the SSID and password, to the device requesting to connect. Once the device receives the command for Wi-Fi connection, the device tries to connect with the requested SSID and then replies with the connected SSID and its' local IP address. After the Wi-Fi configuration is performed, the smartphone sends a performed command, and the device then closes the socket connection, enables advertising, and waits for a new Bluetooth connection up to the timeout. Detect Gunshot: A flowchart of the gunshot detection firmware is shown in Figure 5. First, it captures a one-second sound having a sampling rate of 44,100 Hz [55]. To classify the sound, it is normalized using the min-max normalization method [35] and then the MFCC of the sound signal is calculated [38]. Then, reading from the norm.npy file, the mean is subtracted and then divided by the standard deviation. The signal is then classified using the generated TRT engine, as discussed in Section 3.1.4., as gunshot or other. The engine outputs the gunshot probability of the captured sound, thus a probability greater than or equal to 0.5 is classified as gunshot sound. The flag isGunshot is set to True if the sound is classified as a gunshot, and False otherwise. The heartbeat LED is turned on at the beginning of the classification, and it is turned off after the classification. Figure 5: Flowchart of the gunshot detection firmware implemented in the microcontroller. If a gunshot is detected, then the sound is saved as a WAV file [56] in the rec_gs folder in the SD card with the current date and time as the filename. As the device is connected to Wi-Fi, it can provide the correct date and time information [57]. To avoid continuous notifications when several gunshots are fired one after the other within a short time, the program sends only one notification to the server for the first captured sound and does not send any notification for the successive gunshot sounds until a non-gunshot sound is captured. The last detected sound status is saved in the isPrevGunshot flag.