Skip to content

macosma1/yolov5_object_detection

Repository files navigation



 

English | [简体中文](.github/README_cn.md)
CI CPU testing YOLOv5 Citation Docker Pulls
Open In Colab Open In Kaggle Join Forum

YOLOv5 🚀 is a family of object detection architectures and models pretrained on the COCO dataset, and represents Ultralytics open-source research into future vision AI methods, incorporating lessons learned and best practices evolved over thousands of hours of research and development.

Documentation

See the YOLOv5 Docs for full documentation on training, testing and deployment.

Quick Start Examples

Install

Clone repo and install requirements.txt in a Python>=3.7.0 environment, including PyTorch>=1.7.

git clone https://github.com/pablomuo/yolov5_object_detection.git
cd Object_detection_using_yolov5
pip install -r requirements.txt
Inference with detect.py

detect.py runs inference on a variety of sources, downloading models automatically from the latest YOLOv5 release and saving results to runs/detect.

python detect.py --source 0  # webcam
                          img.jpg  # image
                          vid.mp4  # video
                          path/  # directory
                          path/*.jpg  # glob
                          'https://youtu.be/Zgi9g1ksQHc'  # YouTube
                          'rtsp://example.com/media.mp4'  # RTSP, RTMP, HTTP stream
Training

The commands below reproduce YOLOv5 COCO results. Models and datasets download automatically from the latest YOLOv5 release. Training times for YOLOv5n/s/m/l/x are 1/2/4/6/8 days on a V100 GPU (Multi-GPU times faster). Use the largest --batch-size possible, or pass --batch-size -1 for YOLOv5 AutoBatch. Batch sizes shown for V100-16GB.

python train.py --data coco.yaml --cfg yolov5n.yaml --weights '' --batch-size 128
                                       yolov5s                                64
                                       yolov5m                                40
                                       yolov5l                                24
                                       yolov5x                                16
Tutorials
Labeling

To label the database with the YOLOv5 format, the use of the ModifiedOpenLabelling tool is recommended. To install it and tag all images, follow the steps below:

# To install ModifiedOpenLabelling clone the following repository.
git clone https://github.com/ivangrov/ModifiedOpenLabelling
# In the file "class\_list.txt", list all classes to be detected.
# Paste all the images to be labeled in folder "images".
python run.py #execute the script
# Once all the images are finished, in the folder "bbox\_txt" will be a ".txt" file for each image.

Training custom dataset

  • Once the whole database has been labelled, move the images for training to data_train/images/train and the validation ones to data_train/images/val. Move the labels of the training images to data_train/labels/train, and the labels of validation to data_train/labels/val.
  • Go to folder "data" and modify the file "coco128.yaml". Firstly, indicate the total number of classes in variable "nc" and secondly, in variable "names" are the names of the classes, they must be in the same order in which they have been ordered when labelled.
  • The model can already be trained. It is necessary to indicate the number of epochs, for example 300. Run the following code:
python train.py --data coco128.yaml --weights yolo5s.pt --epochs 300
  • Once the model has finished the training, the weights will have been stored in runs/train/exp/weights/best.pt. To run the model with the weights of the training, run the following code:
python detect.py --weights runs/train/exp/weights/best.pt --source 0

Running the pre-trained model

For running the pre-trained model, the weights "weights.pt" must be used in the next code:

python detect.py --weights weights.pt --source 0

Modifications to use Gstreamer and WebSocket

Transmit with Gstreamer from AGV to Yolov5

To carry out the transmission of the stream from the AGV camera (Client I) to Yolov5 (Server I) is necessary the codes of the folder 'summit_codes' where there are two files, 'vid_tx.sh' and 'master_edit.config' that are responsible for the transmission. In 'master_edit.config the parameters are established and it will be necessary to indicate the 'IP' and the 'port' of the Server I. The code 'vid_tx.sh' will will be in charge of making the transmission from the parameters of 'master_edit.config'. In this code the parameters to transmit are set in:

IP_TX=10.236.24.39
PORT_TX=8554
Receive with Gstreamer from AGV

In order to (server) receive the streaming from the Robotnik Summit XL camera (Client I), it is necessary to modify the 'port' in the 'dataloaders.py' code through which it is transmitting the video.

class LoadStreams:
    def __init__():
    ...
       gstreamer_str = (f'udpsrc port={PORT_RX} auto-multicast=0 ! application/x-rtp, media=video, encoding-name=H264 !\
            rtpjitterbuffer latency=300 ! rtph264depay ! decodebin ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1')            
       cap = cv2.VideoCapture(gstreamer_str, cv2.CAP_GSTREAMER)
    ...
Transmit with Gstreamer from Yolov5 to platform

To be able to transmit the video output of the YOLOv5 algorithm, it is necessary to set the 'IP' and the 'port' in the 'detect.py' code:

#Example to send the stream to the client II     
    #send the stream to the client via GStreamer
    gst_str_rtp =(f'appsrc ! videoconvert ! videoscale ! video/x-raw,format=I420,width=1280,height=720,framerate=20/1 !  videoconvert !\
         x264enc tune=zerolatency bitrate=3000 speed-preset=superfast ! rtph264pay !\
         udpsink host= {IP_TX} port= {PORT_TX}')
    fourcc = cv2.VideoWriter_fourcc(*'H264')
    out_send = cv2.VideoWriter(gst_str_rtp, fourcc, 20, (1280, 720), True)
Transmit with WebSocket

To be able to transmit the classes, weights, areas of the detected objects output of the YOLOv5 algorithm, it is necessary to set the 'IP' and the 'port' in the 'detect.py' code:

#send infor (text) via WebSocket to the Servidor_Broadcast and then to the platform 
    HOST_PORT = (f'ws://{IP_TX}:{PORT_SB}')
Transmit messages from Server II to the platform

In order to transmit the information from the server to the platform, it is necessary to establish the 'port' and the 'IP' of the server II, which is going to send the information to the platform (client II). This is set in 'servidor_broadcast.py' code:

   start_server = websockets.serve(echo, IP_TX, PORT_SB)
config.ini

The “config.ini” file is in charge of configuring the necessary specifications of “IP”, “port”, among others, which will be required at the time of the video transmission in real-time and the alert.

; config.ini
[DEFAULT]
PORT_RX= 8554
PORT_TX= 8650
IP_TX= 192.168.100.74
PORT_SB= 8000

For a better understanding of the architecture, see the following figure

#To run the full yolov5 algorithm
python ejecutar.py

YOLOv5 with Docker

For running the algorithm with docker, as long as docker is downloaded, it is necessary to be located in the 'utils/docker' folder where the Dockerfile and the requirements.txt file are. Once there, the following code must be executed to create the yolov5 image:

docker build -t yolov5:v1 .

Once the image is created, it is necessary to execute the following code in the terminal, which will set the appropriate ’IP’ and ’port’ parameters and in turn, execute the yolov5 algorithm:

docker run -it --rm   --name yolo  --net=host  -e IP_TX=192.10.25.55   -e IP_SB=10.236.1.1   -e PORT_TX=8650   -e PORT_RX=8554   -e PORT_SB=8000   -e CONF_MIN=0.6 -e DISPLAY   -e QT_X11_NO_MITSHM=1   -v /tmp/.X11-unix:/tmp/.X11-unix   -v $HOME/.Xauthority:/root/.Xauthority   --device /dev/video0   --device /dev/video1 yolov5:v1

Contribute

We love your input! We want to make contributing to YOLOv5 as easy and transparent as possible. Please see our Contributing Guide to get started, and fill out the YOLOv5 Survey to send us feedback on your experiences. Thank you to all our contributors!

Contact

For YOLOv5 bugs and feature requests please visit GitHub Issues. For business inquiries or professional support requests please visit https://ultralytics.com/contact.


About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published