Ejemplo n.º 1
0
def deal_with_ias():
    """
    # 封装ias
    :param algo_image:  接收传入的镜像名称:image_name,
    :return:
    """
    res_datas = request.values
    port = res_datas.get('port')
    port = int(port)
    image_name = res_datas.get('image_name')

    # 获取到容器id
    cmd = run_sdk_config_GPU + f"-p {port}:80 {image_name}"
    status, res = sdk_subprocess(cmd)
    print(res, "res")
    if status:
        contain_id = res[:12]
    elif "port is already" in res:
        return jsonify(errno=RET.DATAERR, errmsg="端口被占用请重新输入")
    else:
        return jsonify(errno=RET.DATAERR, errmsg="输入镜像名称有误请重新确认之后输入")

    # 获取OpenCV版本
    url = request_host + "/api/v1.0/algo_sdk/opencv_message"
    data = {"image_name": image_name}
    OpenCv = requests.post(url, data=data).json().get("OpenCv")

    # 判断命令上传封装的ias包p
    if OpenCv == 3.4:
        cmd = f"cp {path}/sdk_package/ias/ias_3.4.tar.gz /tmp/ljx;tar -xvf /tmp/ljx/ias_3.4.tar.gz -C /tmp/ljx"
        status, _ = sdk_subprocess(cmd)
        if not status:
            return jsonify(errno=RET.DATAERR, errmsg="封装失败,请联系管理员")
    else:
        cmd = f"cp {path}/sdk_package/ias/ias_4.1.tar.gz /tmp/ljx;tar -xvf /tmp/ljx/ias_4.1.tar.gz -C /tmp/ljx"
        status, _ = sdk_subprocess(cmd)
        if not status:
            return jsonify(errno=RET.DATAERR, errmsg="封装失败,请联系管理员")

    # 上传成功之后解压 安装
    ias_install = f"docker exec  {contain_id} bash /tmp/give_license.sh &"
    subprocess.Popen(ias_install, shell=True)

    return jsonify(errno=RET.OK, errmsg='封装IAS成功,可以直接调用IAS')
Ejemplo n.º 2
0
def deal_with_vas():
    """
    # 封装vas
    :param algo_image:  接收传入的镜像名称:image_name,
    :return:
    """
    res_datas = request.values
    image_name = res_datas.get('image_name')

    # 获取OpenCV版本
    url = request_host + "/api/v1.0/algo_sdk/opencv_message"
    data = {"image_name": image_name}
    OpenCv = requests.post(url, data=data).json().get("OpenCv")

    # 判断命令上传封装的ias包p
    if OpenCv == 3.4:
        docker_vas = f"docker build -t {image_name}_test --build-arg IMAGE_NAME={image_name} -f {opencv34_dir} ."
    else:
        docker_vas = f"docker build -t {image_name}_test --build-arg IMAGE_NAME={image_name} -f {opencv41_dir} ."
    status, res_docker_vas = sdk_subprocess(docker_vas)
    if not status:
        return jsonify(errno=RET.DATAERR,
                       errmsg=f"封装失败,请联系管理员, {res_docker_vas}")

    # 上传成功之后解压 安装
    # 获取到容器id
    cmd = run_sdk_config_GPU + f"-p {image_name}_test"
    status, contain_id = sdk_subprocess(cmd)
    os.system(
        f"docker cp {path}/sdk_package/vas/authorzation.sh {contain_id}:/usr/local/ev_sdk "
    )
    os.system(
        f"docker exec  {contain_id} bash /usr/local/ev_sdk/authorzation.sh &")

    cmd = f"docker exec {contain_id} bash -c 'cat /usr/local/vas/vas_data/log/vas.INFO|grep \"ji_init return = 0\"'"
    status, res_code = sdk_subprocess(cmd)
    if not status:
        return jsonify(errno=RET.ALGOVERSIONERR, errmsg=f'封装VAS失败,{res_code}')
    if int(res_code[-1]) == 0:
        return jsonify(errno=RET.OK, errmsg='封装IAS成功,可以直接调用IAS')
    else:
        return jsonify(errno=RET.ALGOVERSIONERR, errmsg=f'封装VAS失败,{res_code}')
