moviepy音视频剪辑:headblur函数遇到的ValueError assignment destination is read-only问题及解决办法

☞ ░ 前往老猿Python博文目录

一、运行环境

运行环境如下:
python版本:3.7
opencv-python版本:4.2.0.34
numpy版本:1.19.0

二、错误案例代码及报错信息

使用如下代码调用headblur进行人脸跟踪打马赛克:

if __name__ == '__main__':
    movie_in = sys.argv[1]
    if len(sys.argv) == 3: #参数指定的视频文件名
        subclip_s = float(sys.argv[2]) #是否指定了只加载视频的前n秒,n为浮点数
    else:
        subclip_s =  None

    clip = VideoFileClip(movie_in)
    if subclip_s is not None:
        clip = clip.subclip(0, subclip_s)

    tracking = moviepy.video.tools.tracking.manual_tracking(clip,0,3, fps=2)[0] #取返回的第一个跟踪对象,实际上nobjects使用的是默认值1,因此也就一个跟踪对象
  
    clip_blurred = clip.fx(vfx.headblur, tracking.xi, tracking.yi, 30) #进行模糊化处理,圆半径设置为30像素

    clip_blurred.write_videofile(movie_in + '_blurred_tofxfy.mp4')
报错信息如下:
"C:\Program Files\Python37\python.exe" F:/study/python/project/moviepyTest/moviepyTest.py F:\video\zbl1.mp4 4
Traceback (most recent call last):
  File "F:/study/python/project/moviepyTest/moviepyTest.py", line 63, in <module>
    clip_blurred = clip.fx(vfx.headblur, tracking.xi, tracking.yi, 30) #进行模糊化处理,圆半径设置为30像素
  File "C:\Program Files\Python37\lib\site-packages\moviepy\Clip.py", line 212, in fx
    return func(self, *args, **kwargs)
  File "C:\Program Files\Python37\lib\site-packages\moviepy\video\fx\headblur.py", line 88, in headblur
    return clip.fl(fl)
  File "C:\Program Files\Python37\lib\site-packages\moviepy\Clip.py", line 136, in fl
    newclip = self.set_make_frame(lambda t: fun(self.get_frame, t))
  File "<decorator-gen-61>", line 2, in set_make_frame
  File "C:\Program Files\Python37\lib\site-packages\moviepy\decorators.py", line 14, in outplace
    f(newclip, *a, **k)
  File "C:\Program Files\Python37\lib\site-packages\moviepy\video\VideoClip.py", line 644, in set_make_frame
    self.size = self.get_frame(0).shape[:2][::-1]
  File "<decorator-gen-11>", line 2, in get_frame
  File "C:\Program Files\Python37\lib\site-packages\moviepy\decorators.py", line 89, in wrapper
    return f(*new_a, **new_kw)
  File "C:\Program Files\Python37\lib\site-packages\moviepy\Clip.py", line 93, in get_frame
    return self.make_frame(t)
  File "C:\Program Files\Python37\lib\site-packages\moviepy\Clip.py", line 136, in <lambda>
    newclip = self.set_make_frame(lambda t: fun(self.get_frame, t))
  File "C:\Program Files\Python37\lib\site-packages\moviepy\video\fx\headblur.py", line 85, in fl
    im[y1:y2, x1:x2] = mask*blurred + (1-mask)*orig
ValueError: assignment destination is read-only

Process finished with exit code 1

三、问题分析及解决办法

这个报错信息是由于numpy认为headblur中调用get_frame返回的帧numpy数组是不可编辑的,要解决这个问题有2个办法。

方法1:降低numpy的版本并将im变成可写的

经查资料需要将numpy的版本降到1.14.5,并将get_frame返回的帧变成可写:

  1. 降低numpy版本降到1.14.5,执行如下命令:
    pip install numpy==1.14.5 -i https://pypi.tuna.tsinghua.edu.cn/simple
  2. 修改headblur函数,将get_frame返回的帧变成可写,执行如下语句:
    im.flags.writeable = True
    更改后的headblur函数代码如下:
