windows-conda虚拟环境下安装MXNET,运行insightface(二)

1.运行环境继续安装

安装完torch后还需要安装:

pip install tensorboard -i https://pypi.tuna.tsinghua.edu.cn/simple


pip install insightface
# 报错的话,换源安装
pip install insightface -i https://pypi.tuna.tsinghua.edu.cn/simple

# 注意安装时区分cpu和gpu版
# cpu
pip install onnxruntime -i https://pypi.tuna.tsinghua.edu.cn/simple
# gpu
pip install onnxruntime-gpu -i https://pypi.tuna.tsinghua.edu.cn/simple

2.尝试使用insightface来测试环境是否搭建成功

参考链接:基于insightface实现的人脸检测,人脸识别,insightface源码讲解。-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/m0_55674689/article/details/134332043?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2~default~CTRLIST~PaidSort-1-134332043-blog-130600472.235%5Ev43%5Epc_blog_bottom_relevance_base2&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2~default~CTRLIST~PaidSort-1-134332043-blog-130600472.235%5Ev43%5Epc_blog_bottom_relevance_base2&utm_relevant_index=1

按照该链接教程,后续操作在python-package中进行。

1.进入 python-package目录下运行setup.py

python setup.py build_ext -i

2.创建text.py代码,测试代码运行情况

import cv2
import numpy as np
import insightface
from insightface.app import FaceAnalysis
from insightface.data import get_image as ins_get_image

app = FaceAnalysis(allowed_modules=['detection'], providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])

app.prepare(ctx_id=0, det_size=(640, 640))
img = ins_get_image('t1')  # 不用带后缀,图片放到./insightface/python-package/insightface/data/images
faces = app.get(img)
print("faces::::", faces)
print("len:", len(faces))
rimg = app.draw_on(img, faces)
cv2.imwrite("./ldh_out put.jpg", rimg)
cv2.imshow("frame", rimg)
if cv2.waitKey(0) & 0xFF == ord('Q'):
    cv2.destroyAllWindows()

这个代码写的bug是每次运行都会下载权值文件,但是不科学上网的话下载速度巨慢。因此在参考作者的基础上增加一些说明:

①不使用下载数据的权值文件格式是什么样的:

下载默认是在:C:\Users\Administrator/.insightface\models\

下载链接:https://github.com/deepinsight/insightface/releases/download/v0.7/buffalo_l.zip

我们可以直接用链接下载好后解压在当前路径下,文件结构如下:

②更改文件下载位置:

修改代码位置:D:\code\Insightface-master\python-package\insightface\app\face_analysis.py

class FaceAnalysis:
    # 修改root就可以更改权值文件下载路径
    def __init__(self, name=DEFAULT_MP_NAME, root='~/.insightface', allowed_modules=None, **kwargs):
        onnxruntime.set_default_logger_severity(3)
        self.models = {}
        self.model_dir = ensure_available('models', name, root=root)
        onnx_files = glob.glob(osp.join(self.model_dir, '*.onnx'))
        onnx_files = sorted(onnx_files)

③不重复下载代码修改:

修改代码位置:D:\code\Insightface-master\python-package\insightface\utils\storage.py

def download(sub_dir, name, force=False, root='~/.insightface'):
    _root = os.path.expanduser(root)
    dir_path = os.path.join(_root, sub_dir, name)
    print('download_path:', dir_path)
    zip_file_path = os.path.join(_root, sub_dir, name + '.zip')

    # ---------------------------------------- 修改后 --------------------------------------
    if os.path.exists(zip_file_path):
        print(f"模型目录 '{zip_file_path}' 已存在,跳过下载")
        return dir_path

    else:
        model_url = "%s/%s.zip" % (BASE_REPO_URL, name)
        download_file(model_url,
                      path=zip_file_path,
                      overwrite=True)
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)
        with zipfile.ZipFile(zip_file_path) as zf:
            zf.extractall(dir_path)
        # os.remove(zip_file_path)
        return dir_path
    # ------------------------------------------------------------------------------
    
    # # ------------------ 原来代码 ------------------
    # model_url = "%s/%s.zip"%(BASE_REPO_URL, name)
    # download_file(model_url,
    #          path=zip_file_path,
    #          overwrite=True)
    # if not os.path.exists(dir_path):
    #     os.makedirs(dir_path)
    # with zipfile.ZipFile(zip_file_path) as zf:
    #     zf.extractall(dir_path)
    # #os.remove(zip_file_path)
    # return dir_path

后续操作可以关注油豆皮这位CSDN博主,我目前进度只到这里。

油豆皮-CSDN博客

使用insightface还在研究中.....

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值