From 43d964e587b72fc0f083f639e3b21f25b72558fd Mon Sep 17 00:00:00 2001 From: Sunkara Venkata Karthik Sai Date: Fri, 25 Oct 2024 12:33:59 +0530 Subject: [PATCH] Added Weapon-Detection-System --- WEAPON-DETECTION-SYSTEM/.gitattributes | 2 + WEAPON-DETECTION-SYSTEM/README.md | 97 +++ WEAPON-DETECTION-SYSTEM/req.txt | Bin 0 -> 1682 bytes WEAPON-DETECTION-SYSTEM/weapon_detection.py | 74 ++ WEAPON-DETECTION-SYSTEM/weapon_detection1.py | 74 ++ WEAPON-DETECTION-SYSTEM/yolov3_testing.cfg | 789 +++++++++++++++++++ 6 files changed, 1036 insertions(+) create mode 100644 WEAPON-DETECTION-SYSTEM/.gitattributes create mode 100644 WEAPON-DETECTION-SYSTEM/README.md create mode 100644 WEAPON-DETECTION-SYSTEM/req.txt create mode 100644 WEAPON-DETECTION-SYSTEM/weapon_detection.py create mode 100644 WEAPON-DETECTION-SYSTEM/weapon_detection1.py create mode 100644 WEAPON-DETECTION-SYSTEM/yolov3_testing.cfg diff --git a/WEAPON-DETECTION-SYSTEM/.gitattributes b/WEAPON-DETECTION-SYSTEM/.gitattributes new file mode 100644 index 0000000000..dfe0770424 --- /dev/null +++ b/WEAPON-DETECTION-SYSTEM/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/WEAPON-DETECTION-SYSTEM/README.md b/WEAPON-DETECTION-SYSTEM/README.md new file mode 100644 index 0000000000..8a0e7ca473 --- /dev/null +++ b/WEAPON-DETECTION-SYSTEM/README.md @@ -0,0 +1,97 @@ +Weapon Detection Using YOLOv3 +============================= + +Project Overview +---------------- + +This project utilizes the YOLOv3 (You Only Look Once) algorithm for real-time weapon detection. The code supports two modes of operation: + +1. **Webcam-based weapon detection**: Uses the webcam to detect weapons in real-time. +2. **Video file-based weapon detection**: Uses a pre-recorded video file to detect weapons. + +Features +-------- + +- Real-time detection of weapons using either a webcam or a video file. +- Display of bounding boxes and labels for detected objects. +- Customizable detection thresholds. + +Technologies and Libraries Used +------------------------------- + +- **Python (version 3.x)** +- **OpenCV**: Library for computer vision tasks. +- **NumPy**: Library for numerical computations. +- **YOLOv3**: Pre-trained deep learning model for object detection. + +Getting Started +--------------- + +### Prerequisites + +Make sure you have the following installed: + +- Python 3.x +- OpenCV +- NumPy + +You can install the required libraries using pip: + +`pip install opencv-python numpy` + + + +### Setup + +1. **Clone the repository and navigate to the project directory**: + + ```bash + git clone + cd + +2. **Download YOLOv3 weights and configuration files**: + + Download the YOLOv3 weights and configuration files from the official YOLO website or any other reliable source. Place these files in the project directory. + +### Running the Code + +Depending on your use case, run the appropriate script: + +1. **Webcam-based weapon detection**: + + ```bash + python weapon_detection1.py + +2. **Video file-based weapon detection**: + + ```bash + python weapon_detection.py + +### Sample Output + +The following image demonstrates the output of the YOLOv3 model, showing bounding boxes and labels for detected objects: + +![341933578-7d7e3f96-2c43-4b0e-a6f4-9a0ec38037ae](https://github.com/venkat-2811/WEAPON-DETECTION-SYSTEM/assets/147246984/aa55d2f7-d279-4972-85e9-3c16637d7fc0) + + +By following the steps outlined above, you can execute the code and perform real-time weapon detection using either a webcam or a video file. + +Customization +------------- + +You can customize the detection thresholds and other parameters within the script to suit your specific requirements. Detailed comments within the code will guide you through making these adjustments. + +Contact +------- + +For further assistance or inquiries, please reach out via the repository's contact information. + +DESCRIPTION +------------ +This project is a real-time weapon detection system utilizing OpenCV and YOLO (You Only Look Once) object detection framework. The system is designed to detect various weapons such as knives, guns, and bombs using a pre-trained YOLOv3 model. The implementation involves loading the YOLO model with custom-trained weights and configuration files, capturing live video feed from the camera, and processing each frame to detect and highlight weapons. The detection results are displayed on the screen with bounding boxes and labels around the detected weapons. This tool is potentially useful for enhancing security measures in public spaces by providing an automated method for weapon detection. + +![341661359-02732cda-5175-47bc-86ef-31f15307e2f0](https://github.com/venkat-2811/WEAPON-DETECTION-SYSTEM/assets/147246984/159229ec-db56-416a-a665-87417d69a477) + + +here is the video for weapon detection using yolo: - +https://github.com/charann29/cmr_opensource/assets/130299325/32dcf337-e16e-4c4a-aedd-026f7dbea1b6 diff --git a/WEAPON-DETECTION-SYSTEM/req.txt b/WEAPON-DETECTION-SYSTEM/req.txt new file mode 100644 index 0000000000000000000000000000000000000000..ca68910cb04c38865dcedc97aceb8cbaa002703c GIT binary patch literal 1682 zcmai#-EPxR5QO&{iAM>los^afu91)`A@KmnandFwv0cX@O&=bZZ}*(ok&uv;{&;t1 zXJ+@D{P|tk&IYS&YrTEeb88=LuFs9Vw|9C!=;`g?vC<>uCAw7Y)*Ab1>r`DRcCSzA z)wSwt+bB9%t7n&Ra*uh+qIwY0qdssb)xETZa6V43HbR35Xft(f_1|9a;P7@~0Czr_ z0nu6v!G#$N?^0hFfl@gR88?bXU(`RTcCeK#y_)yL3q3@?c=Vf{m9r5}<5~2k$3iT~ z+N!=$2hfQ{@2`5_*t6c3mq=7m^DLF%9g=&fq#g#Po;;m@<;_&p$&Fea@RJ$fZQy|t z?gme=LJd87`Lt5zNxf&P!RJo;GI!L+UOc4iqvGH7_oF%ncBsE~?R%jeg}U=tc$X*B zD6+qxqrLQLFTApK4UO3U5{KH^vxi&FA~;Pw$CtuqLyvp;gp1c0==gt4CoODC<+E?} zGgL(fP|0YdtCG)IF)qfwbz->ltvY!$SmAZ})=5Jv9<6j7@})Cc?R`t zV|0j{f(^aCeePTn*`ep4F0ZKv2zZ?%d%qbvk20a?fy?-TO70HsnBjsDDgyymIsG^> zq?5!gH0~4JKtN-l^E0+7^A4{gI-Xdfy`ytxsfYO#`7G@*fgj@WDh9AY?|2(T{j}qsM)6V;i46E=&L^*BYOspg(_?h* 0.5: + # Object detected + center_x = int(detection[0] * width) + center_y = int(detection[1] * height) + w = int(detection[2] * width) + h = int(detection[3] * height) + + # Rectangle coordinates + x = int(center_x - w / 2) + y = int(center_y - h / 2) + + boxes.append([x, y, w, h]) + confidences.append(float(confidence)) + class_ids.append(class_id) + + indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) + if len(indexes) > 0: + for i in indexes.flatten(): + x, y, w, h = boxes[i] + label = str(classes[class_ids[i]]) + color = colors[class_ids[i]] + cv2.rectangle(img, (x, y), (x + w, y + h), color, 2) + cv2.putText(img, label, (x, y + 30), cv2.FONT_HERSHEY_PLAIN, 3, color, 3) + + cv2.imshow("Image", img) + key = cv2.waitKey(1) + if key == 27: # Press 'Esc' key to break the loop + break + +cap.release() +cv2.destroyAllWindows() diff --git a/WEAPON-DETECTION-SYSTEM/weapon_detection1.py b/WEAPON-DETECTION-SYSTEM/weapon_detection1.py new file mode 100644 index 0000000000..8ffcb25107 --- /dev/null +++ b/WEAPON-DETECTION-SYSTEM/weapon_detection1.py @@ -0,0 +1,74 @@ +import cv2 +import numpy as np + +# Load Yolo +net = cv2.dnn.readNet("yolov3_training_2000.weights", "yolov3_testing.cfg") +net.setPreferableBackend(cv2.dnn.DNN_BACKEND_DEFAULT) +net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU) +classes = ["Weapon"] + +# Open the camera +cap = cv2.VideoCapture(0) + +if not cap.isOpened(): + print("Error: Could not open camera.") + exit() + +while True: + ret, img = cap.read() + + if not ret: + print("Error: Could not read frame from camera.") + break + + height, width, channels = img.shape + + # Detecting objects + blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False) + net.setInput(blob) + + layer_names = net.getLayerNames() + output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()] + colors = np.random.uniform(0, 255, size=(len(classes), 3)) + outs = net.forward(output_layers) + + # Showing information on the screen + class_ids = [] + confidences = [] + boxes = [] + for out in outs: + for detection in out: + scores = detection[5:] + class_id = np.argmax(scores) + confidence = scores[class_id] + if confidence > 0.5: + # Object detected + center_x = int(detection[0] * width) + center_y = int(detection[1] * height) + w = int(detection[2] * width) + h = int(detection[3] * height) + + # Rectangle coordinates + x = int(center_x - w / 2) + y = int(center_y - h / 2) + + boxes.append([x, y, w, h]) + confidences.append(float(confidence)) + class_ids.append(class_id) + + indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) + if len(indexes) > 0: + for i in indexes.flatten(): + x, y, w, h = boxes[i] + label = str(classes[class_ids[i]]) + color = colors[class_ids[i]] + cv2.rectangle(img, (x, y), (x + w, y + h), color, 2) + cv2.putText(img, label, (x, y + 30), cv2.FONT_HERSHEY_PLAIN, 3, color, 3) + + cv2.imshow("Image", img) + key = cv2.waitKey(1) + if key == 27: # Press 'Esc' key to break the loop + break + +cap.release() +cv2.destroyAllWindows() diff --git a/WEAPON-DETECTION-SYSTEM/yolov3_testing.cfg b/WEAPON-DETECTION-SYSTEM/yolov3_testing.cfg new file mode 100644 index 0000000000..e91213b9f3 --- /dev/null +++ b/WEAPON-DETECTION-SYSTEM/yolov3_testing.cfg @@ -0,0 +1,789 @@ +[net] +# Testing +batch=8 +subdivisions=1 +# Training +# batch=64 +# subdivisions=166 +width=416 +height=416 +channels=3 +momentum=0.9 +decay=0.0005 +angle=0 +saturation = 1.5 +exposure = 1.5 +hue=.1 + +learning_rate=0.001 +burn_in=1000 +max_batches = 4000 +policy=steps +steps=400000,450000 +scales=.1,.1 + +[convolutional] +batch_normalize=1 +filters=32 +size=3 +stride=1 +pad=1 +activation=leaky + +# Downsample + +[convolutional] +batch_normalize=1 +filters=64 +size=3 +stride=2 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=32 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=64 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + +# Downsample + +[convolutional] +batch_normalize=1 +filters=128 +size=3 +stride=2 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=64 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=128 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + +[convolutional] +batch_normalize=1 +filters=64 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=128 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + +# Downsample + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=2 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=128 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + +[convolutional] +batch_normalize=1 +filters=128 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + +[convolutional] +batch_normalize=1 +filters=128 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + +[convolutional] +batch_normalize=1 +filters=128 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + + +[convolutional] +batch_normalize=1 +filters=128 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + +[convolutional] +batch_normalize=1 +filters=128 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + +[convolutional] +batch_normalize=1 +filters=128 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + +[convolutional] +batch_normalize=1 +filters=128 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + +# Downsample + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=2 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + +# Downsample + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=2 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + +[convolutional] +batch_normalize=1 +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + +[convolutional] +batch_normalize=1 +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + +[convolutional] +batch_normalize=1 +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[shortcut] +from=-3 +activation=linear + +###################### + +[convolutional] +batch_normalize=1 +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +size=1 +stride=1 +pad=1 +filters=18 +activation=linear + + +[yolo] +mask = 6,7,8 +anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326 +classes=1 +num=9 +jitter=.3 +ignore_thresh = .7 +truth_thresh = 1 +random=1 + + +[route] +layers = -4 + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[upsample] +stride=2 + +[route] +layers = -1, 61 + + + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=512 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=512 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=512 +activation=leaky + +[convolutional] +size=1 +stride=1 +pad=1 +filters=18 +activation=linear + + +[yolo] +mask = 3,4,5 +anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326 +classes=1 +num=9 +jitter=.3 +ignore_thresh = .7 +truth_thresh = 1 +random=1 + + + +[route] +layers = -4 + +[convolutional] +batch_normalize=1 +filters=128 +size=1 +stride=1 +pad=1 +activation=leaky + +[upsample] +stride=2 + +[route] +layers = -1, 36 + + + +[convolutional] +batch_normalize=1 +filters=128 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=256 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=128 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=256 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=128 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=256 +activation=leaky + +[convolutional] +size=1 +stride=1 +pad=1 +filters=18 +activation=linear + + +[yolo] +mask = 0,1,2 +anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326 +classes=1 +num=9 +jitter=.3 +ignore_thresh = .7 +truth_thresh = 1 +random=1 +