質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

746閲覧

pythonで関数間の変数共有

reiya_123

総合スコア57

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2022/08/11 01:52

前提

python3.8
ubuntu20.04をしようしています

実現したいこと

yolo関数に変数color_imageとdepth_imageを渡したいと考えています。
ですが、下記のコードではcolor_imageやdepth_imageが定義されていないためエラーがでてしまいます。
そこで、クラスの外にcolor_image = None,depth_image = Noneと書くと
'NoneType' object has no attribute 'shape'となってしまいます。

この場合、どのように渡せばよいのでしょうか。
申し訳ございませんがご教示お願いいたします。

該当のソースコード

class Yolo: def __init__(self): rospy.init_node('yolo', anonymous=True) self.bridge = CvBridge() sub_rgb = message_filters.Subscriber("/camera/color/image_raw",Image) sub_depth = message_filters.Subscriber("/camera/depth/image_rect_raw",Image) self.mf = message_filters.ApproximateTimeSynchronizer([sub_rgb, sub_depth], 100, 0.5) self.mf.registerCallback(self.ImageCallback) def ImageCallback(self, rgb_data , depth_data): global color_image,depth_image try: cv_color_image = self.bridge.imgmsg_to_cv2(rgb_data, 'rgb8') color_image = np.array(cv_color_image) cv_depth_image = self.bridge.imgmsg_to_cv2(depth_data, '32FC1') depth_image = np.array(cv_depth_image) print(depth_image[int((640+0)/2)][int((480+0)/2)]) except CvBridgeError as e: rospy.logerr(e) def yolo(self,color_image,depth_image): try: while not rospy.is_shutdown(): img, orig_im, dim = self.prep_image(color_image, inp_dim) im_dim = torch.FloatTensor(dim).repeat(1,2) if CUDA: im_dim = im_dim.cuda() img = img.cuda() output = model(Variable(img), CUDA) output = write_results(output, confidence, num_classes, nms = True, nms_conf = nms_thesh) output[:,1:5] = torch.clamp(output[:,1:5], 0.0, float(inp_dim))/inp_dim im_dim = im_dim.repeat(output.size(0), 1) output[:,[1,3]] *= self.color_image.shape[1] output[:,[2,4]] *= self.color_image.shape[0] list(map(lambda x: self.write(x, orig_im,depth_image,color_image), output)) if __name__ == '__main__': try: Y = Yolo() Y.yolo(color_image,depth_image) rospy.spin() except rospy.ROSInterruptException: pass

試したこと

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

ただpublisherを起動していないだけでした。
そうすれば、'NoneType' object has no attribute 'shape'このエラーも消えて実装できました。

投稿2022/08/11 03:17

reiya_123

総合スコア57

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問