👒

matlab+yoloで人を認識してブロックゲームを作る

課題3

detector=yolov3ObjectDetector('tiny-yolov3-coco'); cap=webcam; ObjectPosition = [randi([0,100],1),randi([0,100],1),50,50]; while true ObjectPosition2 = [randi([0,300],1),randi([0,300],1),50]; image=cap.snapshot(); image=preprocess(detector,image); image=im2single(image); [bboxes,scores,labels]=detect(detector,image,'DetectionPreprocessing','none'); detectedImg=insertObjectAnnotation(image,'Rectangle',bboxes,labels); detectedImg = insertShape(detectedImg,'Rectangle',ObjectPosition,'LineWidth',5); detectedImg = insertShape(detectedImg,'Circle',ObjectPosition2,'LineWidth',5); imshow(detectedImg); for i = 1: size(labels) if(labels(i) == 'person') fprintf("person!!\n"); end end ObjectPosition = ObjectPosition + [0,10,0,0]; if(ObjectPosition(1,2) > 400) ObjectPosition = ObjectPosition - [0,400,0,0]; end end

課題4

detector=yolov3ObjectDetector('tiny-yolov3-coco'); cap=webcam; ObjectPosition = [randi([0,100],1),randi([0,100],1),50,50]; while true image=cap.snapshot(); image=preprocess(detector,image); image=im2single(image); [bboxes,scores,labels]=detect(detector,image,'DetectionPreprocessing','none'); detectedImg=insertObjectAnnotation(image,'Rectangle',bboxes,labels); detectedImg = insertShape(detectedImg,'Rectangle',ObjectPosition,'LineWidth',5); imshow(detectedImg); for i = 1: size(labels) if(labels(i) == 'person') PersonX_min = bboxes(i,1); PersonY_min = bboxes(i,2); PersonX_max = bboxes(i,1) + bboxes(i,3); PersonY_max = bboxes(i,2) + bboxes(i,4); ObjX_min = ObjectPosition(1,1); ObjY_min = ObjectPosition(1,2); ObjX_max = ObjX_min + ObjectPosition(1,3); ObjY_max = ObjY_min + ObjectPosition(1,4); fmt = ['The vector P is: [', repmat('%g, ', 1, numel(ObjectPosition)-1), '%g]\t']; fprintf(fmt,ObjectPosition); % fprintf("[%d\t%d\t%d\t%d]\t",ObjX_min,ObjX_max,ObjY_min,ObjY_max); x_in = false; y_in = false; stateA_X = (ObjX_min < PersonX_min && PersonX_min < ObjX_max); stateB_X = (ObjX_min < PersonX_max && PersonX_max < ObjX_max); stateC_X = (PersonX_min < ObjX_min && ObjX_min < PersonX_max && PersonX_min < ObjX_max && ObjX_max < PersonX_max); stateA_Y = (ObjY_min < PersonY_min && PersonY_min < ObjY_max); stateB_Y = (ObjY_min < PersonY_max && PersonY_max < ObjY_max); stateC_Y = (PersonY_min < ObjY_min && ObjY_min < PersonY_max && PersonY_min < ObjY_max && ObjY_max < PersonY_max); if(stateA_X || stateB_X || stateC_X) fprintf("X IN \t"); x_in = true; else fprintf("X OUT \t"); x_in = false; end if(stateA_Y || stateB_Y || stateC_Y) fprintf("Y IN \t"); y_in = true; else fprintf("Y OUT \t"); y_in = false; end if(x_in && y_in) fprintf("Game Over!!\n"); else fprintf("noting \n"); end end end ObjectPosition = ObjectPosition + [0,10,0,0]; if(ObjectPosition(1,2) > 400) ObjectPosition = ObjectPosition - [0,400,0,0]; end end

物体接触の判定について

4パターンに分けることができる。
A または B またはCの時に物体接触と言える。
以下の画像はXについての画像であるが、Yについても同じことを言える。
notion image
 
notion image