Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Canny edge detection applying to video, but converted video is not saving

Hi,

The purpose of this code is to find a RAW video, run a canny edge detection conversion on it, and then save the converted video as output.avi to the designated target directory. For the most part, it runs fine - it finds the video, runs the canny edge detection just fine, and outputs to the correct directory.

The issue is every time the code outputs just a 5.5kb file (which I think is just the avi header). The strange thing is that if out.write(edges) is replaced with out.write(frame), the video outputs correctly, having converted from the RAW file to an avi. This leads me to believe there is some issue with the edges output, but I have no idea what it is, as both frame and edges have the exact same type.

Note: using Python 2.7.3 and OpenCV 3.0

import cv2, os, glob, re
import numpy as np
target_dir = "/home/pi/raw_dump"

fps = 30
width = 320
height = 240

os.chdir(target_dir)
print "Detecting valid files for conversion..."

for file in glob.glob("*RAW.h264"):
    print("Converting file: "+file)

    #Find the name of the file
    output_filename = re.sub("-RAW\.h264","", file)

    #Define the codec and create VideoWriter object
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter('output.avi',fourcc,fps,(width,height))

    #create capture stream object on which to modify
    cap_stream = cv2.VideoCapture(file)
    while(cap_stream.isOpened()):
        # Take each frame
        ret, frame = cap_stream.read()

        if ret == True:
            #flip the frame
            frame = cv2.flip(frame,0)

            # modify the arguments for cv2.Canny to change thresh values
            edges = cv2.Canny(frame,100,200)

            #print modified frame
            cv2.imshow('edges',edges)

            #print original frame
            #cv2.imshow('frame',frame)

            out.write(edges)

            # if waitkey() returns anything other than -1, and error may have occurred. abort
            if cv2.waitKey(10) >= 0:
                break
        else:
            print "Finishing conversion..."
            break

    # release and clean up streams
    print "Created output_filename.avi"

    cap_stream.release()
    out.release()

If anyone has any idea of how to fix this issue, please let me know.

Thanks for your time.