๐ ๋ชฉ์ฐจ
1. YOLO v3๋ก ์ ์ง์์์์ Object Detectionํ๊ธฐ
2. YOLO v3๋ก ๋น๋์ค์์ Object Detectionํ๊ธฐ(๋ฉํ ์บ ์ฌ์ฉ)
0. ํ์ผ์ค๋น ๋ฐ YOLO v3 ๊ณตํต์ฝ๋
๋จผ์ ์์ ์ ๋ก์ปฌ ํด๋์ ๋ค์ 3๊ฐ์ ํ์ผ์ด ์์ด์ผํ๋ค.
โ 'coco_names.txt' (์ด ํ์ผ์ 1~80ํ๊น์ง ๋ณต์ฌํ txtํ์ผ์ ์์ฑํด ๋ถ์ฌ๋ฃ๋๋ค.)
โ 'yolov3.weights'
โ 'yolov3.cfg'
์ดํ ์๋ ์ฝ๋์ ๋ํด ์ค๋ช ํด๋ณด๊ฒ ๋ค.
- 5~13ํ: YOLO ๋ชจ๋ธ ๊ตฌ์ฑํจ์
- 15~38ํ: YOLO๋ชจ๋ธ๋ก img์์์์ ๋ฌผ์ฒดํ์ง ํ ๋ฐํํ๋ ํจ์
def construct_yolo_v3(): f=open('coco_names.txt', 'r') class_names=[line.strip() for line in f.readlines()] model=cv.dnn.readNet('yolov3.weights','yolov3.cfg') layer_names=model.getLayerNames() out_layers=[layer_names[i-1] for i in model.getUnconnectedOutLayers()] return model,out_layers,class_names def yolo_detect(img,yolo_model,out_layers): height,width=img.shape[0],img.shape[1] test_img=cv.dnn.blobFromImage(img,1.0/256,(448,448),(0,0,0),swapRB=True) yolo_model.setInput(test_img) output3=yolo_model.forward(out_layers) box,conf,id=[],[],[] # ๋ฐ์ค, ์ ๋ขฐ๋, ํด๋์ค ๋ฒํธ for output in output3: for vec85 in output: scores=vec85[5:] class_id=np.argmax(scores) confidence=scores[class_id] if confidence>0.5: # ์ ๋ขฐ๋๊ฐ 50% ์ด์์ธ ๊ฒฝ์ฐ๋ง ์ทจํจ centerx,centery=int(vec85[0]*width),int(vec85[1]*height) w,h=int(vec85[2]*width),int(vec85[3]*height) x,y=int(centerx-w/2),int(centery-h/2) box.append([x,y,x+w,y+h]) conf.append(float(confidence)) id.append(class_id) ind=cv.dnn.NMSBoxes(box,conf,0.5,0.4) objects=[box[i]+[conf[i]]+[id[i]] for i in range(len(box)) if i in ind] return objects model,out_layers,class_names=construct_yolo_v3() # YOLO ๋ชจ๋ธ ์์ฑ colors=np.random.uniform(0,255,size=(len(class_names),3)) # ํด๋์ค๋ง๋ค ์๊นโ
05~13ํ์ construct_yolo_v3 ํจ์๋ฅผ ์ดํด๋ณด์.
โ 06~07ํ์ COCO ๋ฐ์ดํฐ์ ์ ํด๋์ค์ด๋ฆ์ ๋ด๊ณ ์๋ coco_names. txt ํ์ผ์์ ํด๋์ค ์ด๋ฆ์ ์ฝ์ด class_names์ ์ ์ฅํ๋ค.
โ 09ํ์ YOLO v3 ๋ชจ๋ธ ์ ๋ณด๋ฅผ ํ์ผ์์ ์ฝ์ด yolo_model ๊ฐ์ฒด์ ์ ์ฅํ๋ค.
yolov3.weights ํ์ผ์์๋ ์ ๊ฒฝ๋ง์ ๊ฐ์ค์น ์ ๋ณด๋ฅผ ์ฝ์ด์ค๊ณ yolov3.cfg ํ์ผ์์๋ ์ ๊ฒฝ๋ง์ ๊ตฌ์กฐ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์จ๋ค.
โ 10~11ํ์ getUnconnectedOutLayers ํจ์๋ฅผ ์ด์ฉํ์ฌ yolo 82, yolo 94, yolo_106 ์ธต์ ์์๋ด์ด out_layers ๊ฐ์ฒด์ ์ ์ฅํ๋ค.
โ 13ํ์ ๋ชจ๋ธ, ์ธต, ํด๋์ค ์ด๋ฆ์ ๋ด์ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
15~38ํ์ yolo_detect ํจ์๋ฅผ ์ดํด๋ณด์.
โ 16ํ์ ์๋ณธ ์์์ธ img์ ๋์ด์ ๋๋น ์ ๋ณด๋ฅผ height์ width์ ์ ์ฅํ๋ค.
โ 17ํ์ OpenCV์ blobFromImage ํจ์๋ก ์์์ YOLO์ ์ ๋ ฅํ ์ ์๋ ํํ๋ก ๋ณํํด test_img์ ์ ์ฅํ๋ค.
์ด ํจ์๋ [0,255] ๋ฒ์์ ํ์๊ฐ์ [0,1]๋ก ๋ณํํ๊ณ ์์ ํฌ๊ธฐ๋ฅผ 448 X 448๋ก ๋ณํํ๋ฉฐ BGR ์์๋ฅผ RGB๋ก ๋ฐ๊พผ๋ค.์๋ณธ ์์์ img์ ๋จ์์๋ค.
โ 19ํ์ test_img์ ์ ์ฅ๋์ด ์๋ ์์์ ์ ๊ฒฝ๋ง์ ์ ๋ ฅํ๋ค.โ 20ํ์ ์ ๊ฒฝ๋ง์ ์ ๋ฐฉ ๊ณ์ฐ์ ์ํํ๋๋ฐ, out layers๊ฐ ์ถ๋ ฅํ ํ ์๋ฅผ output3 ๊ฐ์ฒด์ ์ ์ฅํ๋ค.
์ด๋ก ์ธํด Ouput3 ๊ฐ์ฒด๋ ์๋ 3๊ฐ์ง ํ ์๋ฅผ ๊ฐ๋๋ค.
14×14×85×3,
28×28×85×3,
56×56×85×3โ 22~34ํ์ output3 ๊ฐ์ฒด๋ก๋ถํฐ ๋ฌผ์ฒด ์์น๋ฅผ ๋ํ๋ด๋ ๋ฐ์ค ์ ๋ณด์ ํจ๊ป ๋ฌผ์ฒด ํด๋์ค์ ์ ๋ขฐ๋ ์ ๋ณด๋ฅผ ์ถ์ถํ๋ค.
22ํ์ ๋ฐ์ค์ ์ ๋ขฐ๋, ํด๋์ค์ ๋ณด๋ฅผ ์ ์ฅํ ๋ฆฌ์คํธ๋ฅผ ์์ฑํ๋ค.
23ํ์ ์ธ ๊ฐ์ ํ ์๋ฅผ ๊ฐ๊ฐ๋ฐ๋ณต ์ฒ๋ฆฌํ๋ฉฐ
24ํ์ 85์ฐจ์ ๋ฒกํฐ๋ฅผ ๋ฐ๋ณต ์ฒ๋ฆฌํ๋ค.
85 ์ฐจ์ ๋ฒกํฐ๋ (x, y, w, h,o, p1. p2, … p80)์ผ๋ก ํํ๋๋ฉฐ
์์ ๋ค ์์๋ ๋ฐ์ค, o๋ ์ ๋ขฐ๋, ๋ค์ 80๊ฐ ์์๋ ํด๋์คํ๋ฅ ์ด๋ค.
โ 25~27ํ์ ๋ค์ 80๊ฐ ์์ ๊ฐ์์ ์ต๊ณ ํ๋ฅ ์ ํด๋นํ๋ ํด๋์ค๋ฅผ ์์๋ด ํด๋์ค ๋ฒํธ๋ class_id, ํ๋ฅ ์ confidence์ ์ ์ฅํ๋ค.
โ 28ํ์ confidence๊ฐ 0.5๋ณด๋ค ํฌ์ง ์์ผ๋ฉด ๋ฒ๋ฆฐ๋ค.
0.5๋ณด๋ค ํฌ๋ฉด 29~31ํ์์ [0,1] ๋ฒ์๋ก ํํ๋ ๋ฐ์ค๋ฅผ ์๋ ์์ ์ขํ ๊ณ๋ก ๋ณํํด
์ผ์ชฝ ์์ ์์น๋ฅผ x์ y, ๋๋น์ ๋์ด๋ฅผ w์ h์ ์ ์ฅํ๋ค.
โ 32~34ํ์ ๋ฐ์ค์ ์ ๋ขฐ๋, ํด๋์ค ์ ๋ณด๋ฅผ ๋ฆฌ์คํธ์ ์ถ๊ฐํ๋ค. ๋ฐ์ค๋ ์ผ์ชฝ ์์ ์ค๋ฅธ์ชฝ ์๋ ๊ตฌ์ ์ขํ๋ฅผ ์ ์ฅํ๋ค.
โ 22~34ํ์ผ๋ก ๊ฒ์ถํ ๋ฐ์ค๋ค์๋ ์๋นํ ์ค๋ณต์ฑ์ด ์๋ค.
์ฆ, ์ด์ ์๊ฐ์ ์ค๋ช ํ ๊ทธ๋ฆผ์์ ๋นจ๊ฐ์ ์ ๋ํผ ์ ์๋ฅผ ๊ฒ์ถํ ๋ฐ์ค๊ฐ ๊ฒ์์ ์นธ์๋ง ๋ํ๋์ง ์๊ณ ๊ทธ ์ฃผ์์ ์ฌ๋ฟ ๋ํ๋๋ ํ์์ด๋ค.โ 36ํ์ NMSBoxes ํจ์๋ ๋ฐ์ค๋ฅผ ๋์์ผ๋ก ๋น์ต๋ ์ต์ ๋ฅผ ์ ์ฉํด ์ค๋ณต์ฑ์ ์ ๊ฑฐํ๋ค.
โ 37ํ ์ ๋น์ต๋ ์ต์ ์์ ์ด์๋จ์ ๋ฐ์ค์ ์์น, ์ ๋ขฐ๋, ํด๋์ค ์ด๋ฆ์ ๋ชจ์ objects ๊ฐ์ฒด์ ์ ์ฅํ๋ค.
โ 38ํ์ objects๋ฅผ ๋ฐํํ๋ค.
โ 40ํ์ contruct yolo_v3 ํจ์๋ก YOLO ๋ชจ๋ธ์ ๊ตฌ์ฑํ๋ค.
โ 41 ํ์ ๋ฌผ์ฒด ํด๋์ค๋ฅผ ๊ณ ์ ํ ์์ผ๋ก ํ์ํ๊ธฐ ์ํด ์ปฌ๋ฌ ๋ชฉ๋ก์ ๋ง๋ค์ด colors ๊ฐ์ฒด์ ์ ์ฅํ๋ค.
1. YOLO v3๋ก ์ ์ง์์์์ Object Detectionํ๊ธฐ
๋ฉ์ธ ํ๋ก๊ทธ๋จ์ด ์์๋๋
โ 43ํ์ ์ ๋ ฅ ์์์ ์ฝ์ด img์ ์ ์ฅํ๋ค.
โ 46ํ์ yolo_detect ํจ์๋ก ์๋ณธ ์์ img์์ ๋ฌผ์ฒด๋ฅผ ๊ฒ์ถํด res์ ์ ์ฅํ๋ค.
โ 48~52ํ์ res์ ์๋ ๋ฐ์ค์ ํด๋์ค ์ด๋ฆ, ์ ๋ขฐ๋๋ฅผ ์์์ ํ์ํ๋ค.
ํ๋ก๊ทธ๋จ ์คํ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด, ์ผ์ชฝ๊ณผ ์ค๋ฅธ์ชฝ ์ ์๋ฅผ ๊ฐ๊ฐ 100%์ 94.1% ์ ๋ขฐ๋์ person ํด๋์ค๋ก ์ ๋๋ก ๊ฒ์ถํ๋ค.
๋ํ ์ถ๊ตฌ๊ณต์ 99.9% ์ ๋ขฐ๋๋ก sports ball ํด๋์ค๋ก ์ณ๊ฒ ๊ฒ์ถํ๋ค.
img=cv.imread('soccer.jpg') if img is None: sys.exit('ํ์ผ์ด ์์ต๋๋ค.') res=yolo_detect(img,model,out_layers) # YOLO ๋ชจ๋ธ๋ก ๋ฌผ์ฒด ๊ฒ์ถ for i in range(len(res)): # ๊ฒ์ถ๋ ๋ฌผ์ฒด๋ฅผ ์์์ ํ์ x1,y1,x2,y2,confidence,id=res[i] text=str(class_names[id])+'%.3f'%confidence cv.rectangle(img,(x1,y1),(x2,y2),colors[id],2) cv.putText(img,text,(x1,y1+30),cv.FONT_HERSHEY_PLAIN,1.5,colors[id],2) plt.imshow(img[...,::-1])
2. YOLO v3๋ก ๋น๋์ค์์ Object Detectionํ๊ธฐ (๋ฉํ ์บ ์ฌ์ฉ)
โ 43~64ํ์ ์น ์บ ์์ ๋น๋์ค๋ฅผ ์ฝ์ด ๋์คํ๋ ์ดํ๋ ์ฝ๋์ YOLO๋ฅผ ์ ์ฉํ๋ค.
โ 50ํ์ ๋น๋์ค์์ ํ๋ํ ํ์ฌ ํ๋ ์์ yolo_detect ํจ์์ ์ ๋ ฅํด ๋ฌผ์ฒด๋ฅผ ๊ฒ์ถํ๊ณ ๊ฒฐ๊ณผ๋ฅผ res์ ์ ์ฅํ๋ค.
โ 52~56ํ์ ๊ฒ์ถํ ๋ฌผ์ฒด ์ ๋ณด๋ฅผ ์์์ ํ์ํ๋ค.
cap=cv.VideoCapture(0) if not cap.isOpened(): sys.exit('์นด๋ฉ๋ผ ์ฐ๊ฒฐ ์คํจ') while True: ret,frame=cap.read() if not ret: sys.exit('ํ๋ ์ ํ๋์ ์คํจํ์ฌ ๋ฃจํ๋ฅผ ๋๊ฐ๋๋ค.') res=yolo_detect(frame,model,out_layers) for i in range(len(res)): x1,y1,x2,y2,confidence,id=res[i] text=str(class_names[id])+'%.3f'%confidence cv.rectangle(frame,(x1,y1),(x2,y2),colors[id],2) cv.putText(frame,text,(x1,y1+30),cv.FONT_HERSHEY_PLAIN,1.5,colors[id],2) cv.imshow("Object detection from video by YOLO v.3",frame) key=cv.waitKey(1) if key==ord('q'): break
'Gain Study > Vision' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Gain Study_CV]03. Advanced Vision (2) | 2024.01.08 |
---|---|
[Gain Study_CV]02-1. Object Detection (0) | 2023.09.18 |
[Gain Study_CV]01. Object Recognition(Classification, Semantic Segmentation) (0) | 2023.09.15 |