【问题标题】:AttributeError: 'tuple' object has no attribute 'write' , instance segmentation pythonAttributeError:'tuple'对象没有属性'write',实例分割python
【发布时间】:2021-05-25 18:12:19
【问题描述】:

我使用了这个博客的代码“https://learnopencv.com/deep-learning-based-object-detection-and-instance-segmentation-using-mask-r-cnn-in-opencv-python-c /" 标题为 Python 中 OpenCV 中使用 Mask R-CNN 进行基于深度学习的对象检测和实例分割。我正在使用实时流,并希望对其进行对象检测和实例分割,并修改了下面的代码,其余部分与博客中的说明相同

input_path = 'rtsp://...'
cap = cv.VideoCapture(input_path)
print(cap.isOpened())

# We need to set resolutions. 
# so, convert them from float to integer. 
frame_width = int(cap.get(3)) 
frame_height = int(cap.get(4)) 
size = (frame_width, frame_height) 

#cv2.VideoWriter( filename, fourcc, fps, frameSize )
result = cv.VideoWriter('sample.avi',  
                         cv.VideoWriter_fourcc(*'MJPG'), 
                         22, size) ,round(cap.get(cv.CAP_PROP_FRAME_HEIGHT))


while True:
    ret, frame = cap.read()
    if ret:
        
        # You can do processing on this frame variable
        blob = cv.dnn.blobFromImage(frame, swapRB=True, crop=False)

        # Set the input to the network
        net.setInput(blob)

        # Run the forward pass to get output from the output layers
        boxes, masks = net.forward(['detection_out_final', 'detection_masks'])

        # Extract the bounding box and mask for each of the detected objects
        postprocess(boxes, masks)

        # Put efficiency information.
        t, _ = net.getPerfProfile()
        label = 'Mask-RCNN : Inference time: %.2f ms' % (t * 1000.0 / cv.getTickFrequency())
        cv.putText(frame, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))

        result.write(frame.astype(np.uint8))

        cv.imshow("winName", frame)
        
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

result.release()
cap.release()
cv.destroyAllWindows()

我在运行时遇到错误

AttributeError                            Traceback (most recent call last)
<ipython-input-10-9712242a2634> in <module>
     36         cv.putText(frame, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
     37 
---> 38         result.write(frame)
     39 
     40         cv.imshow("winName", frame)

AttributeError: 'tuple' object has no attribute 'write'

如何纠正这个错误。

【问题讨论】:

    标签: python opencv cv2 semantic-segmentation


    【解决方案1】:

    Result 是一个长度为 2 的元组,而它应该是一个简单的类型,您可以将第 38 行更改为:

    result[0].write(frame.astype(np.uint8))
    

    python round(cap.get(cv.CAP_PROP_FRAME_HEIGHT)) 的值似乎没有任何作用,因此您可以将第 12-14 行替换为:

    result = cv.VideoWriter('sample.avi',  
                             cv.VideoWriter_fourcc(*'MJPG'), 
                             22, size)
    

    【讨论】:

      【解决方案2】:

      在这一行中,您正在创建一个元组

      result = cv.VideoWriter('sample.avi', cv.VideoWriter_fourcc(*'MJPG'), 22, size), round(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
      

      而是简单地做

      result = cv.VideoWriter('sample.avi', cv.VideoWriter_fourcc(*'MJPG'), 22, size)
      variableX = round(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-18
        • 1970-01-01
        • 2023-01-26
        • 2017-07-06
        • 1970-01-01
        • 1970-01-01
        • 2020-05-18
        相关资源
        最近更新 更多