AttributeError:'tuple'对象没有属性'write',实例分割python
Posted
技术标签:
【中文标题】AttributeError:\'tuple\'对象没有属性\'write\',实例分割python【英文标题】: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'
如何纠正这个错误。
【问题讨论】:
【参考方案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))
【讨论】:
以上是关于AttributeError:'tuple'对象没有属性'write',实例分割python的主要内容,如果未能解决你的问题,请参考以下文章
AttributeError: 'tuple' 对象在使用 mysql-connector 插入数据时没有属性 'encode'
AttributeError:'tuple'对象没有属性'price'
如何修复“AttributeError at /orders/create/‘tuple’对象在 Django 中没有属性‘get’
AttributeError:“元组”对象没有属性“startswith”
readline() 方法给出错误:AttributeError: 'tuple' object has no attribute 'readline'