前提・実現したいこと
Python, OpenCVを用いてキャプチャした画像に対して、tensorflowで学習したモデルを使って物体認識を行い、
動画のFPSをなるべく落とさずに、処理結果を画像に表示しながら、動画として再生させたいです。
ただし、どのように処理を工夫すればいいか分かりません。バッファリングや並列処理等、ナレッジや処理のベストプラクティスを教えていただけないでしょうか?
発生している問題・エラーメッセージ
動画を再生しているのですが、かなり遅いです。
該当のソースコード
python
1import cv2 2 3cap = cv2.VideoCapture("sample.mp4") 4 5frame_count = int(cap.get(7)) 6frame_rate = int(cap.get(5)) 7 8for i in range(frame_count): 9 is_read, frame = cap.read() 10 image_np = frame 11 #############↓処理に時間がかかる場所↓############### 12 with detection_graph.as_default(): 13 with tf.Session(graph=detection_graph) as sess: 14 image_np_expanded = np.expand_dims(image_np, axis=0) 15 image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') 16 # Each box represents a part of the image where a particular object was detected. 17 boxes = detection_graph.get_tensor_by_name('detection_boxes:0') 18 # Each score represent how level of confidence for each of the objects. 19 # Score is shown on the result image, together with the class label. 20 scores = detection_graph.get_tensor_by_name('detection_scores:0') 21 classes = detection_graph.get_tensor_by_name('detection_classes:0') 22 num_detections = detection_graph.get_tensor_by_name('num_detections:0') 23 # Actual detection. 24 (boxes, scores, classes, num_detections) = sess.run( 25 [boxes, scores, classes, num_detections], 26 feed_dict={image_tensor: image_np_expanded}) 27 # Visualization of the results of a detection. 28 vis_util.visualize_boxes_and_labels_on_image_array( 29 image_np, 30 np.squeeze(boxes), 31 np.squeeze(classes).astype(np.int32), 32 np.squeeze(scores), 33 category_index, 34 use_normalized_coordinates=True, 35 line_thickness=8) 36 #############↑処理に時間がかかる場所↑############### 37 cv2.imshow("player", image_np) 38 # 動画再生中に動画を選択してescを押したら終了する 39 if cv2.waitKey(1) == 27: 40 break 41cap.release() 42cv2.destroyAllWindows()
試したこと
上記の該当のソースコードの
「#############処理に時間がかかる場所###############」
の範囲を消すと、なめらかに動画自体は再生されている事は確認しています。
補足情報(FW/ツールのバージョンなど)
Python 3.5.5
Opencv 3.4.1
動画ファイルのframe_rateは30fps、画像サイズは512×512です。
物体認識の学習モデルには下記のネットワークを使っております。
https://github.com/tensorflow/models/blob/master/research/object_detection/samples/configs/ssd_mobilenet_v2_coco.config
スペックは下記です
MacBook Pro (Retina, 13-inch, Early 2015)
2.7 GHz Intel Core i5
8GB 1867 MHz DDR3
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。