如何在此问题中使用图像裁剪?

问题描述 投票:0回答:1

[我编写了一个代码,该代码读取文件夹中的图像,然后应用深度卷积神经网络推理来检测8个不同的对象。

网络输出与检测到的不同对象相关的边界框坐标:x,y,w(width)和h(height)

然后,代码创建检测到的类的文件夹,以保存图像及其检测到的边界框坐标。这是代码:

for f1 in files:
    # load our input image and grab its spatial dimensions
    image=cv2.imread(f1)

    # construct a blob from the input image and then perform a forward
    # pass of the YOLO object detector, giving us our bounding boxes and
    # associated probabilities
    blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416),
                                 swapRB=True, crop=False)
    net.setInput(blob)
    layerOutputs = net.forward(ln)

    # initialize our lists of detected bounding boxes, confidences, and
    # class IDs, respectively
    boxes = []
    confidences = []
    classIDs = []

    # loop over each of the layer outputs
    for output in layerOutputs:
         # loop over each of the detections
         for detection in output:
              # extract the class ID and confidence (i.e., probability) of
              # the current object detection
              scores = detection[5:]
              classID = np.argmax(scores)
              confidence = scores[classID]
              box = detection[0:4]
              (centerX, centerY, width, height) = box.astype("int")
              # get upper left corner

              x = int(centerX - (width / 2))
              y = int(centerY - (height / 2))



              boxes.append([x, y, int(width), int(height)])
              confidences.append(float(confidence))
              classIDs.append(classID)


              # ensure at least one detection exists
              if len(idxs) > 0:
                    # loop over the indexes we are keeping
                    for i in idxs.flatten():
                        # extract the bounding box coordinates
                        (x, y) = (boxes[i][0], boxes[i][1])
                        (w, h) = (boxes[i][2], boxes[i][3])
                        crop = image[y:y + h, x:x + w]



                        # draw a bounding box rectangle and label on the image
                        color = [int(c) for c in COLORS[classIDs[i]]]
                        cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)
                        text = "{}: {:.4f}".format(LABELS[classIDs[i]], confidences[i])
                        cv2.putText(image, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX,
                            0.5, color, 2)



                    # filter out weak predictions by ensuring the detected
                    # probability is greater than the minimum probability
                    if confidence > args["confidence"]:

                      # write output files
                      class_dir = os.path.join('/path to folder where we want to save cropped images/', LABELS[classID])
                      if not os.path.exists(class_dir):
                          os.makedirs(class_dir)
                      path = os.path.join(class_dir, f1.split('/')[-1][:-4])

                      cv2.imwrite(path + '.jpg', crop)
                      with open((path + '.txt'), 'a+') as f:
                          f.write(str(classID) + ' ' + str(box[0]) + ' ' + str(box[1]) + ' ' + str(box[2]) + ' ' + str(box[3]) + '\n')

我的目标是裁剪其中包含目标对象的图像的一部分,然后将其保存在创建的文件夹中。

我正在使用'crop = image [y:y + h,x:x + w]'裁剪其中具有目标对象的图像部分。但是,我收到以下错误:

  cv2.imwrite(path + '.jpg', crop)
cv2.error: OpenCV(4.2.0-dev) (-215:Assertion failed) !_img.empty() in function 'imwrite'

如何解决此错误?请随时编辑代码,这样我会更好地理解问题。

python image-processing deep-learning crop
1个回答
0
投票

通常,当读取错误路径中的图像或操作后变量不再是图像时,通常会收到此错误。很难找到直接的解决方案,但是我对理解错误有一些建议。

  • 在读取图像以检测到此之前,请使用此命令确保图像路径正确print(os.path.exists(f1))

  • [裁剪图像之前检查img变量的形状。

  • 裁剪图像后,检查裁剪变量的形状。

这些是识别可能的行的方法。这将使我们更容易发现错误。

© www.soinside.com 2019 - 2024. All rights reserved.