OpenCV VideoCapture of an "address" fails

Viewed 17

I have calibrated a fisheye camera and have acquired the the camera calibration matrix, I was able to get a perfectly undistored image. But I am getting this error when I try to output the result of a live video feed.

Traceback (most recent call last):
  File "calibration.py", line 30, in <module>
    h, w = frame.shape[:2]
AttributeError: 'NoneType' object has no attribute 'shape'

This was my code:

import cv2
import numpy as np
import os
import glob
import sys

#Establishing a Video Capture: 

cap = cv2.VideoCapture('address')

# Properties
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
fps = cap.get(cv2.CAP_PROP_FPS)

#Video Writer

fourcc = cv2.VideoWriter_fourcc(*'XVID')

output = cv2.VideoWriter('output.mp4', fourcc, fps, (frame_width, frame_height))

while True:
    success, frame = cap.read()
    DIM =(1280, 720)
    K = np.array([[378.68613029205295, 0.0, 631.5070934616149], [0.0, 379.1372862000342, 353.49063700941866], [0.0, 0.0, 1.0]])
    D = np.array([[-0.044342379001557394], [0.07719525629446446], [-0.07364775294823307], [0.023329834056879786]])



    h, w = frame.shape[:2]
    map1, map2 = cv2.fisheye.initUndistortRectifyMap(K, D, np.eye(3), K, DIM, cv2.CV_16SC2)
    new_frame = cv2.remap(frame, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)

    output.write(new_frame)
    if cv2.waitKey(20) == ord('q'):
        break
    
0 Answers
Related