使用Python进行视频/图像处理:错误:操作系统错误:无法识别图像文件'solidWhiteRight.mp4'

时间:2016-12-17 03:36:14

标签: python numpy computer-vision

我一直在处理静止图像而没有错误。

当我尝试运行以下代码进行视频处理时,我收到错误:

    OS Error: cannot identify image file 'solidWhiteRight.mp4'

然后我无法继续。

任何人都能找出导致错误的原因吗?

完整的错误代码:

OSError: cannot identify image file 'solidWhiteRight.mp4'

OSError                                   Traceback (most recent call last)
<ipython-input-85-8871a0549a09> in <module>()
      1 white_output = 'white.mp4'
      2 clip1 = VideoFileClip('solidWhiteRight.mp4')
----> 3 white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!!
      4 get_ipython().magic('time white_clip.write_videofile(white_output, audio=False)')

/home/michael/anaconda3/lib/python3.5/site-packages/moviepy/video/VideoClip.py in fl_image(self, image_func, apply_to)
    512         `get_frame(t)` by another frame,  `image_func(get_frame(t))`
    513         """
--> 514         return self.fl(lambda gf, t: image_func(gf(t)), apply_to)
    515 
    516     # --------------------------------------------------------------

/home/michael/anaconda3/lib/python3.5/site-packages/moviepy/Clip.py in fl(self, fun, apply_to, keep_duration)
    134 
    135         #mf = copy(self.make_frame)
--> 136         newclip = self.set_make_frame(lambda t: fun(self.get_frame, t))
    137 
    138         if not keep_duration:

<decorator-gen-178> in set_make_frame(self, mf)

/home/michael/anaconda3/lib/python3.5/site-packages/moviepy/decorators.py in outplace(f, clip, *a, **k)
     12     """ Applies f(clip.copy(), *a, **k) and returns clip.copy()"""
     13     newclip = clip.copy()
---> 14     f(newclip, *a, **k)
     15     return newclip
     16 

/home/michael/anaconda3/lib/python3.5/site-packages/moviepy/video/VideoClip.py in set_make_frame(self, mf)
    652         """
    653         self.make_frame = mf
--> 654         self.size = self.get_frame(0).shape[:2][::-1]
    655 
    656 

<decorator-gen-135> in get_frame(self, t)

/home/michael/anaconda3/lib/python3.5/site-packages/moviepy/decorators.py in wrapper(f, *a, **kw)
     87         new_kw = {k: fun(v) if k in varnames else v
     88                  for (k,v) in kw.items()}
---> 89         return f(*new_a, **new_kw)
     90     return decorator.decorator(wrapper)
     91 

/home/michael/anaconda3/lib/python3.5/site-packages/moviepy/Clip.py in get_frame(self, t)
     93                 return frame
     94         else:
---> 95             return self.make_frame(t)
     96 
     97     def fl(self, fun, apply_to=[] , keep_duration=True):

/home/michael/anaconda3/lib/python3.5/site-packages/moviepy/Clip.py in <lambda>(t)
    134 
    135         #mf = copy(self.make_frame)
--> 136         newclip = self.set_make_frame(lambda t: fun(self.get_frame, t))
    137 
    138         if not keep_duration:

/home/michael/anaconda3/lib/python3.5/site-packages/moviepy/video/VideoClip.py in <lambda>(gf, t)
    512         `get_frame(t)` by another frame,  `image_func(get_frame(t))`
    513         """
--> 514         return self.fl(lambda gf, t: image_func(gf(t)), apply_to)
    515 
    516     # --------------------------------------------------------------

<ipython-input-84-5748bb68776c> in process_image(image)
      6     #Read in image
      7     #Grayscale the image
----> 8     image = mpimg.imread('solidWhiteRight.mp4')
      9     gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
     10 

/home/michael/anaconda3/lib/python3.5/site-packages/matplotlib/image.py in imread(fname, format)
   1304 
   1305     if ext not in handlers:
-> 1306         im = pilread(fname)
   1307         if im is None:
   1308             raise ValueError('Only know how to handle extensions: %s; '

/home/michael/anaconda3/lib/python3.5/site-packages/matplotlib/image.py in pilread(fname)
   1282         except ImportError:
   1283             return None
-> 1284         image = Image.open(fname)
   1285         return pil_to_array(image)
   1286 

/home/michael/anaconda3/lib/python3.5/site-packages/PIL/Image.py in open(fp, mode)
   2315 
   2316     raise IOError("cannot identify image file %r"
-> 2317                   % (filename if filename else fp))
   2318 
   2319 #

OSError: cannot identify image file 'solidWhiteRight.mp4'

视频处理代码:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import cv2
%matplotlib inline
import os
os.listdir()
from moviepy.editor import VideoFileClip

def process_image(image):
    # The output you return should be a color image (3 channel) for processing video below
    # TODO: put your pipeline here,
    # you should return the final output (image with lines are drawn on lanes)

    #Read in image
    #Grayscale the image
    image = mpimg.imread(solidWhiteRight.mp4)
    gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)

    #Define a kernel size and apply Gaussian smoothing (blurring)
    kernel_size = 5
    blur_gray = cv2.GaussianBlur(gray,(kernel_size,kernel_size),0)

    #Define the parameters for the Canny edge detection algorithm and apply
    low_threshold = 50
    high_threshold = 250
    edges = cv2.Canny(blur_gray, low_threshold, high_threshold)

    #Create a "masked edges" image using cv2.fillPoly()
    mask = np.zeros_like(edges)
    ignore_mask_color = 255

    #Define a four-side polygon to mask the image
    imshape = image.shape
    vertices = np.array([[(0, imshape[0]),(450,290),(490, 290),(imshape[1],imshape[0])]], dtype=np.int32)
    cv2.fillPoly(mask, vertices, ignore_mask_color)
    masked_edges = cv2.bitwise_and(edges,mask)

    #Define the Hough transform parameters
    #Make a blank the same size as our image to draw on
    rho = 2 #Distance resolution in pixels of the Hough grid
    theta = np.pi/180 #Angular resolution in radians of the Hough grid
    threshold = 15 #Minimum number of votes (intersections in Hough grid cell)
    min_line_length = 40 #Minimum number of pixels making up a line
    max_line_gap = 20 #Maximum gap in pixels between connectable line segments
    line_image = np.copy(image)*0 #Creating a blank to draw lines on

    #Run Hough on edge detected image
    #Output "lines" is an array containing endpoints of detected line segments
    lines = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.array([]),
                            min_line_length, max_line_gap)

    #Iterate over the output "lines' and draw lines on a blank image
    for line in lines:
        for x1,y1,x2,y2 in line:
            cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),10)

    #Create a "color" binary image to combine with the line image
    color_edges = np.dstack((edges, edges, edges))    

    #Draw the lines on the edge image
    lines_edges = cv2.addWeighted(color_edges, 0.8, line_image, 1, 0)

    return line_edges


#Solid white first
white_output = 'white.mp4'
clip1 = VideoFileClip("solidWhiteRight.mp4")
white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!!
%time white_clip.write_videofile(white_output, audio=False)

#Play video inline
HTML("""
<video width="960" height="540" controls>
  <source src="{0}">
</video>
""".format(white_output))

错误:操作系统错误:无法识别图像文件'solidWhiteRight.mp4'

1 个答案:

答案 0 :(得分:0)

答案是:

第8行: image = mpimg.imread(solidWhiteRight.mp4)

正在尝试处理静止图像文件。如果你评论出来, 代码运行正常。