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

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

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

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

Python 3.x

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

GitHub

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

Q&A

解決済

1回答

2064閲覧

TelloPyのvideo_effectを実行するとエラーが起きてしまいます

Tello-python

総合スコア6

OpenCV

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

Python 3.x

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

GitHub

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

0グッド

0クリップ

投稿2019/10/21 05:07

編集2019/10/21 05:19

Telloから物体認識を行いたくてTelloPyのvideo_effectを実行すると

python

1Traceback (most recent call last): 2 File "C:\Users\one\Anaconda3\envs\py3.5\lib\site-packages\tellopy\examples\video_effect.py", line 106, in main 3 image = getSSDImage(imgpil) 4 File "C:\Users\one\Anaconda3\envs\py3.5\lib\site-packages\tellopy\examples\video_effect.py", line 67, in getSSDImage 5 score = top_conf[i] 6UnboundLocalError: local variable 'i' referenced before assignment 7local variable 'i' referenced before assignment

python

1Traceback (most recent call last): 2 File "C:\Users\one\Anaconda3\envs\py3.5\lib\site-packages\tensorflow\python\client\session.py", line 582, in __del__ 3UnboundLocalError: local variable 'status' referenced before assignment

python

1Traceback (most recent call last): 2 File "C:\Users\one\Anaconda3\envs\py3.5\lib\site-packages\tensorflow\python\client\session.py", line 582, in __del__ 3AttributeError: 'NoneType' object has no attribute 'TF_DeleteStatus'

というエラーが出てしまいます。
解決方法がわかる方、よろしくお願いします。

#ソースコード
video_effect.py(下記)のコードはTelloドローンでプログラミング!ーディープラーニングで物体認識編ーから元のコードから変更しています。

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 numpy 19plt.rcParams['figure.figsize'] = (8, 8) 20plt.rcParams['image.interpolation'] = 'nearest' 21np.set_printoptions(suppress=True) 22config = tf.ConfigProto() 23config.gpu_options.per_process_gpu_memory_fraction = 0.45 24set_session(tf.Session(config=config)) 25voc_classes = ['Aeroplane', 'Bicycle', 'Bird', 'Boat', 'Bottle', 26'Bus', 'Car', 'Cat', 'Chair', 'Cow', 'Diningtable', 27'Dog', 'Horse','Motorbike', 'Person', 'Pottedplant', 28'Sheep', 'Sofa', 'Train', 'Tvmonitor'] 29NUM_CLASSES = len(voc_classes) + 1 30input_shape=(100, 100, 3) 31model = SSD300v2(input_shape, num_classes=NUM_CLASSES) 32model.load_weights('weights_SSD300.hdf5', by_name=True) 33bbox_util = BBoxUtility(NUM_CLASSES) 34def getSSDImage(frame): 35 #global i 36 img2 = image.img_to_array(frame.resize((100, 100))) 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 #print(top_conf.shape[0]) 60 for i in range(top_conf.shape[0]): 61 xmin = int(round(top_xmin[i] * img.shape[1])) 62 ymin = int(round(top_ymin[i] * img.shape[0])) 63 xmax = int(round(top_xmax[i] * img.shape[1])) 64 ymax = int(round(top_ymax[i] * img.shape[0])) 65 #print(i) 66 67 score = top_conf[i] 68 label = int(top_label_indices[i]) 69 label_name = voc_classes[label - 1] 70 display_txt = '{:0.2f}, {}'.format(score, label_name) 71 color = colors[label] 72 cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (int(colors[label][0]*255), int(colors[label][1]*255), int(colors[label][2]*255)), 2) 73 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) 74 cv2.putText(img, display_txt, (xmin, ymin), cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1, cv2.LINE_AA) 75 76 print(display_txt) 77 78 imgcv = img[:, :, ::-1].copy() 79 return imgcv 80def main(): 81 drone = tellopy.Tello() 82 try: 83 drone.connect() 84 drone.wait_for_connection(60.0) 85 drone.set_loglevel(drone.LOG_INFO) 86 drone.set_exposure(0) 87 #container = av.open(drone.get_video_stream()) 88 89 retry=3 90 container=None 91 while container is None and 0 < retry: 92 retry -= 1 93 try: 94 container = av.open(drone.get_video_stream()) 95 except av.AVError as ave: 96 print(ave) 97 print('retry...') 98 99 frame_skip=300 100 frame_count = 0 101 while True: 102 for frame in container.decode(video=0): 103 frame_count = frame_count + 1 104 if (frame_count > 300) and (frame_count%50 == 0): 105 imgpil = frame.to_image() 106 image = getSSDImage(imgpil) 107 108 cv2.imshow('Original', image) 109 cv2.waitKey(1) 110 except Exception as ex: 111 exc_type, exc_value, exc_traceback = sys.exc_info() 112 traceback.print_exception(exc_type, exc_value, exc_traceback) 113 print(ex) 114 finally: 115 drone.quit() 116 cv2.destroyAllWindows() 117if __name__ == '__main__': 118 main()

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

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

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

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

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

guest

回答1

0

自己解決

インデントの問題でした。

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 numpy 19plt.rcParams['figure.figsize'] = (8, 8) 20plt.rcParams['image.interpolation'] = 'nearest' 21np.set_printoptions(suppress=True) 22config = tf.ConfigProto() 23config.gpu_options.per_process_gpu_memory_fraction = 0.45 24set_session(tf.Session(config=config)) 25voc_classes = ['Aeroplane', 'Bicycle', 'Bird', 'Boat', 'Bottle', 26'Bus', 'Car', 'Cat', 'Chair', 'Cow', 'Diningtable', 27'Dog', 'Horse','Motorbike', 'Person', 'Pottedplant', 28'Sheep', 'Sofa', 'Train', 'Tvmonitor'] 29NUM_CLASSES = len(voc_classes) + 1 30input_shape=(100, 100, 3) 31model = SSD300v2(input_shape, num_classes=NUM_CLASSES) 32model.load_weights('weights_SSD300.hdf5', by_name=True) 33bbox_util = BBoxUtility(NUM_CLASSES) 34global label_name 35def getSSDImage(frame): 36 img2 = image.img_to_array(frame.resize((100, 100))) 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 83 retry=3 84 container=None 85 while container is None and 0 < retry: 86 retry -= 1 87 try: 88 container = av.open(drone.get_video_stream()) 89 except av.AVError as ave: 90 print(ave) 91 print('retry...') 92 93 frame_skip=300 94 frame_count = 0 95 while True: 96 for frame in container.decode(video=0): 97 frame_count = frame_count + 1 98 if (frame_count > 300) and (frame_count%50 == 0): 99 imgpil = frame.to_image() 100 image = getSSDImage(imgpil) 101 102 cv2.imshow('Original', image) 103 cv2.waitKey(1) 104 105 except Exception as ex: 106 exc_type, exc_value, exc_traceback = sys.exc_info() 107 traceback.print_exception(exc_type, exc_value, exc_traceback) 108 print(ex) 109 finally: 110 drone.quit() 111 cv2.destroyAllWindows() 112if __name__ == '__main__': 113 main()

投稿2019/10/21 07:57

Tello-python

総合スコア6

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問