ultralytics / yolov5

YOLOv5 πŸš€ in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.67k stars 16.33k forks source link

How can i generate a alarm single in detect.py so when ever my target object is in the camera's range an alarm is generated? #3963

Closed raja-usama closed 3 years ago

raja-usama commented 3 years ago

❔Question

Additional context

github-actions[bot] commented 3 years ago

πŸ‘‹ Hello @raja-usama, thank you for your interest in πŸš€ YOLOv5! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a πŸ› Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://www.ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

$ pip install -r requirements.txt

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

shershunov commented 3 years ago

Hi You need to use PytorchHub to detect objects. And play the sound effect when an object is detected using the winsound library.

from cv2 import waitKey, destroyAllWindows, imshow, VideoCapture, rectangle
from torch import hub
from winsound import MessageBeep, MB_OK
import time

model = hub.load('ultralytics/yolov5', 'yolov5s')
cap = VideoCapture(0)
while True:
    time.sleep(1)
    ret, img = cap.read()
    result = model(img, size=640)
    repos = result.xyxy[0]
    for i in range(len(repos)):
        pos = repos[i].detach()
        rectangle(img, (int(pos[0]), int(pos[1])), (int(pos[2]), int(pos[3])), (255, 255, 255), 2)
    if len(repos) > 0:
        MessageBeep(type=MB_OK)
    imshow('Detect', img)
    if waitKey(1) & 0xFF == ord("q"):
        destroyAllWindows()
        break
github-actions[bot] commented 3 years ago

πŸ‘‹ Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 πŸš€ resources:

Access additional Ultralytics ⚑ resources:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLOv5 πŸš€ and Vision AI ⭐!

1Yleng commented 1 year ago

How about this one, please? .py?

glenn-jocher commented 1 year ago

πŸ‘‹ Hello! Thanks for asking about handling inference results. YOLOv5 πŸš€ PyTorch Hub models allow for simple model loading and inference in a pure python environment without using detect.py.

Simple Inference Example

This example loads a pretrained YOLOv5s model from PyTorch Hub as model and passes an image for inference. 'yolov5s' is the YOLOv5 'small' model. For details on all available models please see the README. Custom models can also be loaded, including custom trained PyTorch models and their exported variants, i.e. ONNX, TensorRT, TensorFlow, OpenVINO YOLOv5 models.

import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')  # yolov5n - yolov5x6 official model
#                                            'custom', 'path/to/best.pt')  # custom model

# Images
im = 'https://ultralytics.com/images/zidane.jpg'  # or file, Path, URL, PIL, OpenCV, numpy, list

# Inference
results = model(im)

# Results
results.print()  # or .show(), .save(), .crop(), .pandas(), etc.
results.xyxy[0]  # im predictions (tensor)

results.pandas().xyxy[0]  # im predictions (pandas)
#      xmin    ymin    xmax   ymax  confidence  class    name
# 0  749.50   43.50  1148.0  704.5    0.874023      0  person
# 2  114.75  195.75  1095.0  708.0    0.624512      0  person
# 3  986.00  304.00  1028.0  420.0    0.286865     27     tie

results.pandas().xyxy[0].value_counts('name')  # class counts (pandas)
# person    2
# tie       1

See YOLOv5 PyTorch Hub Tutorial for details.

Good luck πŸ€ and let us know if you have any other questions!

1Yleng commented 1 year ago

❔Question

Additional context

Please, How did you solve it later?

glenn-jocher commented 11 months ago

@1Yleng i see that you've made an attempt to generate an alarm when a target object is detected in the camera's range using YOLOv5 and the winsound library. This can be achieved by making use of the results obtained from your object detection model. By checking the detected objects and triggering an alarm using winsound, you can create a successful object detection alarm system.

The code provided shows the process of capturing a video stream, running inference on each frame using the YOLOv5 model, and then displaying the results with detected objects outlined. When a detection occurs, an alarm sound is played. This is a good initial approach!

Feel free to keep iterating on this procedure and leverage the YOLOv5 results to further enhance your object detection alarm system. If you have any more specific questions or encounter any challenges, please let me know, and I'd be happy to assist further!