python
1python -m tellopy.examples.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 92, in main 3 image = getSSDImage(imgpil) 4 File "C:\Users\one\Anaconda3\envs\py3.5\lib\site-packages\tellopy\examples\video_effect.py", line 65, in getSSDImage 5 score = top_conf[i] 6NameError: name 'i' is not defined 7name 'i' is not defined
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 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 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 90 cv2.imshow('Original', image) 91 cv2.waitKey(1) 92 except Exception as ex: 93 exc_type, exc_value, exc_traceback = sys.exc_info() 94 traceback.print_exception(exc_type, exc_value, exc_traceback) 95 print(ex) 96 finally: 97 drone.quit() 98 cv2.destroyAllWindows() 99if __name__ == '__main__': 100 main()
エラーで書かれている
python
1score = top_conf[i]
というコードより上に変数「i」が使用されているのにそこではエラーが出ません。
なぜなのでしょう?
またどのように解決するのでしょう
#環境
TelloPy:0.6.0
python:3.5
Keras:2.0.1
opencv-python:3.1.0.0
ssd:1.0.0
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。