python 逐行时间性能分析之 line_profiler 模块

pip3 install line_profiler
@profile
def rank_from_video_blur(dir, start, end, num_out):
    idx2v = {}
    images = os.listdir(dir)
    for image in images:
        frame_num = int(re.findall(r'\d+', image)[0])
        if frame_num >= start and frame_num <= end:
            img = plt.imread(os.path.join(dir, image))
            img = cv2.resize(img, (960, 540), interpolation = cv2.INTER_CUBIC)
            gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
            v = cv2.Laplacian(gray,cv2.CV_64F).var()
            idx2v[v] = image
        
    b = sorted([(k,v) for k,v in idx2v.items()], reverse=True)

    results = []
    for score, image in b[: min(len(b), num_out)]:
         #print('frame: ' + image +' v: ' + str(score))
        results.append(os.path.join(dir, image))
        #plt.imshow(plt.imread(os.path.join(dir, image)))
        #plt.show()
    return results
kernprof -l -v loopdemo.py

-l表示逐行分析,-v用于输出。
在这里插入图片描述
%Time一列是每一行运行时间百分比,可以看到
img = plt.imread(os.path.join(dir, image))v = cv2.Laplacian(gray,cv2.CV_64F).var()两句消耗了大部分时间,分别消耗165和300s,这是由于我们读取的图片分辨率为1920*1080,共6631帧,因此时间花费很大。

对其进行downsample,再看消耗时间:
在这里插入图片描述
分别消耗185和80s,读取图片时间差不多不变,而对其进行拉普拉斯变换时间变为了将近1/4。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JYJ-Alex

在校学生,学习动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值