【问题标题】:AttributeError: 'NoneType' object has no attribute 'tostring'AttributeError:“NoneType”对象没有属性“tostring”
【发布时间】:2017-06-09 19:49:50
【问题描述】:

所以我几天前刚开始编程,我遇到了这个问题:AttributeError: 'NoneType' object has no attribute 'tostring'。不知道怎么回事。

#Reads through each frame, calculates the timestamp, places it on the frame and
#exports the frame to the output video.
while current_frame < total_frames:
    success, image = video.read()
    elapsed_time = video.get(cv2.CAP_PROP_POS_MSEC)
    current_frame = video.get(cv2.CAP_PROP_POS_FRAMES)
    timestamp = initial + dt.timedelta(microseconds = elapsed_time*1000)
    cv2.putText(image, 'Date: ' + str(timestamp)[0:10], (50,int(height-150)), cv2.FONT_HERSHEY_COMPLEX_SMALL, 2, (255, 255, 255), 3)
    cv2.putText(image, 'Time: ' + str(timestamp)[11:-4], (50,int(height-100)), cv2.FONT_HERSHEY_COMPLEX_SMALL, 2, (255, 255, 255), 3)
    pipe.stdin.write(image.tostring())

【问题讨论】:

    标签: python-2.7 opencv ffmpeg


    【解决方案1】:

    可能是某些帧没有正确返回,因此image 变量为空,因此为NoneType,即它没有类型。

    使用image.shape 检查图像的形状。

    您还可以使用type(variable) 检查 Python 中任何变量的类型。

    if image.shape[0]==0 or image.shape[1]==0: continue #or do something else to handle this

    【讨论】:

    • 这看起来像是评论,而不是问题的答案。
    猜你喜欢
    • 2019-01-01
    • 2021-12-26
    • 2019-07-23
    • 2018-05-13
    • 2020-09-07
    • 2017-05-03
    • 2023-03-16
    • 2018-07-14
    • 2013-06-16
    相关资源
    最近更新 更多