AttributeError: 'tuple' object has no attribute 'write' , instance segmentation python

Question:

I have used code of this blog "https://learnopencv.com/deep-learning-based-object-detection-and-instance-segmentation-using-mask-r-cnn-in-opencv-python-c/"
Titled Deep learning based Object Detection and Instance Segmentation using Mask R-CNN in OpenCV in python . I am using live stream and want to do object detection and instance segmentation on that and modified the code below rest is same as explained in the blog

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()

I am getting below error while running this

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'

How to correct this error .

Asked By: user15234292

||

Answers:

In this line your are creating a tupple

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

instead do simply

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

Result is a tuple of length 2 whereas it should be a simple type you can change line 38 to :

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

The value python round(cap.get(cv.CAP_PROP_FRAME_HEIGHT)) does not seem to do anything so you can remove by it replacing lines 12-14 by:

result = cv.VideoWriter('sample.avi',  
                         cv.VideoWriter_fourcc(*'MJPG'), 
                         22, size)
Answered By: toto le harico