博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
YOLO-V2实现AIP接口化
阅读量:2051 次
发布时间:2019-04-28

本文共 2149 字,大约阅读时间需要 7 分钟。

安装好Yolo-v2后,官方给出的调用化方法为,在命令行界面输入

./darknet detect cfg/yolov3.cfg  cfg/yolov3.weights /home/it/Yolo/crop001007.png

这种方式并不对python代码友好。于是我选择用subprocess模块来实现模拟命令行调用。

#getstatusoutput调用
声明:环境 python3.6
废话少说,先上代码:

import subprocessimport osimport shutilfrom PedestrainWeb.Utils.readyaml import ReadYamlfrom PedestrainWeb.Utils.fileutil import FileUtildef yoloRredict(imagePath):    config = ReadYaml(os.path.join(FileUtil.getProjectObsPath(),'PedestrainConfig.yaml')).getValue()    darknet_path = config['darknet_path'] #darknet 存放路径    dir = os.path.dirname(FileUtil.getProjectObsPath())  #根目录    PedestrainImgPath, f = os.path.split(imagePath)    dir_string = 'cd {0} && ./darknet detect cfg/yolov3.cfg  cfg/yolov3.weights {1}'.format(darknet_path,os.path.join(dir,imagePath))    (status, output)=subprocess.getstatusoutput(dir_string)    shutil.copy(os.path.join(darknet_path,'predictions.png'), os.path.join(dir,PedestrainImgPath))  # 移动文件到目标路径(移动文件呢,目标路径)    return os.path.join(PedestrainImgPath,'predictions.png'),len(output.split('person'))-1if __name__ == '__main__':    imagePath ="cache/PedestrainWeb_Img/b44e1395.PNG"    yoloRredict(imagePath)

在这里我用到的是subprocess.getstatusoutput。若环境是python2.7的同学,请尝试使用command, status,output = commands.getstatusoutput("cmd")


早期的Python版本中,主要是通过os.system()、os.popen().read()等函数来执行命令行指令的,另外还有一个很少使用的commands模块。但是从Python 2.4开始官方文档中建议使用的是subprocess模块,commands模块的功能已经被整合到subprocess中了。可查看来了解具体细节。


##各种尝试

  1. pExpect
    在最开始的时候,我曾尝试用pExpect 模块来实现该功能。
import pexpectinput_string='./darknet detect cfg/yolov3.cfg  cfg/yolov3.weights /home/it/Yolo/crop001007.png'# 启动FTP服务器,并将运行期间的输出都放到标准输出中process = pexpect.spawn('cd /home/it/Yolo/darknet')process.logfile_read = sys.stdout

结果发现spawm 没任何输出,因为spawm函数第一个必须输入一个命令,然后我就放弃了。

2. sh脚本实现

#!/bin/bashcd /home/it/Yolo/darknet && ./darknet detect cfg/yolov3.cfg  cfg/yolov3.weights /home/it/Yolo/crop001007.png

后来我换了一种思路,用shell脚本能实现该功能。在这里将识别路径用 ¥1设置成变量即可。结果能实现。

3. os.system()函数

>>> import os>>>>>> retcode = os.system('dir')

os.sysyem能实现,也能给shell脚本添加参数,却没办法记录stdout的输入内容

4.os.popen()函数

>>> import os>>>>>> ret = os.popen('dir').read()

popen函数,能实现shell脚本,却没办法给shell脚本添加参数

本人修改的代码

#参考文献

转载地址:http://bewlf.baihongyu.com/

你可能感兴趣的文章
mybatis 根据 数据库表 自动生成 实体
查看>>
win10将IE11兼容ie10
查看>>
checkbox设置字体颜色
查看>>
第一篇 HelloWorld.java重新学起
查看>>
ORACLE表空间扩张
查看>>
orcal 循环执行sql
查看>>
web.xml配置监听器,加载数据库信息配置文件ServletContextListener
查看>>
结构型模式之桥接模式(Bridge)
查看>>
行为型模式之状态模式(State)
查看>>
行为型模式之策略模式(Strategy)
查看>>
行为型模式之模板方法模式(TemplateMethod)
查看>>
行为型模式之访问者模式(Visitor)
查看>>
大小端详解
查看>>
source insight使用方法简介
查看>>
<stdarg.h>头文件的使用
查看>>
C++/C 宏定义(define)中# ## 的含义 宏拼接
查看>>
Git安装配置
查看>>
linux中fork()函数详解
查看>>
C语言字符、字符串操作偏僻函数总结
查看>>
Git的Patch功能
查看>>