Skip to content

Add real-time webcam inference loop with FPS and compliance counters - #6

Open
MathewTech254 wants to merge 1 commit into
AIMLCLUBOCT:mainfrom
MathewTech254:feat/webcam-inference-loop
Open

MathewTech254 wants to merge 1 commit into
AIMLCLUBOCT:mainfrom
MathewTech254:feat/webcam-inference-loop

Conversation

@MathewTech254

Copy link
Copy Markdown

What this does

Turns the detect.py stub into an actual working detector. It pulls frames from your webcam (or a video file), runs them through YOLO, draws the boxes, and puts an FPS counter and a helmet/no-helmet tally in the corner.

pip install -r requirements.txt
python detect.py                                       # webcam
python detect.py --model helmet-yolov8n.pt --conf 0.5   # custom weights
python detect.py --source campus-footage.mp4           # video file

Press q to quit.

Closes #2

Things I ran into

The default yolov8n.pt is a COCO model and doesn't have a helmet class, so out of the box it will always say NO DETECTIONS no matter what you point it at. That's expected — you need weights trained on helmet classes, which you pass with --model. I mentioned it in the --help text so nobody thinks it's broken.

Class names came back in more shapes than I expected, so classify_label normalises them first. Helmet, no-helmet, No_Helmet, helmetless and without helmet all end up in the right bucket. A plain "no" in label check would have worked too, but it also matches things like Nose helmet, which is not a thing, but you get the idea. Also worth knowing: Ultralytics gives you names as a dict on most models and a list on others, so both are handled.

One more: the first version stopped the whole loop the first time a frame failed to read. Webcams drop frames, so now it puts up with 10 in a row before deciding the stream is actually over.

Testing

No webcam here, so I ran the loop against generated video and real photos with the GUI calls stubbed out.

  • python -m py_compile detect.py clean, --help shows all three flags, yolov8n.pt loads
  • 40-frame video end to end: every frame processed, correct window title, valid BGR frames, HUD drawn, q quits early, end of stream exits clean
  • Bad video path returns exit 1 without drawing anything
  • Real detections come through with boxes, labels and confidence
  • Fed it helmet-classed boxes to check the counters and the COMPLIANT / VIOLATION banner actually flip

Note

Only detect.py changed. requirements.txt and README.md untouched.

@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown

🎉 Thank you for your Pull Request, @MathewTech254!

Welcome to AIML Club OCT! 🚀

A maintainer will review your code shortly. In the meantime:

Happy coding! 💻✨

@UmeshCode1

Copy link
Copy Markdown
Member

Hi @MathewTech254! 👋

Thank you so much for this PR! The real-time webcam loop, FPS calculation, normalized multi-label handling (\helmetless, \No_Helmet, etc.), and 10-frame drop tolerance are fantastic additions to make the detector actually deployable in live environments.

There is currently a small git conflict with the latest \main\ branch in \�eginner/safety-helmet-detector/detect.py\ (due to recent fallback updates).

Could you please sync your fork with upstream \main\ and rebase / resolve the conflict?
\\�ash
git fetch origin main
git rebase origin/main

resolve conflicts in detect.py, then:

git push --force-with-lease
\\

As soon as that's rebased, we are ready to approve and merge this! Appreciate your awesome work! 🚀

@MathewTech254

MathewTech254 commented Oct 23, 2026 via email

Copy link
Copy Markdown
Author

@UmeshCode1 UmeshCode1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the substantial improvement and for documenting the custom-weights requirement. I found blocking issues that should be addressed before merge:

  1. In the frame loop, the capture tuple provides im0s, but the later drawing/display/save code uses im0, p, s, and save_path without assigning them in the shown implementation. This will raise NameError on the first processed frame (and im0 is also needed for plot_one_box). Select the correct frame from im0s for each stream and define the path/label variables, or simplify the implementation around the current single-stream requirement.
  2. helmet_count and no_helmet_count are initialized outside the frame loop and never reset, so the HUD reports cumulative detections across the entire video, not the current frame as the module docstring and README-style description imply. Reset per-frame counters inside the loop; keep separate cumulative totals only if they are explicitly labelled as totals.
  3. The implementation's source handling includes multi-stream dataloader paths, but the result-processing logic should be tested for both single and multi-stream input before claiming support.
  4. Please add or update the detector's dependency/setup documentation and test coverage for label normalization, class-name lookup, frame selection, and counter semantics. The PR description says only detect.py changed, leaving contributors without reproducible dependency instructions.

The PR currently reports merge conflicts (mergeable: false), so those must also be resolved against the current base before it can be considered. The custom helmet-trained weights caveat is correctly called out; please retain it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add real-time webcam inference loop to safety-helmet-detector

2 participants