找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2593|回复: 1

[求助] 这个python程序如何让其运行

2

主题

3

帖子

3

积分

贫民

积分
3
xiop 发表于 2018-4-30 10:12:02 | 显示全部楼层 |阅读模式

如下python程序是计算某算法的性能指标,本人不会python,在自己的python3.5环境下总提示错误:

这句:from Stats import Stats
不知道是什么问题?

  1. import os
  2. import shutil
  3. import subprocess
  4. import sys

  5. from Stats import Stats

  6. call = subprocess.call
  7. #current process call the subprocess

  8. def main():   
  9.     datasetPath = sys.argv[1]
  10.     binaryRootPath = sys.argv[2]
  11.    
  12.     if not isValidRootFolder(datasetPath):
  13.         print('The folder ' + datasetPath + ' is not a valid root folder.');
  14.         return
  15.    
  16.     if os.path.exists(binaryRootPath):
  17.         print('The folder ' + binaryRootPath + ' has been cleaned.');
  18.         shutil.rmtree(binaryRootPath)
  19.     os.mkdir(binaryRootPath)
  20.    
  21.     processFolder(datasetPath, binaryRootPath)

  22. def processFolder(datasetPath, binaryRootPath):
  23.     """Call your executable for all sequences in all categories."""
  24.     #stats = Stats(datasetPath)  #STATS
  25.     for category in getDirectories(datasetPath):
  26.         #stats.addCategories(category)  #STATS
  27.         
  28.         categoryPath = os.path.join(datasetPath, category)
  29.         os.mkdir(os.path.join(binaryRootPath, category))
  30.         
  31.         for video in getDirectories(categoryPath):
  32.             videoPath = os.path.join(categoryPath, video)
  33.             binaryPath = os.path.join(binaryRootPath, category, video)
  34.             if isValidVideoFolder(videoPath):
  35.                 processVideoFolder(videoPath, binaryPath)            
  36.                 #confusionMatrix = compareWithGroungtruth(videoPath, binaryPath)  #STATS
  37.                
  38.                 #stats.update(category, video, confusionMatrix)  #STATS
  39.         #stats.writeCategoryResult(category)  #STATS
  40.     #stats.writeOverallResults()  #STATS

  41. def processVideoFolder(videoPath, binaryPath):
  42.     """Call your executable on a particular sequence."""
  43.     os.mkdir(binaryPath);
  44.     retcode = call([os.path.join('exe', 'TODO.exe'),
  45.                     videoPath, binaryPath],
  46.                    shell=True)

  47. def compareWithGroungtruth(videoPath, binaryPath):
  48.     """Compare your binaries with the groundtruth and return the confusion matrix"""
  49.     statFilePath = os.path.join(videoPath, 'stats.txt')
  50.     deleteIfExists(statFilePath)

  51.     retcode = call([os.path.join('exe', 'comparator.exe'),
  52.                     videoPath, binaryPath],
  53.                    shell=True)
  54.    
  55.     return readCMFile(statFilePath)

  56. def readCMFile(filePath):
  57.     """Read the file, so we can compute stats for video, category and overall."""
  58.         if not os.path.exists(filePath):
  59.                 print("The file " + filePath + " doesn't exist.\nIt means there was an error calling the comparator.")
  60.                 raise Exception('error');
  61.        
  62.     with open(filePath) as f:
  63.         for line in f.readlines():
  64.             if line.startswith('cm:'):
  65.                 numbers = line.split()[1:]
  66.                 return [int(nb) for nb in numbers[:5]]





  67. def isValidRootFolder(path):
  68.     """A valid root folder must have the six categories"""
  69.     categories = set(['dynamicBackground', 'baseline', 'cameraJitter', 'intermittentObjectMotion', 'shadow', 'thermal', 'badWeather', 'lowFramerate', 'nightVideos', 'PTZ', 'turbulence'])
  70.     folders = set(getDirectories(path))
  71.     return len(categories.intersection(folders)) == 6

  72. def isValidVideoFolder(path):
  73.     """A valid video folder must have \\groundtruth, \\input, ROI.bmp, temporalROI.txt"""
  74.     return os.path.exists(os.path.join(path, 'groundtruth')) and os.path.exists(os.path.join(path, 'input')) and os.path.exists(os.path.join(path, 'ROI.bmp')) and os.path.exists(os.path.join(path, 'temporalROI.txt'))

  75. def getDirectories(path):
  76.     """Return a list of directories name on the specifed path"""
  77.     return [file for file in os.listdir(path)
  78.             if os.path.isdir(os.path.join(path, file))]

  79. def deleteIfExists(path):
  80.     if os.path.exists(path):
  81.         os.remove(path)


  82. if __name__ == "__main__":
  83.     main()
复制代码







回复

使用道具 举报

1

主题

2

帖子

2

积分

贫民

积分
2
寒心烟雨情 发表于 2018-4-30 11:46:28 | 显示全部楼层
先安装Stats这个模块
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表