【问题标题】:numpy.ndarray object has no attribute 'read'(and 'seek')numpy.ndarray 对象没有属性'read'(和'seek')
【发布时间】:2020-04-28 23:36:21
【问题描述】:

我收到错误 numpy.ndarray object has no attribute 'read'numpy.ndarray object has no attribute 'seek'。我尝试在网上寻找答案,但失败了。

我想要做的是检测视频中的对象 - 在这种情况下,我想检测斑马。

我使用了一个图像检测器,并尝试将其应用于视频。我正在尝试遍历视频的每一帧并最终将帧传递给函数draw_boxes

这是错误信息:

Traceback (most recent call last):
  File "/Users/ysquared/Library/Python/3.7/lib/python/site-packages/PIL/Image.py", line 2770, in open
    fp.seek(0)
AttributeError: 'numpy.ndarray' object has no attribute 'seek'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 204, in <module>
  File "<string>", line 118, in load_image_pixels
  File "/Users/ysquared/Library/Python/3.7/lib/python/site-packages/keras_preprocessing/image/utils.py", line 110, in load_img
    img = pil_image.open(path)
  File "/Users/ysquared/Library/Python/3.7/lib/python/site-packages/PIL/Image.py", line 2772, in open
    fp = io.BytesIO(fp.read())
AttributeError: 'numpy.ndarray' object has no attribute 'read'

这里是相关代码:

model = load_model('model.h5')

# define the expected input shape for the model
input_w, input_h = 416, 416

# define the anchors
anchors = [[116,90, 156,198, 373,326], [30,61, 62,45, 59,119], [10,13, 16,30, 33,23]]

# define the labels
labels = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck",
        "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench",
        "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe",
        "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard",
        "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
        "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana",
        "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake",
        "chair", "sofa", "pottedplant", "bed", "diningtable", "toilet", "tvmonitor", "laptop", "mouse",
        "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator",
        "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush"]

vs = cv2.VideoCapture('Zebras.mp4')
fourcc = cv2.VideoWriter_fourcc(*'XVID')
writer = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))

class_threshold = 0.6
boxes = list()

while True:
    (grabbed, frame) = vs.read()

    if grabbed==True:

        image, image_w, image_h = load_image_pixels(frame, (input_w, input_h))
        yhat = model.predict(image)

        for i in range(len(yhat)):
            # decode the output of the network
            boxes += decode_netout(yhat[i][0], anchors[i], class_threshhold, input_h, input_w)
         # correct the sizes of the bounding boxes for the shape of the image
        correct_yolo_boxes(boxes, image_h, image_w, input_h, input_w)
         # suppress non-maximal boxes
        do_nms(boxes, 0.5)

         # get the details of the detected objects
        v_boxes, v_labels, v_scores = get_boxes(boxes, labels, class_threshold)

         # draw what we found
        frame = draw_boxes(frame, v_boxes, v_labels, v_scores)

        writer.write(frame)

        cv2.imshow('frame', frame)

        if cv2.waitkey(1) & 0xFF == ord('q'):
            break

    else:
        break

vs.release()

writer.release()

cv2.destroyAllWindows()

【问题讨论】:

    标签: python numpy deep-learning object-detection


    【解决方案1】:

    这是我解决问题的方法(即消除了错误):

    
    ##[..] 
    cv2.imwrite("framex.jpg", frame)
    filename = "framex.jpg"
    
    image, image_w, image_h = load_image_pixels(filename, (input_w, input_h))
    
    ##[..]
    
    frame = draw_boxes(filename, v_boxes, v_labels, v_scores)
    
    ##[..]
    

    【讨论】:

      【解决方案2】:

      seekread 是打开的文件可以做的事情。我从回溯中推断出这个错误发生在

      image, image_w, image_h = load_image_pixels(frame, (input_w, input_h))
      

      行,问题必须在frame 参数中。该函数需要一个打开的文件或文件名,但 frame 显然是 numpy 数组。它在上面定义:

      (grabbed, frame) = vs.read()
      

      所以它是来自mp4 的框架,是一个数组,而不是一个文件。

      所以要么你需要给load_image_pixels一个文件名,要么你需要使用其他函数来处理frame数组。

      一般的网络搜索对此类错误没有帮助。它既过于宽泛又过于具体。

      no attribute Python 中的错误很常见。这意味着此时的对象具有与程序员预期的不同的类。例如,如果cv 读取找不到文件,它会返回None,而不是图像。您不能像处理图像一样处理None。但是您使用数组而不是文件名的特定组合是独一无二的。

      在进行网络搜索之前,请尝试了解错误发生的位置以及涉及的变量。检查它的实际性质(类型、形状等)和代码的预期、被调用的函数。

      【讨论】:

      • 如何将框架(如您所说的 numpy 数组)转换为文件,例如 jpg 文件?所以我想弄清楚的是如何将帧(一个 numpy 数组)转换为 jpg 文件,以便 load_image_pixels 使用正确的类型。我已经尝试过从 keras.preprocessing.image 导入的Image.fromarrayarray_to_img。如果你能帮助我,我将不胜感激。
      • 如果frame已经是数组,为什么还要使用load_image_pixels?为什么将其保存为文件只是为了再次将其加载到数组或keras 结构中?您是否正在学习针对不同类型输入的教程?
      • load_image_pixels 还与图像一起返回图像的高度和宽度。图像的高度和宽度由另一个函数消耗。所以这就是为什么我认为我需要使用load_image_pixels
      • 不能从数组中推导出高和宽吗?
      • 函数 draw_boxes 使用文件名而不是 numpy 数组。不像load_image_pixels 我不能修改这个。要检测我需要draw_boxes 的对象。所以我需要将numpy数组转换为jpg文件。
      猜你喜欢
      • 2023-03-19
      • 2021-05-24
      • 2021-11-24
      • 1970-01-01
      • 2020-09-22
      • 2020-02-21
      • 2020-12-08
      • 2019-12-10
      • 2017-07-10
      相关资源
      最近更新 更多