AttributeError:' NoneType'对象没有属性'形状'的OpenCV

时间:2017-04-01 10:30:06

标签: python-2.7 opencv3.0 attributeerror non-type

我试图让以下工作正常但仍然收到NoneType错误。

# import the necessary packages
from __future__ import print_function
from imutils.video import VideoStream
import numpy as np
import argparse
import imutils
import time
import cv2

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-o", "--output", required=True,
    help="path to output video file")
ap.add_argument("-p", "--picamera", type=int, default=-1,
    help="whether or not the Raspberry Pi camera should be used")
ap.add_argument("-f", "--fps", type=int, default=20,
    help="FPS of output video")
ap.add_argument("-c", "--codec", type=str, default="MJPG",
    help="codec of output video")
args = vars(ap.parse_args())

# initialize the video stream and allow the camera
# sensor to warmup
print("[INFO] warming up camera...")
vs = VideoStream(usePiCamera=args["picamera"] > 0).start()
time.sleep(2.0)

# initialize the FourCC, video writer, dimensions of the frame, and
# zeros array
fourcc = cv2.VideoWriter_fourcc(*args["codec"])
writer = None
(h, w) = (None, None)
zeros = None
# loop over frames from the video stream
while True:
    # grab the frame from the video stream and resize it to have a
    # maximum width of 300 pixels
    frame = vs.read()
    frame = imutils.resize(frame, width=300)

    # check if the writer is None
if writer is None:
    # store the image dimensions, initialzie the video writer,
    # and construct the zeros array
    (h, w) = frame.shape[:2]
    writer = cv2.VideoWriter(args["output"], fourcc, args["fps"],
    (w * 2, h * 2), True)
    zeros = np.zeros((h, w), dtype="uint8")
    # break the image into its RGB components, then construct the
    # RGB representation of each frame individually
    (B, G, R) = cv2.split(frame)
    R = cv2.merge([zeros, zeros, R])
    G = cv2.merge([zeros, G, zeros])
    B = cv2.merge([B, zeros, zeros])

# construct the final output frame, storing the original frame
# at the top-left, the red channel in the top-right, the green
# channel in the bottom-right, and the blue channel in the
# bottom-left
    output = np.zeros((h * 2, w * 2, 3), dtype="uint8")
    output[0:h, 0:w] = frame
    output[0:h, w:w * 2] = R
    output[h:h * 2, w:w * 2] = G
    output[h:h * 2, 0:w] = B

# write the output frame to file
    writer.write(output)
# show the frames
    cv2.imshow("Frame", frame)
    cv2.imshow("Output", output)
    key = cv2.waitKey(1) & 0xFF

# if the `q` key was pressed, break from the loop
if key == ord("q"):
    sys.exit()

# do a bit of cleanup
    print("[INFO] cleaning up...")
    cv2.destroyAllWindows()
    vs.stop()
    writer.release()

构建信息:

