首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

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

  • 23-09-22 14:04
  • 3643
  • 5192
blog.csdn.net

☞ ░ 前往老猿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')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
报错信息如下:
"C:Program FilesPython37python.exe" F:/study/python/project/moviepyTest/moviepyTest.py F:videozbl1.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 FilesPython37libsite-packagesmoviepyClip.py", line 212, in fx
    return func(self, *args, **kwargs)
  File "C:Program FilesPython37libsite-packagesmoviepyvideofxheadblur.py", line 88, in headblur
    return clip.fl(fl)
  File "C:Program FilesPython37libsite-packagesmoviepyClip.py", line 136, in fl
    newclip = self.set_make_frame(lambda t: fun(self.get_frame, t))
  File "", line 2, in set_make_frame
  File "C:Program FilesPython37libsite-packagesmoviepydecorators.py", line 14, in outplace
    f(newclip, *a, **k)
  File "C:Program FilesPython37libsite-packagesmoviepyvideoVideoClip.py", line 644, in set_make_frame
    self.size = self.get_frame(0).shape[:2][::-1]
  File "", line 2, in get_frame
  File "C:Program FilesPython37libsite-packagesmoviepydecorators.py", line 89, in wrapper
    return f(*new_a, **new_kw)
  File "C:Program FilesPython37libsite-packagesmoviepyClip.py", line 93, in get_frame
    return self.make_frame(t)
  File "C:Program FilesPython37libsite-packagesmoviepyClip.py", line 136, in <lambda>
    newclip = self.set_make_frame(lambda t: fun(self.get_frame, t))
  File "C:Program FilesPython37libsite-packagesmoviepyvideofxheadblur.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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

三、问题分析及解决办法

这个报错信息是由于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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

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

说明:

如果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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

四、小结

本文介绍了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博文目录 ░

文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树首页概览333550 人正在系统学习中
老猿Python
微信公众号
专注Python相关语言、图像音视频处理、AI
注:本文转载自blog.csdn.net的LaoYuanPython的文章"https://blog.csdn.net/LaoYuanPython/article/details/107219560"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2024 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top