def headblur(clip,fx,fy,r_zone,r_blur=None):
    """
    Returns a filter that will blurr a moving part (a head ?) of
    the frames. The position of the blur at time t is
    defined by (fx(t), fy(t)), the radius of the blurring
    by ``r_zone`` and the intensity of the blurring by ``r_blur``.
    Requires OpenCV for the circling and the blurring.
    Automatically deals with the case where part of the image goes
    offscreen.
    """
    
    if r_blur is None: r_blur = int(2*r_zone/3)
    
    def fl(gf,t):
        
        im = gf(t)
        im.flags.writeable = True

        h,w,d = im.shape
        x,y = int(fx(t)),int(fy(t))
        x1,x2 = max(0,x-r_zone),min(x+r_zone,w)
        y1,y2 = max(0,y-r_zone),min(y+r_zone,h)
        region_size = y2-y1,x2-x1

        mask = np.zeros(region_size).astype('uint8')
        cv2.circle(mask, (r_zone,r_zone), r_zone, 255, -1,
                   lineType=cv2.CV_AA)
                               
        mask = np.dstack(3*[(1.0/255)*mask])
        
        orig = im[y1:y2, x1:x2]
        blurred = cv2.blur(orig,(r_blur, r_blur))
        im[y1:y2, x1:x2] = mask*blurred + (1-mask)*orig
        return im
    
    return clip.fl(fl)

使用该方法再执行前面的人脸跟踪代码就正常了。

说明:

如果numpy不降低版本,直接修改writeable,会报错:“ ValueError: cannot set WRITEABLE flag to True of this array

方法2:对返回帧的数据分段复制再与模糊化部分拼接

方法1降低了numpy的版本,虽然解决了一些问题,但可能会引入新的问题,如导致调用numpy的一些其他模块不能正常运行。所以最好的办法是在现有版本上修改headblur函数来解决问题,这就是下面的方法。

该方法是修改headblur函数代码,将返回的帧数据通过切片方式分解成不同部分,分别对应模糊化部分的左边、右边、上边、下边,再与模糊化部分一起重新构造新帧数据。修改之后的完整代码如下(添加备注部分是修改代码):

def headblur(clip, fx, fy, r_zone, r_blur=None):
    """
    Returns a filter that will blurr a moving part (a head ?) of
    the frames. The position of the blur at time t is
    defined by (fx(t), fy(t)), the radius of the blurring
    by ``r_zone`` and the intensity of the blurring by ``r_blur``.
    Requires OpenCV for the circling and the blurring.
    Automatically deals with the case where part of the image goes
    offscreen.
    """
    if r_blur is None: r_blur = 2 * r_zone / 3
    r_blur = int(2 * r_zone / 3)  # added by jwp,TypeError: integer argument expected, got float

    def fl(gf, t):
        im = gf(t)
        h, w, d = im.shape
        x, y = int(fx(t)), int(fy(t))

        x1, x2 = max(0, x - r_zone), min(x + r_zone, w)
        y1, y2 = max(0, y - r_zone), min(y + r_zone, h)
        region_size = y2 - y1, x2 - x1

        mask = np.zeros(region_size).astype('uint8')
        cv2.circle(mask, (r_zone, r_zone), r_zone, 255, -1,
                   lineType=cv2.CV_AA)

        mask = np.dstack(3 * [(1.0 / 255) * mask])

        orig = im[y1:y2, x1:x2]
        blurred = cv2.blur(orig, (r_blur, r_blur))
        imblur = mask * blurred + (1 - mask) * orig
        imseg = np.hstack((im[y1:y2, 0:x1],imblur,im[y1:y2,x2:])) #取模糊化对应矩形范围同一水平位置的矩形左边和右边的数据以及模糊数据一起水平堆叠获得模糊化矩形范围对应的所有水平数据
        imnew = np.vstack((im[0:y1,0:],imseg,im[y2:,0:])) #将模糊化对应矩形对应的所有水平数据与其上和其下的数据竖直堆叠作为返回的帧数据

        return imnew

    return clip.fl(fl)

四、小结

本文介绍了Python3.7+moviepy1.03+numpy1.19环境下,人脸模糊化函数headblur遇到的帧数据不能修改问题的两种解决办法,这两种办法都能解决问题,但老猿推荐最后一种方法。

更多moviepy的介绍请参考《PyQt+moviepy音视频剪辑实战文章目录》或《moviepy音视频开发专栏》。

关于收费专栏

老猿的付费专栏《使用PyQt开发图形界面Python应用》专门介绍基于Python的PyQt图形界面开发基础教程,付费专栏《moviepy音视频开发专栏》详细介绍moviepy音视频剪辑合成处理的类相关方法及使用相关方法进行相关剪辑合成场景的处理,两个专栏加起来只需要19.9元,都适合有一定Python基础但无相关专利知识的小白读者学习。这2个收费专栏都有对应免费专栏,只是收费专栏的文章介绍更具体、内容更深入、案例更多。

对于缺乏Python基础的同仁,可以通过老猿的免费专栏《专栏:Python基础教程目录》从零开始学习Python。

如果有兴趣也愿意支持老猿的读者,欢迎购买付费专栏。

跟老猿学Python、学5G!

☞ ░ 前往老猿Python博文目录

posted on 2020-07-13 23:01  老猿Python  阅读(740)  评论(0编辑  收藏  举报