UI: 
    QT:                          NO
    GTK+ 2.x:                    YES (ver 2.24.25)
    GThread :                    YES (ver 2.42.1)
    GtkGlExt:                    NO
    OpenGL support:              NO
    VTK support:                 NO

  Media I/O: 
    ZLib:                        /usr/lib/arm-linux-gnueabihf/libz.so (ver 1.2.
    JPEG:                        libjpeg (ver 90)
    WEBP:                        build (ver 0.3.1)
    PNG:                         /usr/lib/arm-linux-gnueabihf/libpng.so (ver 1.2.50)
    TIFF:                        build (ver 42 - 4.0.2)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 1.7.1)
    GDAL:                        NO
    GDCM:                        NO

  Video I/O:
    DC1394 1.x:                  NO
    DC1394 2.x:                  NO
    FFMPEG:                      YES
      avcodec:                   YES (ver 56.1.0)
      avformat:                  YES (ver 56.1.0)
      avutil:                    YES (ver 54.3.0)
      swscale:                   YES (ver 3.0.0)
      avresample:                YES (ver 2.1.0)
    GStreamer:                   NO
    OpenNI:                      NO
    OpenNI PrimeSensor Modules:  NO
    OpenNI2:                     NO
    PvAPI:                       NO
    GigEVisionSDK:               NO
    Aravis SDK:                  NO
    UniCap:                      NO
    UniCap ucil:                 NO
    V4L/V4L2:                    NO/YES
    XIMEA:                       NO
    Xine:                        NO
    gPhoto2:                     NO

  Parallel framework:            pthreads

  Other third-party libraries:
    Use IPP:                     NO
    Use VA:                      NO
    Use Intel VA-API/OpenCL:     NO
    Use Lapack:                  NO
    Use Eigen:                   NO
    Use Cuda:                    NO
    Use OpenCL:                  YES
    Use OpenVX:                  NO
    Use custom HAL:              YES (carotene (ver 0.0.1))

  OpenCL:                        <Dynamic loading of OpenCL library>
    Include path:                /home/pi/opencv-3.2.0/3rdparty/include/opencl/1.2
    Use AMDFFT:                  NO
    Use AMDBLAS:                 NO

  Python 2:
    Interpreter:                 /home/pi/.virtualenvs/cv/bin/python2.7 (ver 2.7.9)
    Libraries:                   /usr/lib/arm-linux-gnueabihf/libpython2.7.so (ver 2.7.9)
    numpy:                       /home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/numpy/core/include (ver 1.12.1)
    packages path:               lib/python2.7/site-packages

  Python 3:
    Interpreter:                 /usr/bin/python3.4 (ver 3.4.2)
    Libraries:                   /usr/lib/arm-linux-gnueabihf/libpython3.4m.so (ver 3.4.2)
    numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.8.2)
    packages path:               lib/python3.4/site-packages

  Python (for build):            /home/pi/.virtualenvs/cv/bin/python2.7

  Java:
    ant:                         NO
    JNI:                         NO
    Java wrappers:               NO
    Java tests:                  NO

  Matlab:                        Matlab not found or implicitly disabled

  Documentation:
    Doxygen:                     NO

  Tests and samples:
    Tests:                       YES
    Performance tests:           YES
    C/C++ Examples:              YES

  Install path:                  /usr/local

  cvconfig.h is in:              /home/pi/opencv-3.2.0/build

返回的错误是:

python writing_video.py --output example.avi
[INFO] warming up camera...
Traceback (most recent call last):
  File "writing_video.py", line 38, in <module>
    frame = imutils.resize(frame, width=300)
  File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/imutils/convenience.py", line 69, in resize
    (h, w) = image.shape[:2]
AttributeError: 'NoneType' object has no attribute 'shape

^我已经进入了convenience.py,看看我是否能发现我正在制作的任何错误,但它似乎与我的代码对齐

我理解Nonetype错误意味着形状没有价值,因此无法调用它。我已经被困在这个错误上一个多星期了,现在看不到答案。我刚刚重新映像我的pi并为python2.7重新安装了opencv-3.2.0。

我一直在研究NoneType解决方案文章(http://www.pyimagesearch.com/2016/12/26/opencv-resolving-nonetype-errors/)从此我得出结论相机功能齐全,我可以拍摄raspstill并录制视频。

但我无法通过omxplayer播放视频。当播放细节并度过美好的一天;)显示但没有打开新窗口。这让我觉得它是编解码器或驱动程序的问题。此外,Adrian还发表评论说opencv很可能在没有视频I / O支持的情况下编译。经过数小时的搜索和疑难解答后,我仍然不确定如何解决此错误。

任何帮助将不胜感激!感谢

1 个答案:

答案 0 :(得分:0)

这是因为视频读取了最后一帧,并且后面没有任何内容,所以会有“ NoneType”。

解决方案:添加if语句。

if ret == True:
    (h, w) = frame.shape [:2]
else:
    break