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

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

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

Anacondaは、Python本体とPythonで利用されるライブラリを一括でインストールできるパッケージです。環境構築が容易になるため、Python開発者間ではよく利用されており、商用目的としても利用できます。

OpenCV

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

Python 3.x

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

GitHub

GitHubは、Gitバージョン管理システムを利用したソフトウェア開発向けの共有ウェブサービスです。GitHub商用プランおよびオープンソースプロジェクト向けの無料アカウントを提供しています。

Q&A

解決済

1回答

2493閲覧

cv2でAttributeErrorが...

Tello-python

総合スコア6

Anaconda

Anacondaは、Python本体とPythonで利用されるライブラリを一括でインストールできるパッケージです。環境構築が容易になるため、Python開発者間ではよく利用されており、商用目的としても利用できます。

OpenCV

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

Python 3.x

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

GitHub

GitHubは、Gitバージョン管理システムを利用したソフトウェア開発向けの共有ウェブサービスです。GitHub商用プランおよびオープンソースプロジェクト向けの無料アカウントを提供しています。

0グッド

0クリップ

投稿2019/10/16 02:13

編集2019/10/16 04:12

#やりたいこと
トイドローンであるTelloから物体認識をしたいです。
TelloPySnowMasaya/ssd_kerasを使用しています。
足りない点があれば随時追記します!よろしくお願いします!

#いざ実行!
python -m tellopy.examples.video_effect
と実行すると下のエラーが出ます

python

1Traceback (most recent call last): 2 File "C:\Users\one\Anaconda3\envs\py3.5\lib\runpy.py", line 193, in _run_module_as_main 3 "__main__", mod_spec) 4 File "C:\Users\one\Anaconda3\envs\py3.5\lib\runpy.py", line 85, in _run_code 5 exec(code, run_globals) 6 File "C:\Users\one\Anaconda3\envs\py3.5\lib\site-packages\tellopy\examples\video_effect.py", line 18, in <module> 7 import cv2.cv2 as cv2 8AttributeError: module 'cv2.cv2' has no attribute 'cv2'

#実行しているvideo_effect.pyのソース

python

1import cv2 2import keras 3from keras.applications.imagenet_utils import preprocess_input 4from keras.backend.tensorflow_backend import set_session 5from keras.models import Model 6from keras.preprocessing import image 7import matplotlib.pyplot as plt 8import numpy as np 9from scipy.misc import imread 10import tensorflow as tf 11from ssd_v2 import SSD300v2 12from ssd_utils import BBoxUtility 13from PIL import Image 14import sys 15import traceback 16import tellopy 17import av 18import cv2.cv2 as cv2 19import numpy 20plt.rcParams['figure.figsize'] = (8, 8) 21plt.rcParams['image.interpolation'] = 'nearest' 22np.set_printoptions(suppress=True) 23config = tf.ConfigProto() 24config.gpu_options.per_process_gpu_memory_fraction = 0.45 25set_session(tf.Session(config=config)) 26voc_classes = ['Aeroplane', 'Bicycle', 'Bird', 'Boat', 'Bottle', 27'Bus', 'Car', 'Cat', 'Chair', 'Cow', 'Diningtable', 28'Dog', 'Horse','Motorbike', 'Person', 'Pottedplant', 29'Sheep', 'Sofa', 'Train', 'Tvmonitor'] 30NUM_CLASSES = len(voc_classes) + 1 31input_shape=(300, 300, 3) 32model = SSD300v2(input_shape, num_classes=NUM_CLASSES) 33model.load_weights('weights_SSD300.hdf5', by_name=True) 34bbox_util = BBoxUtility(NUM_CLASSES) 35def getSSDImage(frame): 36 img2 = image.img_to_array(frame.resize((300, 300))) 37 img = np.asarray(frame) 38 inputs = [] 39 inputs.append(img2.copy()) 40 inputs = preprocess_input(np.array(inputs)) 41 preds = model.predict(inputs, batch_size=1, verbose=1) 42 results = bbox_util.detection_out(preds) 43 # Parse the outputs. 44 det_label = results[0][:, 0] 45 det_conf = results[0][:, 1] 46 det_xmin = results[0][:, 2] 47 det_ymin = results[0][:, 3] 48 det_xmax = results[0][:, 4] 49 det_ymax = results[0][:, 5] 50 # Get detections with confidence higher than 0.6. 51 top_indices = [i for i, conf in enumerate(det_conf) if conf >= 0.6] 52 top_conf = det_conf[top_indices] 53 top_label_indices = det_label[top_indices].tolist() 54 top_xmin = det_xmin[top_indices] 55 top_ymin = det_ymin[top_indices] 56 top_xmax = det_xmax[top_indices] 57 top_ymax = det_ymax[top_indices] 58 colors = plt.cm.hsv(np.linspace(0, 1, 21)).tolist() 59 for i in range(top_conf.shape[0]): 60 xmin = int(round(top_xmin[i] * img.shape[1])) 61 ymin = int(round(top_ymin[i] * img.shape[0])) 62 xmax = int(round(top_xmax[i] * img.shape[1])) 63 ymax = int(round(top_ymax[i] * img.shape[0])) 64 score = top_conf[i] 65 label = int(top_label_indices[i]) 66 label_name = voc_classes[label - 1] 67 display_txt = '{:0.2f}, {}'.format(score, label_name) 68 color = colors[label] 69 cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (int(colors[label][0]*255), int(colors[label][1]*255), int(colors[label][2]*255)), 2) 70 cv2.rectangle(img, (xmin, ymin-15), (xmin+100, ymin+5), (int(colors[label][0]*255), int(colors[label][1]*255), int(colors[label][2]*255)),-1) 71 cv2.putText(img, display_txt, (xmin, ymin), cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1, cv2.LINE_AA) 72 imgcv = img[:, :, ::-1].copy() 73 return imgcv 74def main(): 75 drone = tellopy.Tello() 76 try: 77 drone.connect() 78 drone.wait_for_connection(60.0) 79 drone.set_loglevel(drone.LOG_INFO) 80 drone.set_exposure(0) 81 container = av.open(drone.get_video_stream()) 82 frame_count = 0 83 while True: 84 for frame in container.decode(video=0): 85 frame_count = frame_count + 1 86 if (frame_count > 300) and (frame_count%50 == 0): 87 imgpil = frame.to_image() 88 image = getSSDImage(imgpil) 89 cv2.imshow('Original', image) 90 cv2.waitKey(1) 91 except Exception as ex: 92 exc_type, exc_value, exc_traceback = sys.exc_info() 93 traceback.print_exception(exc_type, exc_value, exc_traceback) 94 print(ex) 95 finally: 96 drone.quit() 97 cv2.destroyAllWindows() 98if __name__ == '__main__': 99 main()

#環境
Python 3.5.5
Keras 2.0.1
opencv-python 3.1.0.0
ssd 1.0.0
tellopy 0.6.0
tensorflow 1.0.0

#参考サイト
SSD: Single Shot MultiBox Detector 高速リアルタイム物体検出デモをKerasで試す
Telloドローンでプログラミング!ーディープラーニングで物体認識編ー

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

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

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

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

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

tiitoi

2019/10/16 04:17

import cv2.cv2 as cv2 は単に import cv2 でいいのではないでしょうか
Tello-python

2019/10/16 05:20

エラー消えて実行することができました! ありがとうございます!
guest

回答1

0

自己解決

tiitoiさんのご指摘通り

python

1import cv2

に変更したらエラーが消えて実行することが出来ました。

投稿2019/10/16 05:23

Tello-python

総合スコア6

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問