๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

ํ”„๋กœ์ ํŠธ/AI

[AI] Yolo v3๋ฅผ ์ด์šฉํ•œ ์ด์ƒํ–‰๋™ ๊ฐ์ง€ ์ธ๊ณต์ง€๋Šฅ

728x90

 

2021.03.20.-2021.06.01.

โ€‹

โ€‹

๋ฒ”์ฃ„ ์˜ˆ๋ฐฉ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” CCTV๋ฅผ ๊ด€๋ฆฌํ•  ์ธ๋ ฅ์ด ๋ถ€์กฑํ•˜์—ฌ ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ์ธ๊ณต์ง€๋Šฅ์ด ํญ๋ ฅํ–‰์œ„๋ฅผ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ธ๊ณต์ง€๋Šฅ์„ ๋ชจ๋ธ๋ง ๋ฐ ํ•™์Šต์‹œํ‚ค๊ณ , ์‹ค์ œ ํ™˜๊ฒฝ์— ๋ฐ”๋กœ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ธ๊ณต์ง€๋Šฅ ๊ฐœ๋ฐœํ•˜์˜€๋‹ค.

โ€‹

์ด์ƒํ–‰๋™ ๊ฐ์ง€ ์ „์ฒด ๋ธ”๋ก๋„

 

 

์˜์ƒ์ด yolo network๋กœ ์ „๋‹ฌ๋˜๋ฉด ๋‹ค์ˆ˜์˜ ๊ฒฝ๊ณ„๋ฐ•์Šค์™€ ๊ทธ๋ฆฌ๋“œ ์…€์„ ํ†ตํ•ด์„œ ๊ฐ’์„ ์–ป์–ด ์ด๋ฅผ ๊ฐ์ฒด๋กœ ๊ฒ€์ถœํ•œ๋‹ค.

โ€‹

YOLO๋ฅผ ํ•™์Šต์‹œํ‚ค๊ธฐ ์œ„ํ•ด ํ•™์Šต๊ณผ ํŠธ๋ ˆ์ด๋‹ ๋ฐ์ดํ„ฐ๋ฅผ ์ •์˜ํ•˜๋Š” data ํŒŒ์ผ, CNN ๋ ˆ์ด์–ด์˜ ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•˜๋Š” cfgํŒŒ์ผ, ํ•™์Šต์‹œํ‚จ ๊ฐ€์ค‘์น˜ ์ •๋ณด๊ฐ€ ๋“ค์–ด์žˆ๋Š” weights ํŒŒ์ผ ์„ธ ๊ฐ€์ง€๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

โ€‹

YOLO์—์„œ๋Š” ๊ธฐ๋ณธ์ ์ธ ๊ฐ์ฒด๊ฒ€์ถœ์— ๋Œ€ํ•œ cfgํŒŒ์ผ๊ณผ weights ํŒŒ์ผ์„ ์ œ๊ณตํ•œ๋‹ค.

โ€‹

์ด๋ฅผ ํ™œ์šฉํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์šฐ๋ฆฌ๋Š” ํ™”์งˆ์ด ๋‚ฎ๊ฑฐ๋‚˜ ์–ด๋‘์šด ๊ณจ๋ชฉ์—์„œ ๋” ์ •ํ™•ํ•œ ๊ฐ์ฒด ์ธ์‹์„ ์›ํ–ˆ๊ธฐ์— ์ด์— ๋Œ€ํ•œ ํ•™์Šต ๋ฐ์ดํ„ฐ์…‹์„ ํ†ตํ•ด ๊ฐ์ฒด ์ธ์‹์„ ํ•™์Šตํ•œ ํ›„ ํญ๋ ฅํ–‰์œ„์— ๋Œ€ํ•œ ํ•™์Šต๋„ ์ง„ํ–‰ํ•˜์˜€๋‹ค.

โ€‹

๊ฐ์ฒด ๊ฒ€์ถœ ๋ฐ ํญ๋ ฅํ–‰์œ„ ์ธ์‹ ๋ธ”๋ก๋„

 

ํ•™์Šต๋œ ์ธ๊ณต์ง€๋Šฅ์—๊ฒŒ ์˜์ƒ์„ ํ‹€์–ด์ฃผ๋ฉด YOLO network๊ฐ€ ์ธ์‹ํ•˜์—ฌ ๋‹ค์ˆ˜์˜ ๊ฒฝ๊ณ„๋ฐ•์Šค์™€ NMS ์‹ค์ œ ๊ฒฝ๊ณ„ ๋ฐ•์Šค๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด ๊ฒ€์ถœ๊ณผ ํญ๋ ฅ์ธ์‹์„ ํ•˜๊ฑฐ๋‚˜ ํด๋ž˜์Šค๋ฅผ ํ‘œํ˜„ํ•œ 7X7 ๊ทธ๋ฆฌ๋“œ ์…€์„ ํ†ตํ•ด ๊ฐ์ฒด ๊ฒ€์ถœ ๋ฐ ํญ๋ ฅ์ธ์‹์„ ํ•œ๋‹ค.

โ€‹

