栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

2021SC@SDUSC山东大学软件学院软件工程应用与实践--YOLOV5代码分析(四)general.py-2

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021SC@SDUSC山东大学软件学院软件工程应用与实践--YOLOV5代码分析(四)general.py-2

2021SC@SDUSC

目录

前言

is_writeable函数

is_docker函数

is_colab函数

is_pip函数

is_ascii函数

file_size函数

check_online函数

emojis函数

check_git_status函数

check_python函数

 check_version函数

check_requirements函数

make_divisible函数

check_img_size函数

check_imshow函数

check_suffix函数

 check_yaml函数

check_file函数

总结


​​​​​​​

前言

这篇文章我们继续对yolov5项目源码进行解读

is_writeable函数
def is_writeable(dir, test=False):
    # Return True if directory has write permissions, test opening a file with write permissions if test=True
    if test:  # method 1
        file = Path(dir) / 'tmp.txt'
        try:
            with open(file, 'w'):  # open file with write permissions
                pass
            file.unlink()  # remove file
            return True
        except IOError:
            return False
    else:  # method 2
        return os.access(dir, os.R_OK)  # possible issues on Windows

参数

dir:测试该目录是否可写,可写则返回true,否则返回false

test:为true时 ,通过创建文件来进行测试

该函数的目标是对给定的目录进行测试,测试当前目录是否允许写。

如果test为true,即通过打开文件进行测试,会在给定的目录下创建tmp.txt,file的值为'dir/tmp.txt',接下来尝试以写的形式打开该文件,如果没有报错,将创建的文件移除并且返回true,如果报错了直接返回false。

test为false时,直接通过os模块看给定目录是否允许写。

is_docker函数
def is_docker():
    # Is environment a Docker container?
    return Path('/workspace').exists()  # or Path('/.dockerenv').exists()

返回当前环境是不是一个docker容器,如果根目录下存在/workspace,说明当前环境是一个docker容器,返回true,不然就返回false

is_colab函数
def is_colab():
    # Is environment a Google Colab instance?
    try:
        import google.colab
        return True
    except Exception as e:
        return False

判断当前环境是不是google colab。

Colaboratory 是一个 Google 研究项目,旨在帮助传播机器学习培训和研究成果。它是一个 Jupyter 笔记本环境,不需要进行任何设置就可以使用,并且完全在云端运行。

Colaboratory 笔记本存储在 Google 云端硬盘中,并且可以共享,就如同使用 Google 文档或表格一样。Colaboratory 可免费使用。

利用Colaboratory ,可以方便的使用Keras,TensorFlow,PyTorch,OpenCV等框架进行深度学习应用的开发。

is_pip函数
def is_pip():
    # Is file in a pip package?
    print(Path(__file__))
    print(Path(__file__).resolve())
    print(Path(__file__).resolve().parts)
    return 'site-packages' in Path(__file__).resolve().parts

判断当前文件是不是在pip包中

Path(__file__).resolve()返回当前文件的路径,.parts将路径拆开,如当前路径为C:\yolov5\general.py,parts返回一个列表[C:\,yolov5,gengral.py]。

is_ascii函数
def is_ascii(s=''):
    # Is string composed of all ASCIi (no UTF) characters?
    s = str(s)  # convert list, tuple, None, etc. to str
    return len(s.encode().decode('ascii', 'ignore')) == len(s)

参数

s:需要进行判断的字符串

判断s是不是全部由ASCII码组成,首先将s转换为字符串,然后对s的长度和转换成ascii码后的s的长度进行比较,相等就说明s全部由ascii码组成,就返回true,不相等说明s包含了不是ascii码的字符,就返回false

file_size函数
def file_size(path):
    # Return file/dir size (MB)
    path = Path(path)
    if path.is_file():
        return path.stat().st_size / 1E6
    elif path.is_dir():
        return sum(f.stat().st_size for f in path.glob('**' + file, recursive=True)  # find file
        assert len(files), f'File not found: {file}'  # assert file was found
        assert len(files) == 1, f"Multiple files match '{file}', specify exact path: {files}"  # assert unique
        return files[0]  # return file

参数

file:文件名或网址

suffix:后缀名

 该函数实现了在目录中搜索文件或是在网络上下载文件

首先对file的后缀进行检查,然后将file转换成str

如果file是文件名或为空,直接返回file

如果file是个网址,会下载该网址的内容,新建一个file,这个file存放下载的内容

如果都不满足,即当前目录下不存在file这个文件,则会在目录下所有子目录搜索该文件,当有多个重名的文件时返回第一个找到的文件

总结

这部分代码基本都是一些检查是否符合规范的,算不上项目的核心部分,由于算法的核心部分主要是在定义模型上,为了体现我对算法的理解,在后面我会用理论的方式来讲解核心算法。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/313670.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号