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)) # ํด๋์ค๋ง๋ค ์๊นโ
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