yolov3+keras学习中遇到的一些问题------(1)

本文记录了在使用Keras YOLOv3进行视频目标检测时遇到的错误,并详细解释了解决方案。错误发生在使用yolo_video.py处理视频的最后一帧时,由于迭代至视频结束后的None帧导致。通过添加if语句检查帧是否为None来避免这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

近期跑github的keras yolov3代码,遇到了一些问题,此博客以记录。

代码地址:https://github.com/qqwweee/keras-yolo3#usage

在使用yolo_video.py检测视频时,最后会报错,且无法保存视频。报错如下:

Traceback (most recent call last):
  File "yolo_video.py", line 75, in <module>
    detect_video(YOLO(**vars(FLAGS)), FLAGS.input, FLAGS.output)
  File "/disk/ZMH/project/keras-yolo3-master/yolo.py", line 205, in detect_video
    image = Image.fromarray(frame) # 报错了 迭代到最后一帧时报错
  File "/home/z/anaconda3/lib/python3.6/site-packages/PIL/Image.py", line 2421, in fromarray
    arr = obj.__array_interface__
AttributeError: 'NoneType' object has no attribute '__array_interface__'

Traceback (most recent call last):
  File "yolo_video.py", line 75, in <module>
    detect_video(YOLO(**vars(FLAGS)), FLAGS.input, FLAGS.output)
  File "/disk/ZMH/project/keras-yolo3-master/yolo.py", line 205, in detect_video
    image = Image.fromarray(frame) # 报错了 迭代到最后一帧时报错
  File "/home/z/anaconda3/lib/python3.6/site-packages/PIL/Image.py", line 2421, in fromarray
    arr = obj.__array_interface__
AttributeError: 'NoneType' object has no attribute '__array_interface__'

尝试寻找原因,发现是由于yolo.py文件中的 detec_video函数,在读取视频帧时使用的是 while 循环:

    while True: # 不能用while true 当视频读完后不会停止迭代
        return_value, frame = vid.read()
        image = Image.fromarray(frame) # 报错了 迭代到最后一帧时报错
        image = yolo.detect_image(image)
        result = np.asarray(image)
        curr_time = timer()
        exec_time = curr_time - prev_time
        prev_time = curr_time
        accum_time = accum_time + exec_time
        curr_fps = curr_fps + 1

使用while循环,会无限循环下去,而视频帧已全部读完时, frame为None,使用Image.fromarry(frame)就会报错。

解决:

添加if判断语句,当frame为None时,跳出循环。修改后代码如下:

while True: # 不能用while true 当视频读完后不会停止迭代
        # # 添加一个if判断语句 当迭代次数等于帧数时 跳出循环
        # if count == 618:
        #     break
        return_value, frame = vid.read()
        if frame is None:
             break
        print('第%d帧/总帧数%d' % (count, vid.get(cv2.CAP_PROP_FRAME_COUNT)))
        image = Image.fromarray(frame) # 报错了 迭代到最后一帧时报错
        image = yolo.detect_image(image)
        result = np.asarray(image)
        curr_time = timer()
        exec_time = curr_time - prev_time
        prev_time = curr_time
        accum_time = accum_time + exec_time
        curr_fps = curr_fps + 1

问题解决。

PS.在修改时还遇到一个问题,最开始加的判断是 当迭代次数等于视频帧数时跳出循环,但发现有些视频本身有问题,还未读到最后一帧时提前报错。

不能保存的问题也同样,测试一些视频时可以保存,而有一个怎样都保存不了。怀疑是视频编码方式的问题。

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值