ํญ๋ ฅ๊ณผ ๊ฐ์ฒด๋Š” ๊ฐ๊ฐ fight์™€ person์œผ๋กœ ์ •ํ™•๋„์™€ ํ•จ๊ป˜ ์˜์ƒ์— ๋„ค๋ชจ ๋ฐ•์Šค๋กœ ๋„์›Œ์„œ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

โ€‹

โ€‹


์‹คํ—˜ํ™˜๊ฒฝ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

โ€‹

Windows 10

OpenCV

Visual Studio

Cuda 10.1

NVIDIA GeForce GTX 660

โ€‹

โ€‹

โ€‹

Yolo v3๋ฅผ ์ด์šฉํ•ด ํ•™์Šต์‹œํ‚ค๊ธฐ ์ „ ์„ธํŒ…ํ•ด์•ผํ•  ๋ถ€๋ถ„์ด ์žˆ๋‹ค.

โ€‹

 

cfgํŒŒ์ผ

 

 

classes : ๋ฐ์ดํ„ฐ ์ข…๋ฅ˜์˜ ์ˆ˜, fight์™€ person 2๊ฐœ๋กœ ์„ค์ •

max_batches : 4000 x classes์˜ ๊ฐœ์ˆ˜

steps : ์œ„์—์„œ ์„ค์ •ํ•œ max_batches ์‚ฌ์ด์ฆˆ์˜ 80%์™€ 90%๋ฅผ ์„ค์ •

โ€‹

โ€‹


Yolo-mark ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ผ๋ฒจ๋ง์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

Yolo-mark๋กœ fight, person์„ ๋ผ๋ฒจ๋งํ•˜๋Š” ๊ณผ์ •

 

 

yolo – mark๋ฅผ ํ†ตํ•ด ์‹ค๋‚ด์™ธ ์‚ฌ๋žŒ ๊ฐ์ฒด์™€ ํญ๋ ฅํ–‰์œ„ ์žฅ๋ฉด์„ ๋ผ๋ฒจ๋งํ•˜์—ฌ YOLO๊ฐ€ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋Š” data์ธ txtํŒŒ์ผ๋กœ ๋งŒ๋“ค์–ด ์ค€๋‹ค.

โ€‹

์ด์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ์…‹์„ ์‹ค๋‚ด์™ธ ํ•ฉ์ณ 1600์žฅ์„ ๋ผ๋ฒจ๋ง ํ•˜์˜€๊ณ  ์ด๋ฅผ ํ•™์Šตํ•œ๋‹ค.

โ€‹

๋ผ๋ฒจ๋ง๋œ txtํŒŒ์ผ

 

 

๋ผ๋ฒจ๋ง์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด txt์— ์ˆซ์ž๊ฐ€ ์žˆ๋‹ค.

โ€‹


 

cmd์—์„œ ์˜์ƒ์„ ์ž…๋ ฅํ•˜์—ฌ ์ถœ๋ ฅ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋ช…๋ น์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

>> darknet.exe detector demo data/obj.data testcfg/yolov3-tiny.cfg backup/yolov3-tiny_final.weights -i 0 data\fight\data2.mp4 
>> darknet.exe detector demo data/obj.data testcfg/yolov3-tiny.cfg backup/yolov3-tiny_final.weights -i 0 (ํ™•์ธํ•  ๋™์˜์ƒ ํŒŒ์ผ)

 

์ถ”๊ฐ€๋กœ ํ•™์Šต์„ ์›ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž…๋ ฅํ•œ๋‹ค.

darknet detector train data\obj.data testcfg\yolov3-tiny.cfg backup/yolov3-tiny_final.weights -gpu 0
 
2000๋ฒˆ๋Œ€ ํ•™์Šต ๊ฒฐ๊ณผ

 

2000ํšŒ๋ฅผ ํ•™์Šต ํ•œ ํ›„ ๊ฐ์ฒด ๊ฒ€์ถœ์˜ ๊ฒฐ๊ณผ๋Š” ์œ„์™€ ๊ฐ™๋‹ค.

โ€‹

โ€‹

5000๋ฒˆ๋Œ€ ์†์‹ค๋ฅ 

 

5000๋ฒˆ๋Œ€์˜ ์†์‹ค๋ฅ  ๊ทธ๋ž˜ํ”„๋Š” ์œ„์™€ ๊ฐ™๋‹ค.

โ€‹


30000๋ฒˆ๋Œ€ ํ•™์Šต ํ›„ ๊ฐ์ฒด๊ฒ€์ถœ

 

30000๋ฒˆ๋Œ€ ํ•™์Šต ํ›„ ๊ฐ์ฒด๊ฒ€์ถœ

 

์ตœ์ข…์ ์œผ๋กœ person ๊ฐ์ฒด์™€ fight๋ผ๋Š” ํญ๋ ฅํ–‰์œ„๋ฅผ ๊ฒ€์ถœํ•œ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ถœ๋ ฅ์„ ๋ณด์ธ๋‹ค.

โ€‹

