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

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

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

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

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

0回答

702閲覧

カメラモジュールで撮影した画像をYOLOで画像解析

empty_colors031

総合スコア1

YOLO

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

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/07/06 06:08

前提・実現したいこと

メラモジュールで撮影した画像をYOLOで画像解析したいとおもっています

ここに質問の内容を詳しく書いてください。
python でYOLOをしています
実装中に以下のエラーメッセージが発生しました。

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

エラーメッセージ ```Traceback(most recent call last): File "/home/pi/Desktop/ca.py",line36, in <module> h, w = image.shape[:2] AttributeError: 'NoneType' object has no attribute 'shape' ### 該当のソースコード ```import time import picamera from datetime import datetime with picamera.PiCamera() as camera: camera.resolution = (1024, 768) camera.start_preview() time.sleep(2) timestr = datetime.now().strftime('%Y%m%d%H%M%S') camera.capture(timestr+'.jpg') import numpy as np import time import cv2 import os DARKNET_PATH = 'yolo' # Read labels that are used on object labels = open(os.path.join(DARKNET_PATH, "data", "coco.names")).read().splitlines() # Make random colors with a seed, such that they are the same next time np.random.seed(0) colors = np.random.randint(0, 255, size=(len(labels), 3)).tolist() # Give the configuration and weight files for the model and load the network. net = cv2.dnn.readNetFromDarknet(os.path.join(DARKNET_PATH, "cfg", "yolov3-tiny.cfg"), "yolov3-tiny.weights") # Determine the output layer, now this piece is not intuitive ln = net.getLayerNames() ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()] # Load the image image = cv2.imread(os.path.join(DARKNET_PATH, "Desktop", "timestr+'.jpg")) # Get the shape h, w = image.shape[:2] # Load it as a blob and feed it to the network blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416), swapRB=True, crop=False) net.setInput(blob) start = time.time() # Get the output layer_outputs = net.forward(ln) end = time.time() # Initialize the lists we need to interpret the results boxes = [] confidences = [] class_ids = [] # Loop over the layers for output in layer_outputs: # For the layer loop over all detections for detection in output: # The detection first 4 entries contains the object position and size scores = detection[5:] # Then it has detection scores - it takes the one with maximal score class_id = np.argmax(scores).item() # The maximal score is the confidence confidence = scores[class_id].item() # Ensure we have some reasonable confidence, else ignorre if confidence > 0.3: # The first four entries have the location and size (center, size) # It needs to be scaled up as the result is given in relative size (0.0 to 1.0) box = detection[0:4] * np.array([w, h, w, h]) center_x, center_y, width, height = box.astype(int).tolist() # Calculate the upper corner x = center_x - width//2 y = center_y - height//2 # Add our findings to the lists boxes.append([x, y, width, height]) confidences.append(confidence) class_ids.append(class_id) # Only keep the best boxes of the overlapping ones idxs = cv2.dnn.NMSBoxes(boxes, confidences, 0.3, 0.3) # Ensure at least one detection exists - needed otherwise flatten will fail if len(idxs) > 0: # Loop over the indexes we are keeping for i in idxs.flatten(): # Get the box information x, y, w, h = boxes[i] # Make a rectangle cv2.rectangle(image, (x, y), (x + w, y + h), colors[class_ids[i]], 2) # Make and add text text = "{}: {:.4f}".format(labels[class_ids[i]], confidences[i]) cv2.putText(image, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, colors[class_ids[i]], 2) # Write the image with boxes and text cv2.imwrite("timestr+'.png", image)

試したこと

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

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

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

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

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

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

jbpb0

2021/07/06 09:34

撮影する時は、 > camera.capture(timestr+'.jpg') と、ファイル名だけ指定してるので、画像ファイルはカレントディレクトリに保存されてるはず 一方、その後で画像ファイルを読み出す時は、 > image = cv2.imread(os.path.join(DARKNET_PATH, "Desktop", "timestr+'.jpg")) と、ファイル名の前に「DARKNET_PATH = 'yolo'」と「"Desktop"」を付けてるので、カレントディレクトリの画像ファイルではなく、カレントディレクトリにある「yolo」というディレクトリの、さらにその中にある「Desktop」というディレクトリの中にある画像ファイルを読み出そうとしている そんなところにそのファイル名の画像ファイルは無いので、画像のデータが読めずに「image」の中身は空っぽだから、 > h, w = image.shape[:2] でエラーになる ということだと思うので、撮影した時に画像ファイルを保存する場所と、その後で画像ファイルを読み出す時の場所を同じにする必要があります
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問