前提
YOLOv5とDenseDepthを使って物体認識と距離測定を同時に行いたいのですが、YOLOv5のバウンディングボックスの座標の値をDenseDepthに上手く渡せず難航しています。
実現したいこと
- バウンディングボックスの座標の値を渡したい
発生している問題・エラーメッセージ
12~15行目の値を71行目に渡したいのですが値が初期値のままになってしまいます
該当のソースコード
python
1detx1=0 2dety1=0 3detx2=340 4dety2=640 5---------------------------------------------------------- 6 # DenseTestを仮り呼び出し 7 8 if len(det): 9 # Rescale boxes from img_size to im0 size1q 10 det[:, :4] = scale_coords(im.shape[2:], det[:, :4], im0.shape).round() 11 12 detx1 = int(det[0][0].numpy()) 13 dety1 = int(det[0][1].numpy()) 14 detx2 = int(det[0][2].numpy()) 15 dety2 = int(det[0][3].numpy()) 16 17 print("if文の中の値",detx1,detx2,dety1,dety2) 18 19 z1 = (detx2-detx1)*(dety2-dety1) 20 21 22 23 # Print results 24 for c in det[:, -1].unique(): 25 n = (det[:, -1] == c).sum() # detections per class 26 s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string 27 28 # Write results 29 for *xyxy, conf, cls in reversed(det): 30 if save_txt: # Write to file 31 xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh 32 line = (cls, *xywh, conf) if save_conf else (cls, *xywh) # label format 33 with open(txt_path + '.txt', 'a') as f: 34 f.write(('%g ' * len(line)).rstrip() % line + '\n') 35 if detx1 < 320 and dety1 < 240 and detx2 > 320 and dety2 > 240 and z1 > 51200: 36 cv2.putText(im0, "STOP!!" , (130, 300), 0, 4.0, (0, 0, 255), 10) 37 if save_img or save_crop or view_img: # Add bbox to image 38 c = int(cls) # integer class 39 label = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}') 40 41 # xyxyが境界ボックスの座標 labelは認識した物体の名前 20220908 M.Ohuchi 42 annotator.box_label(xyxy, label, color=colors(c, True)) # 境界ボックスとラベル描画 43 if save_crop: 44 save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg', BGR=True) 45 # Print counter 46 n_1 = (det[:, -1] == 15).sum() #ラベルAの総数をカウント 47 a = f"{n_1} "#{'A'}{'s' * (n_1 > 1)}, " 48 cv2.putText(im0, "Block : " + str(a), (5, 30), 0, 1.0, (227, 240, 179), 2) 49 n_2 = (det[:, -1] == 16).sum() #ラベルBの総数をカウント 50 b = f"{n_2} "#{'A'}{'s' * (n_1 > 1)}, " 51 cv2.putText(im0, "Pole : " + str(b), (5, 60), 0, 1.0, (196, 240, 239), 2) 52 n_3 = (det[:, -1] == 17).sum() #ラベルCの総数をカウント 53 d = f"{n_3} "#{'A'}{'s' * (n_1 > 1)}, " 54 cv2.putText(im0, "Signboard : " + str(d), (5, 90), 0, 1.0, (240, 196, 240), 2) 55 cx1 = f"{detx1}" 56 cy1= f"{dety1}" 57 cv2.putText(im0, "Point1:X= " + str(cx1) + "Y=" + str(cy1),(5, 120), 0, 1.0, (255, 255, 255), 3) 58 cx2 = f"{detx2}" 59 cy2= f"{dety2}" 60 cv2.putText(im0, "Point2:X= " + str(cx2) + "Y=" + str(cy2),(5, 150), 0, 1.0, (255, 255, 255), 3) 61 z2= f"{z1}" 62 cv2.putText(im0, str(z2) ,(5, 180), 0, 1.0, (255, 255, 255), 3) 63 64------------------------------------------ 65def Predict(inputs): # DenseDepthの本体、距離を推定する処理 66 67 global modelDen 68 69 outputs = predict(modelDen, inputs) 70 outs_2d = np.squeeze(outputs) 71 outs_2d = outs_2d[detx1:detx2,dety1:dety2] 72 print("detの中",detx1,detx2,dety1,dety2) 73 outs_2d = outs_2d[::5,::5] 74 outs_1d = list(itertools.chain.from_iterable(outs_2d)) 75 kyori= [ int(n*1000) for n in outs_1d] 76 print("outputsの次元数=",outputs.shape) 77 print("outs_2dの次元数=",outs_2d.shape) 78 print("kyori.type=",type(kyori)) 79 print("outs_2d.type=",type(outs_2d)) 80 print("kyoriのピクセル数=",len(kyori)) 81 print("outs_1dのピクセル数=",len(outs_1d)) 82 print("距離=",min(kyori),"cm") 83 res = outputs[0] # outputsは0~1に正規化されている。恐らくこれを10倍すればメートルに変わる 84 res255 = np.array(res*255, dtype=np.uint8) 85 cv2.imshow('sindo2', res255) 86 cv2.namedWindow('sindo2', cv2.WINDOW_NORMAL)
試したこと
色々やってみましたが完全にお手上げです泣
補足情報(FW/ツールのバージョンなど)

回答1件
あなたの回答
tips
プレビュー