#やりたいこと
トイドローンであるTelloから物体認識をしたいです。
TelloPyとSnowMasaya/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ドローンでプログラミング!ーディープラーニングで物体認識編ー
回答1件
あなたの回答
tips
プレビュー