簡體   English   中英

AttributeError: 'tuple' object 沒有屬性 'write',實例分段 python

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

我使用了這個博客的代碼“https://learnopencv.com/deep-learning-based-object-detection-and-instance-segmentation-using-mask-r-cnn-in-opencv-python-c/”標題基於深度學習的 Object 檢測和實例分割在 python 中的 OpenCV 中使用 Mask R-CNN。 我正在使用實時 stream 並想要對其進行 object 檢測和實例分割並修改 rest 下面的代碼與博客中解釋的相同

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'

如何糾正這個錯誤。

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)

在這一行中,您正在創建一個元組

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM