前提・実現したいこと
メラモジュールで撮影した画像を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/ツールのバージョンなど)
撮影する時は、
> 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]
でエラーになる
ということだと思うので、撮影した時に画像ファイルを保存する場所と、その後で画像ファイルを読み出す時の場所を同じにする必要があります

あなたの回答
tips
プレビュー