def run_files(self, rootDir, port, names, iou, args, alert_info, hat):
    filenames = iter_files(rootDir)
    xmls = filenames["xmls"]
    files = filenames["files"]
    total_files = len(files)
    file_count = 0
    path = os.path.dirname(
        os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    for xml in xmls:
        xml_create(xml, path)
    time.sleep(3)
    for file in files:
        file_count += 1
        if hat is not None and hat.endswith("gpu"):
            hat = "report_num_hat"
            txt_create(file, port, names, args, alert_info, hat)
        else:
            txt_create(file, port, names, args, alert_info)
        process = int(file_count / total_files * 100)
        self.update_state(state='PROGRESS',
                          meta={
                              'current': file_count,
                              'total': total_files,
                              'status': process
                          })
    main = os.path.join(path, "utils/sdk_precision/main.py")
    if iou is None:
        cmd = f"python3 {main}"
    else:
        cmd = f"python3 {main} -t {iou}"
    os.system(cmd)

    file_res = os.path.join(path, f"utils/sdk_precision/output/output.txt")
    with open(file_res, 'r') as f:
        res = f.read().splitlines()
        res = str(res).replace("'',", "\n")
    clear_dirs()
    contain_stop = "docker ps |grep %s|awk '{print $1}'|xargs docker stop" % port
    status, _ = sdk_subprocess(contain_stop)
    print(rootDir, "rootDir")
    os.system(f"rm -rf {rootDir}")
    return {
        'current': 100,
        'total': 100,
        'status': 'Task completed!',
        "result": res
    }
Ejemplo n.º 4
0
def deal_with_files_data(self, file_name, files_dir, port, file_suffix,
                         tag_suffix, tag_kinds):
    # 解压
    os.system(f"unzip {file_name} -d {files_dir}")
    # 删除文件
    os.system(f"rm -f {file_name}")
    total_files = iter_files(files_dir)

    # 封装ias 启动算法
    url = request_host + "/api/v1.0/algo_sdk/package_ias"
    data = {
        "port":
        port,
        "image_name":
        "ccr.ccs.tencentyun.com/source/dev_helmet_kww_cpu_sdk3.0_modc_uni_lic1b:v1.1.1"
    }
    res = requests.post(url, data=data).json().get("errno")
    if res != "0":
        return jsonify(error=RET.ALGOVERSIONERR, errmsg="封装ias失败")

    # 遍历
    # 验证文件和标签数据是否一致  判断文集和标签是否以指定文件后缀统一格式
    files_tag_dir = total_files["files_tag_dir"]
    files = total_files["files"]
    for file_tag_dir, file in zip(files_tag_dir, files):
        file_tag = file_tag_dir.split("/")[-1]
        # 判断文件和标注文件命名是否匹配
        if file_tag.split(".")[0] != file.split(".")[0]:
            error_files_list["filename_not_match"].append(file)
        # 判断是否是以否以指定后缀命名
        if not file.endswith(file_suffix):
            error_files_list["image"].append(file)
        if not file_tag.endswith(tag_suffix):
            error_files_list["tag"].append(file_tag)
        # 处理xml标签中存在小数点, 更正小数点为正数, 以及判断是否存在多余的标签, 并且删除掉
        if file_tag.endswith("xml"):
            get_xml_res(file_tag_dir, tag_kinds)
        # 判断xml类型是不是ascii
        with open(file_tag_dir, 'rb') as f:
            data = f.read()
            if chardet.detect(data).get("encoding") != 'ascii':
                error_files_list["tag_file_not_ascii"].append(file_tag)
        # 判断命名是否存在中文
        pattern = re.compile(u"[\u4e00-\u9fa5]+")
        result = re.findall(pattern, file)
        if result:
            error_files_list["file_contains_chinese"].append(file)
    self.update_state(state='PROGRESS',
                      meta={
                          'current': 4,
                          'total': 10,
                          'status': 40
                      })
    # 通过算法运行判断图片是否能正常运行
    files = total_files["files_dir"]
    url = request_host_without_port + ":" + str(port) + "/api/analysisImage"
    time.sleep(5)
    for file in files:
        filename = file.split("/")[-1]
        data = {'image': (filename, open(file, 'rb'))}
        try:
            requests.post(url, files=data)
        except Exception as e:
            error_files_list["file_cant_open"].append(filename)
    self.update_state(state='PROGRESS',
                      meta={
                          'current': 9,
                          'total': 10,
                          'status': 90
                      })
    # 打包下载结果
    with open(os.path.join(files_dir, "res.txt"), 'a') as f:
        f.write(str(error_files_list))
    os.system(f"cd {files_dir};tar -cvf result.tar *")
    result_files = f"{files_dir}/result.tar"
    cmd = "docker ps|grep %s|awk '{print $1}'" % port
    status, container_id = sdk_subprocess(cmd)

    return {
        'current': 100,
        'total': 100,
        'status': 'Task completed!',
        'result': result_files,
        "container_id": container_id,
        "files_dir": files_dir
    }
Ejemplo n.º 5
0
def sdk_opencv_message():
    """
    用于获取sdk的opencv版本等相关信息
    :return:
    """
    res_datas = request.values
    image_name = res_datas.get('image_name')
    sdk_version = 3

    # 获取到容器id
    cmd_run_sdk = run_sdk_config_GPU + f"{image_name}"
    status, res = sdk_subprocess(cmd_run_sdk)
    if status:
        contain_id = res[:12]
    else:
        return jsonify(errno=RET.DATAERR, errmsg="算法镜像不存在或者算法镜像有误,请确认之后再输入!")

    sdk_message = f"docker exec -it  {contain_id}  bash  -c 'cat /usr/local/ev_sdk/authorization/privateKey.pem'"
    res_p = subprocess.Popen(sdk_message,
                             shell=True,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
    stdout, stderr = res_p.communicate()
    if "No such file or directory" not in stdout.decode('utf-8'):
        errmsg = "(1):算法SDK版本为3.0系列 配置路径为:/usr/local/ev_sdk/config/algo_config.json \n"
    else:
        sdk_version = 2
        errmsg = "(1):算法SDK版本为2.0系列 配置路径为:/usr/local/ev_sdk/model/algo_config.json \n"

    auth_message = f"docker exec -it  {contain_id}  bash  -c 'cat /usr/local/ev_sdk/3rd/license/lib/pkgconfig/ji_license.pc |grep -i version'"
    res_p = subprocess.Popen(auth_message,
                             shell=True,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
    stdout, stderr = res_p.communicate()
    if not stdout.decode('utf-8').startswith("cat"):
        errmsg += "(2):当前默认应该使用最新的版本库20.1.3, 当前算法授权库版本为" + str(
            stdout.decode('utf-8'))
    else:
        errmsg += "(2):获取授权信息失败, 授权库不是最新的20.1.3" + "\n"

    opencv_message = f"docker exec -it  {contain_id}  bash ldd /usr/local/ev_sdk/lib/libji.so |egrep  'libopencv_core.so.4'"
    res_p = subprocess.Popen(opencv_message,
                             shell=True,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
    stdout, stderr = res_p.communicate()
    if stdout.decode('utf-8') is not '':
        # 公私钥  配置文件 查看
        privateKey = f"docker exec -it  {contain_id}  bash  -c 'cat /usr/local/ev_sdk/authorization/privateKey.pem'"
        res_p = subprocess.Popen(privateKey,
                                 shell=True,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
        stdout, stderr = res_p.communicate()
        privateKey = stdout.decode('utf-8')
        algo_config = f"docker exec -it  {contain_id}  bash  -c 'cat /usr/local/ev_sdk/config/algo_config.json'"
        res_p = subprocess.Popen(algo_config,
                                 shell=True,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
        stdout, stderr = res_p.communicate()
        algo_config = stdout.decode('utf-8')
        errmsg += '(3):当前OpenCV版本为:3.4, vas安装包:vas_v4.3_cv3.4.tar.gz, ias安装包:ias_v4.90_cv3.4.tar.gz'
        OpenCv = 3.4
        if sdk_version == 2:
            privateKey = "不支持2.0系列算法"
            algo_config = "不支持2.0系列算法"
        stop = f"docker stop {contain_id}"
        subprocess.Popen(stop,
                         shell=True,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
        return jsonify(errno=RET.OK,
                       errmsg=errmsg,
                       privateKey=privateKey,
                       algo_config=algo_config,
                       OpenCv=OpenCv)
    else:
        privateKey = f"docker exec -it  {contain_id}  bash  -c 'cat /usr/local/ev_sdk/authorization/privateKey.pem'"
        res_p = subprocess.Popen(privateKey,
                                 shell=True,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
        stdout, stderr = res_p.communicate()
        privateKey = stdout.decode('utf-8')
        algo_config = f"docker exec -it  {contain_id}  bash  -c 'cat /usr/local/ev_sdk/config/algo_config.json'"
        res_p = subprocess.Popen(algo_config,
                                 shell=True,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
        stdout, stderr = res_p.communicate()
        algo_config = stdout.decode('utf-8')
        if sdk_version == 2:
            privateKey = "不支持2.0系列算法"
            algo_config = "不支持2.0系列算法"
        errmsg += '(3):当前OpenCV版本为:4.1, vas安装包:vas_v4.3_cv4.1.tar.gz, ias安装包:ias_v4.74_cv4.1.tar.gz '
        OpenCv = 4.1
        stop = f"docker stop {contain_id}"
        subprocess.Popen(stop,
                         shell=True,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
        return jsonify(errno=RET.OK,
                       errmsg=errmsg,
                       privateKey=privateKey,
                       algo_config=algo_config,
                       OpenCv=OpenCv)