์ฒซ๋ฒˆ์งธ ์‚ฌ์ง„์€ ์‹ค๋‚ด ํญ๋ ฅํ–‰์œ„ ์˜์ƒ์„ ์ž…๋ ฅ data๋กœ ์ง‘์–ด๋„ฃ์–ด ๊ฐ์ฒด๋Š” 34%, 37%์˜ ์ •ํ™•๋„๋ฅผ ๋ณด์ด๊ณ  ํญ๋ ฅํ–‰์œ„๋Š” 55%์˜ ์ •ํ™•๋„๋ฅผ ๋ณด์ด๋ฉฐ ํญ๋ ฅํ–‰์œ„๋ผ๋Š” ์ถœ๋ ฅ์„ ํ™”๋ฉด์ƒ์— ๊ทธ๋ ค์ค€๋‹ค.

โ€‹

๋‘๋ฒˆ์งธ ์‚ฌ์ง„์€ ์‹ค์™ธ ํญ๋ ฅํ–‰์œ„ ์˜์ƒ์„ ์ž…๋ ฅ data๋กœ ์ง‘์–ด๋„ฃ์–ด ๊ฐ์ฒด๋ฅผ 40%, 53%์˜ ์ •ํ™•๋„๋กœ ๊ฒ€์ถœํ•˜๊ณ  ํญ๋ ฅํ–‰์œ„๋Š” 38%์˜ ์ •ํ™•๋„๋กœ ํ™•์ธํ•˜์—ฌ ์ถœ๋ ฅ์œผ๋กœ ๋‚˜ํƒ€๋‚œ๋‹ค.

โ€‹

 


 

์›น ์บ ์„ ํ†ตํ•˜์—ฌ ๊ฐ์ฒด ์ธ์‹์„ ํ•  ๊ฒฝ์šฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

โ€‹

import cv2
import numpy as np

# ์›น์บ  ์‹ ํ˜ธ ๋ฐ›๊ธฐ
VideoSignal = cv2.VideoCapture(0)
# YOLO ๊ฐ€์ค‘์น˜ ํŒŒ์ผ๊ณผ CFG ํŒŒ์ผ ๋กœ๋“œ
YOLO_net = cv2.dnn.readNet("yolo-obj_16000.weights","yolo-obj.cfg")

# YOLO NETWORK ์žฌ๊ตฌ์„ฑ
classes = []
with open("coco.names", "r") as f:
    classes = [line.strip() for line in f.readlines()]
layer_names = YOLO_net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in YOLO_net.getUnconnectedOutLayers()]

while True:
    # ์›น์บ  ํ”„๋ ˆ์ž„
    ret, frame = VideoSignal.read()
    h, w, c = frame.shape

    # YOLO ์ž…๋ ฅ
    blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0),
    True, crop=False)
    YOLO_net.setInput(blob)
    outs = YOLO_net.forward(output_layers)

    class_ids = []
    confidences = []
    boxes = []

    for out in outs:

        for detection in out:

            scores = detection[5:]
            class_id = np.argmax(scores)
            confidence = scores[class_id]

            if confidence > 0.5:
                # Object detected
                center_x = int(detection[0] * w)
                center_y = int(detection[1] * h)
                dw = int(detection[2] * w)
                dh = int(detection[3] * h)
                # Rectangle coordinate
                x = int(center_x - dw / 2)
                y = int(center_y - dh / 2)
                boxes.append([x, y, dw, dh])
                confidences.append(float(confidence))
                class_ids.append(class_id)

    indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.45, 0.4)


    for i in range(len(boxes)):
        if i in indexes:
            x, y, w, h = boxes[i]
            label = str(classes[class_ids[i]])
            score = confidences[i]

            # ๊ฒฝ๊ณ„์ƒ์ž์™€ ํด๋ž˜์Šค ์ •๋ณด ์ด๋ฏธ์ง€์— ์ž…๋ ฅ
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 5)
            cv2.putText(frame, label, (x, y - 20), cv2.FONT_ITALIC, 0.5, 
            (255, 255, 255), 1)

    cv2.imshow("YOLOv3", frame)

    if cv2.waitKey(100) > 0:
        break
 

 

โ€‹

โ€‹

โ€‹


[1] https://www.index.go.kr/potal/main/EachDtlPageDetail.do?idx_cd=2855

[2] CCTV ํ†ตํ•ฉ๊ด€์ œ์„ผํ„ฐ ์šด์˜์‹คํƒœ ๋ฐ ๊ฐœ์„ ๋ฐฉ์•ˆ, p32

[3] Girshick, R., Donahue, J., Darrell, T. and Malik, J., Rich feature hierarchies for accurate object detection and semantic segmentation. CVPR, pp.580-587, 2014.

[4] Girshick, R. Fast r-cnn, ICCV, pp.1440-1448, 2015.

[5] Ren, S., He, K., Girshick, R., & Sun, J., Faster rcnn: Towards real-time object detection with region proposal networks, NIPS, pp.91-99, 2015.

[6] https://jjjhong.tistory.com/25

[8] http://www.dbpia.co.kr/journal/articleDetail?nodeId=NODE06281351

[9] http://daddynkidsmakers.blogspot.com/2020/05/yolo.html

๋ฐ์ดํ„ฐ ์ œ๊ณต : https://aihub.or.kr/aidata/139