← Back to list

I Built a Weapon Detection System That Watches My Webcam and Sounds an Alarm

A few weeks ago I set out to answer a fairly simple question: could I build something that watches a live camera feed and actually tells…

Urooj Fatima · 2026-07-06 10:44 · 1 claps · 3.1 min read
#yolov8 #opencv-python #python-programming #machine-learning #computer-vision
Open on Medium ↗
Wiki topics: ML · Machine Learning EDU · Education & Learning 💻 · Programming 📷 · Photography

I Built a Weapon Detection System That Watches My Webcam and Sounds an Alarm

A few weeks ago I set out to answer a fairly simple question: could I build something that watches a live camera feed and actually tells someone the moment it sees a weapon — not just labels a photo after the fact, but reacts in real time?

Here’s what I built, what broke along the way, and what I’d still change.

Why this, specifically

Most of the object detection tutorials and demos I came across fell into one of two buckets: they ran on static images only, or they leaned on a generic pretrained model that wasn’t really built for this. COCO — the dataset most YOLO models are pretrained on — only has one weapon-adjacent class in it: knife. Everything else (guns, rifles, grenade launchers) simply isn’t in there.

So if I wanted broader weapon detection, I’d need to train something myself.

What it actually does

I trained a YOLOv8 model from scratch on a 9-class dataset: automatic rifles, bazookas, grenade launchers, handguns, knives, shotguns, SMGs, snipers, and swords. Then I wired it up to a live webcam feed using OpenCV — every frame gets run through the model, and if something’s detected, a bounding box, class label, and confidence score get drawn right on the video.

One thing I didn’t expect going in: my custom model, despite being purpose-built for weapons, was sometimes worse at detecting knives than the generic pretrained COCO model — probably because COCO was trained on a far larger and more varied set of images. So the final version actually runs both models on every frame and merges the results: my custom model for the 9 weapon classes, and the pretrained model filtered down to just its knife class. Between the two, detection got noticeably more reliable.

The part that actually took the longest

You’d think the model training would be the hard part. It wasn’t. The hard part was the alarm.

My first attempt was almost embarrassingly simple:

if weapon_detected:
    winsound.Beep(1000, 500)

It worked, technically. But winsound.Beep is a blocking call — meaning the entire video feed froze for half a second every single time it fired. And since a weapon typically stays in frame for several seconds, that meant the feed kept stuttering over and over, re-triggering the beep on nearly every frame.

The fix, once I found it, was almost anticlimactic: move the beep onto its own background thread, and track whether an alarm was already “active” so it only fires once per detection event instead of once per frame.

if weapon_detected and not currently_alarming:
    threading.Thread(target=play_alarm, daemon=True).start()
    currently_alarming = True
elif not weapon_detected:
    currently_alarming = False

It’s a small change. But it was the difference between something that looked like a broken prototype and something that actually felt usable.

Seeing it work

Here’s a knife being detected live, bounding box and confidence score drawn in real time:

And the console-side alerting — each detection logged with its confidence, and a clear “safe” state once the frame clears:

Where it falls short

I want to be upfront about this, because I think it matters more than the parts that worked: confidence scores on some classes — knives especially — hover around 30–50% in imperfect lighting. That’s good enough for a proof of concept, but nowhere near good enough for an actual security deployment. It’s also webcam-only right now, with no persistent logging of detection events beyond what prints to the console, and the dataset itself is on the smaller side for a 9-class problem — some classes would clearly benefit from more training examples.

None of that makes it useless. But it does mean this is a prototype, not a product.

What I’d do next

A few things I’m planning to work on: expanding the dataset (especially for the classes with the weakest confidence scores), adding real event logging instead of just console output, and experimenting with a lighter model to see if I can push the frame rate up without losing accuracy.

If you’ve worked on something similar — particularly around improving detection confidence on smaller datasets, or handling audio/video threading issues in a live pipeline — I’d genuinely like to hear how you approached it.

The full code is on GitHub: github.com/uroojbuilds/Computer-Vision/tree/main/weapon-detection

Built with Python, Ultralytics YOLOv8, and OpenCV.


메타데이터
post_id
b884666dcee4
slug
i-built-a-weapon-detection-system-that-watches-my-webcam-and-sounds-an-alarm-b884666dcee4
url
https://medium.com/@uroojbhatti35/i-built-a-weapon-detection-system-that-watches-my-webcam-and-sounds-an-alarm-b884666dcee4
canonical_url
https://medium.com/@uroojbhatti35/i-built-a-weapon-detection-system-that-watches-my-webcam-and-sounds-an-alarm-b884666dcee4
author_url
https://medium.com/@uroojbhatti35
status
ok
fetched_at
2026-07-15 22:45:01