AI Research/Object Detection

[Object Detection] YOLOv5로 multi stream, multi camera object detection하는 법/ 동시에 여러 video detection 진행하기

wawawaaw 2022. 8. 8. 22:21
반응형

 ⭐ 본 포스팅은 YOLOv5로 여러 video를 동시에 object detection 하는 방법! 

활용할 YOLOv5 코드는 아래와 같다.  

https://github.com/ultralytics/yolov5

 

GitHub - ultralytics/yolov5: YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite. Contribute to ultralytics/yolov5 development by creating an account on GitHub.

github.com

 

multi stream 관련 코드 살펴보기

  • detect.py의 일부분을 보면 data를 받는 부분이 다음과 같이 되어 있다.
# Dataloader
    if webcam:
        view_img = check_imshow()
        cudnn.benchmark = True  # set True to speed up constant image size inference
        dataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt)
        bs = len(dataset)  # batch_size
    else:
        dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt)
        bs = 1  # batch_size
    vid_path, vid_writer = [None] * bs, [None] * bs

 

  • 여기에서 bs가 의미하는 바는 batch size로, detection 진행하고자 하는 카메라의 수를 의미한다. 예를 들어 3대의 카메라를 동시에 object detection을 진행하고자 한다면 bs = 3 이 된다. webcam의 경우 자동으로 batch수를 계산해준다.

 

  • util 폴더 안의 dataloaders.py는 data를 입력을 담당하고 있다. LoadStreams 클래스의 init 함수를 보면 다음과 같이 되어 있다.
def __init__(self, sources='streams.txt', img_size=640, stride=32, auto=True):
        self.mode = 'stream'
        self.img_size = img_size
        self.stride = stride

        if os.path.isfile(sources):
            with open(sources) as f:
                sources = [x.strip() for x in f.read().strip().splitlines() if len(x.strip())]
        else:
            sources = [sources]
.......
  • source의 입력으로 stream을 txt파일로 받을 수 있는 것이다. 

 

실행 순서 

아래 진행되는 순서는 2개의 비디오 detection 을 동시에 실행하는 방법이다. 

1. 실행하고자 하는 streams.txt 파일 생성

  • streams.txt 파일을 다음과 같이 작성해주었다. detection 진행하고 싶은 youtube 영상 2개이다.

2. pafy 모듈 설치

  • youtube stream으로 실행하고자 한다면 pafy 모듈을 설치해주어야한다. 그렇지 않으면 아래와 같은 오류가 뜰 수 있다.

  • 아래 2가지를 차례로 설치해주자.
pip install pafy 
pip install youtube_dl==2020.12.2

 

3. Multi object detection 실행하기

  • 아래 명령어를 입력하면 streams에 적어둔 비디오 stream이 동시에 detection 된다.
python detect.py --source streams.txt
  • 위를 실행하면 2개의 stream을 로드 하는 것을 볼 수 있다.

1/2, 2/2로 2개의 비디오 stream이 로드되었다.

  • 로드가 끝나면 2개의 창이 동시에 실행된다.

반응형