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

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

新規登録して質問してみよう
ただいま回答率
85.50%
OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

1861閲覧

python for文中での%runを実行するとエラーが出る。

sasakikik

総合スコア21

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2018/07/22 03:45

編集2018/07/22 03:52

前提・実現したいこと

https://github.com/DeNA/Chainer_Realtime_Multi-Person_Pose_Estimation/blob/master/README_JP.md
以上のURLで説明されているプログラムを使って、複数の画像データを読み込ましてポーズの推論を行おうとしています

発生している問題・エラーメッセージ

python

1for i in range (20): 2 %run pose_detector.py posenet models/coco_posenet.npz --img '/Users/Chainer_Realtime_Multi-Person_Pose_Estimation/data/'+i+'.png'

を実行すると以下のようなエラーが出ます。

python

1AttributeError Traceback (most recent call last) 2/Users/Chainer_Realtime_Multi-Person_Pose_Estimation/pose_detector.py in <module>() 3 572 4 573 # inference 5--> 574 poses, _ = pose_detector(img) 6 575 7 576 # draw and save image 8 9/Users/Chainer_Realtime_Multi-Person_Pose_Estimation/pose_detector.py in __call__(self, orig_img) 10 483 11 484 def __call__(self, orig_img): 12--> 485 orig_img = orig_img.copy() 13 486 if self.precise: 14 487 return self.detect_precise(orig_img) 15 16AttributeError: 'NoneType' object has no attribute 'copy'

試したこと

for文を使わずに直接入力すると、実行できました。

python

1%run pose_detector.py posenet models/coco_posenet.npz --img '/Users/Chainer_Realtime_Multi-Person_Pose_Estimation/data/0.png'

しかしながら変数を使うと、for文のときと同様のエラーが出ます。

python

1x = "/Users/onohikari//Chainer_Realtime_Multi-Person_Pose_Estimation/data/screen_caps/180.png" 2%run pose_detector.py posenet models/coco_posenet.npz --img x

python

1--------------------------------------------------------------------------- 2AttributeError Traceback (most recent call last) 3/Users/onohikari/Chainer_Realtime_Multi-Person_Pose_Estimation/pose_detector.py in <module>() 4 572 5 573 # inference 6--> 574 poses, _ = pose_detector(img) 7 575 8 576 # draw and save image 9 10/Users/onohikari/Chainer_Realtime_Multi-Person_Pose_Estimation/pose_detector.py in __call__(self, orig_img) 11 483 12 484 def __call__(self, orig_img): 13--> 485 orig_img = orig_img.copy() 14 486 if self.precise: 15 487 return self.detect_precise(orig_img) 16 17AttributeError: 'NoneType' object has no attribute 'copy' 18

pose_detector.pyには、このように書かれています。

python

1if __name__ == '__main__': 2 parser = argparse.ArgumentParser(description='Pose detector') 3 parser.add_argument('arch', choices=params['archs'].keys(), default='posenet', help='Model architecture') 4 parser.add_argument('weights', help='weights file path') 5 parser.add_argument('--img', '-i', default=None, help='image file path') 6 parser.add_argument('--gpu', '-g', type=int, default=-1, help='GPU ID (negative value indicates CPU)') 7 parser.add_argument('--precise', action='store_true', help='do precise inference') 8 args = parser.parse_args() 9 10 chainer.config.enable_backprop = False 11 chainer.config.train = False 12 13 # load model 14 pose_detector = PoseDetector(args.arch, args.weights, device=args.gpu, precise=args.precise) 15 16 # read image 17 img = cv2.imread(args.img) 18 19 # inference 20 poses, _ = pose_detector(img) 21 22 # draw and save image 23 img = draw_person_pose(img, poses) 24 print('Saving result into result.png...') 25 cv2.imwrite('result.png', img)

どうすれば、--imgの読み込みができるのでしょうか。
ご回答よろしくおねがいします。

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

os Mac
python 3.6 でjupyter notebookを使用しています。

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

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

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

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

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

guest

回答1

0

ベストアンサー

%runはあくまでもjupyter notebookのコマンドであってpythonのコードではありません。したがってコードの中に%runを書いてもダメです。

importを使えばいいでしょう(参考)。

投稿2018/07/22 04:20

KojiDoi

総合スコア13669

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

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

sasakikik

2018/07/22 04:59

インポートすればできました! ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問