代码:
# coding:utf-8
# 打包
# pyinstaller -w runtest.py
import sys
import os
import re
import logging
# execute command, and return the output
def execCmd(cmd):
r = os.popen(cmd)
text = r.read()
r.close()
return text
if __name__ == '__main__':
# 必须带参数使用,传入检测数据目录
if len(sys.argv) > 1 :
# 先生成test.list
count = 1044
oct_path = sys.argv[1]
oct_path = oct_path.split(" ")[0]
oct_path = oct_path[0:len(oct_path)-1]
test_list_path = r"test.list"
f = open(test_list_path, "w+", encoding='utf-8')
for i in range(1,count+1):
path = oct_path + "/s%03d.jpg\n" % i
f.write(path)
# 每次都会少两行,因此额外多加两行抽数
path = oct_path + "/s001.jpg\n"
f.write(path)
f.write(path)
f.close()
# 检测
cmd = "test classifier test metal.data 19_448.cfg 19_448_final.weights"
result = execCmd(cmd)
# print(result)
m = re.findall(r"\t\d+\.?\d*e?-?\d*", result)
# print(m)
# count = int(len(m)/2)
# print(count)
# 保存分析可读文件
f2 = open(oct_path + "/simple.txt", "w+")
for j in range(0, count):
if float(m[j*2])>float(m[j*2+1]):
f2.write("0")
else:
fp.write("1")
f2.close()
else:
print("Wrong parameter.")