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

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

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

Darknetは、C言語で記述されたオープンソースのニューラルネットフレームワークで簡単にインストールすることが可能です。学習済みモデルとアルゴリズムも配布しており、ダウンロードすれば容易に動かすこともできます。

YOLO

YOLOとは、画像検出および認識用ニューラルネットワークです。CベースのDarknetというフレームワークを用いて、画像や動画からオブジェクトを検出。リアルタイムでそれが何になるのかを認識し、分類することができます。

Python 3.x

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

Q&A

0回答

2005閲覧

Jetson Nanoで物体検出を行うが、Segmentation faultが発生し実行できない

kokota

総合スコア0

Darknet

Darknetは、C言語で記述されたオープンソースのニューラルネットフレームワークで簡単にインストールすることが可能です。学習済みモデルとアルゴリズムも配布しており、ダウンロードすれば容易に動かすこともできます。

YOLO

YOLOとは、画像検出および認識用ニューラルネットワークです。CベースのDarknetというフレームワークを用いて、画像や動画からオブジェクトを検出。リアルタイムでそれが何になるのかを認識し、分類することができます。

Python 3.x

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

0グッド

0クリップ

投稿2020/10/11 01:11

前提・実現したいこと

Jetson NanoにWebカメラを接続して物体検出を行いたいです。
物体検出を行うためにOpenCVとdarknetを使用しています。
darknetは以下を使用しています。
git clone https://github.com/AlexeyAB/darknet
上記のgitの『darknet_video.py』を使用して物体検出を行おうと
しています。

発生している問題・エラーメッセージ

darknet_video.pyを実行すると、Segmentation fault(コアダンプ)が出力され実行が中止します。

該当のソースコード

Pythonで記述してあります。ビデオを保存できなかったため、もとのソースコードからはout.mp4に出力できるように変更しています。また、yolov3-tinyを使用しています。

from ctypes import * import random import os import cv2 import time import darknet import argparse from threading import Thread, enumerate from queue import Queue def parser(): parser = argparse.ArgumentParser(description="YOLO Object Detection") parser.add_argument("--input", type=str, default=0, help="video source. If empty, uses webcam 0 stream") parser.add_argument("--out_filename", type=str, default="out.mp4", help="inference video name. Not saved if empty") parser.add_argument("--weights", default="yolov3-tiny.weights", help="yolo weights path") parser.add_argument("--dont_show", action='store_true', help="windown inference display. For headless systems") parser.add_argument("--ext_output", action='store_true', help="display bbox coordinates of detected objects") parser.add_argument("--config_file", default="./cfg/yolov3-tiny.cfg", help="path to config file") parser.add_argument("--data_file", default="./cfg/coco.data", help="path to data file") parser.add_argument("--thresh", type=float, default=.25, help="remove detections with confidence below this value") return parser.parse_args() def str2int(video_path): """ argparse returns and string althout webcam uses int (0, 1 ...) Cast to int if needed """ try: return int(video_path) except ValueError: return video_path def check_arguments_errors(args): assert 0 < args.thresh < 1, "Threshold should be a float between zero and one (non-inclusive)" if not os.path.exists(args.config_file): raise(ValueError("Invalid config path {}".format(os.path.abspath(args.config_file)))) if not os.path.exists(args.weights): raise(ValueError("Invalid weight path {}".format(os.path.abspath(args.weights)))) if not os.path.exists(args.data_file): raise(ValueError("Invalid data file path {}".format(os.path.abspath(args.data_file)))) if str2int(args.input) == str and not os.path.exists(args.input): raise(ValueError("Invalid video path {}".format(os.path.abspath(args.input)))) def set_saved_video(input_video, output_video, size): fourcc = cv2.VideoWriter_fourcc('m','p','4', 'v') fps = int(input_video.get(cv2.CAP_PROP_FPS)) video = cv2.VideoWriter(output_video, fourcc, fps, size) return video def video_capture(frame_queue, darknet_image_queue): while cap.isOpened(): ret, frame = cap.read() if not ret: break frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize(frame_rgb, (width, height), interpolation=cv2.INTER_LINEAR) frame_queue.put(frame_resized) darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes()) darknet_image_queue.put(darknet_image) cap.release() def inference(darknet_image_queue, detections_queue, fps_queue): while cap.isOpened(): darknet_image = darknet_image_queue.get() prev_time = time.time() detections = darknet.detect_image(network, class_names, darknet_image, thresh=args.thresh) detections_queue.put(detections) fps = int(1/(time.time() - prev_time)) fps_queue.put(fps) print("FPS: {}".format(fps)) darknet.print_detections(detections, args.ext_output) darknet.free_image(darknet_image) cap.release() def drawing(frame_queue, detections_queue, fps_queue): random.seed(3) # deterministic bbox colors video = set_saved_video(cap, args.out_filename, (width, height)) while cap.isOpened(): frame_resized = frame_queue.get() detections = detections_queue.get() fps = fps_queue.get() if frame_resized is not None: image = darknet.draw_boxes(detections, frame_resized, class_colors) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if args.out_filename is not None: video.write(image) if not args.dont_show: cv2.imshow('Inference', image) if cv2.waitKey(fps) == 27: break cap.release() video.release() cv2.destroyAllWindows() if __name__ == '__main__': frame_queue = Queue() darknet_image_queue = Queue(maxsize=1) detections_queue = Queue(maxsize=1) fps_queue = Queue(maxsize=1) args = parser() check_arguments_errors(args) network, class_names, class_colors = darknet.load_network( args.config_file, args.data_file, args.weights, batch_size=1 ) # Darknet doesn't accept numpy images. # Create one with image we reuse for each detect width = darknet.network_width(network) height = darknet.network_height(network) darknet_image = darknet.make_image(width, height, 3) input_path = str2int(args.input) cap = cv2.VideoCapture(input_path) Thread(target=video_capture, args=(frame_queue, darknet_image_queue)).start() Thread(target=inference, args=(darknet_image_queue, detections_queue, fps_queue)).start() Thread(target=drawing, args=(frame_queue, detections_queue, fps_queue)).start()

試したこと

drawing関数を以下のように変更してみました。
変更箇所はwhile cap.isOpened():内を2回のみ実行するようにしたことと、
fps = fps_queue.get()をコメントアウトしました(fps_queue.get()をするとsegmentation_faultが発生していたため)。
上記のように変更すると、1秒間の動画ファイルを出力し再生できました。
しかしながら、3回以上の実行に変更すると動画ファイルを出力するものの、
再生することができませんでした。

def drawing(frame_queue, detections_queue, fps_queue): random.seed(3) # deterministic bbox colors video = set_saved_video(cap, args.out_filename, (width, height)) #while cap.isOpened(): i = 0 while i <= 1: i = i+1 frame_resized = frame_queue.get() detections = detections_queue.get() # fps = fps_queue.get() if frame_resized is not None: image = darknet.draw_boxes(detections, frame_resized, class_colors) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if args.out_filename is not None: video.write(image) # if not args.dont_show: # cv2.imshow('Inference', image) # if cv2.waitKey(fps) == 27: # break cap.release() video.release() cv2.destroyAllWindows()

補足情報(FW/ツールのバージョンなど)

Python ver3.6.9
OpenCV ver4.4.0

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問