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

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

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

你可能感兴趣的:(yolov3,keras,python)