我正在使用 subprocess.run 在 QuPath 中运行脚本。我的 python 代码在下面,QuPath 脚本在下面。脚本运行但参数未传递。脚本中的第一个代码块打印识别的参数。
当Python代码运行时,QuPath脚本执行成功,但报告没有传递参数,并且脚本由于找不到项目路径而返回。 如果“script”参数放在前面,结果是相同的。 感谢您的想法!
Python代码:
qupath_executable = "C:/path/to/QuPath executable"
script_path = "C:/path/to/script.groovy"
project_path = "C:/path/to/project.qpproj"
command = [
qupath_executable,
"-p", project_path,
"script", script_path,
]
result = subprocess.run(command, capture_output=True, text=True, shell=True)
if result.returncode == 0:
print("Script executed successfully.")
print(result.stdout)
else:
print("Script execution failed.")
print(result.stderr)
QuPath 脚本:
import java.awt.BasicStroke
import java.awt.Graphics2D
import qupath.ext.stardist.StarDist2D
import qupath.lib.images.servers.ImageServer
import qupath.lib.images.servers.ImageServerProvider
// Print all arguments
println "Arguments passed to the script:"
args.eachWithIndex { arg, index ->
println "Argument ${index}: ${arg}"
}
// Retrieve the project path from the arguments
def projectPath = null
for (int i = 0; i < args.length; i++) {
println "Examining argument ${i}: ${args[i]}"
if (args[i] == "-p" && i + 1 < args.length) {
projectPath = args[i + 1]
break
}
}
if (!projectPath) {
println "Error: Project path not specified."
return
}
def annotationOutputPath = projectPath + '/Annotations'
def annotatedImageOutputPath = projectPath + '/AnnotatedImages'
new File(annotationOutputPath).mkdirs()
new File(annotatedImageOutputPath).mkdirs()
def pathModel = projectPath + '/Models/he_heavy_augment.pb'
def stardist = StarDist2D.builder(pathModel)
.threshold(0.5) // Prediction threshold
.normalizePercentiles(1, 99) // Percentile normalization
.pixelSize(0.5) // Resolution for detection
.build()
println 'StarDist model initialized.'
def imageFiles = new File(projectPath + '/Images').listFiles().findAll { it.name.endsWith('.png') || it.name.endsWith('.jpg') }
if (imageFiles.isEmpty()) {
println "No images found in the project directory."
return
}
imageFiles.each { file ->
println "Processing image: ${file.name}"
def server = ImageServerProvider.buildServer(file)
def imageData = server.readImageData()
def pathObjects = getSelectedObjects()
if (pathObjects.isEmpty()) {
println "No objects selected for image: ${file.name}"
return
}
println 'Selected objects: ' + pathObjects.size()
stardist.detectObjects(imageData, pathObjects)
println 'Object detection complete for image: ' + file.name
def annotations = imageData.getHierarchy().getAnnotationObjects()
println 'Number of annotations: ' + annotations.size()
def json = PathIO.writeObjectToJson(annotations)
def annotationFile = new File(annotationOutputPath, file.name + '_annotations.json')
annotationFile.text = json
println 'Annotations saved to ' + annotationFile.getPath()
def region = RegionRequest.createInstance(server.getPath(), 1, 0, 0, server.getWidth(), server.getHeight())
def img = ImageIO.read(server.readBufferedImage(region))
def g2d = img.createGraphics()
g2d.setColor(Color.RED)
g2d.setStroke(new BasicStroke(2))
for (annotation in annotations) {
def roi = annotation.getROI()
def shape = roi.getShape()
g2d.draw(shape)
}
g2d.dispose()
def annotatedImageFile = new File(annotatedImageOutputPath, file.name + '_annotated.png')
ImageIO.write(img, 'PNG', annotatedImageFile)
println 'Annotated image saved to ' + annotatedImageFile.getPath()
}
println 'Processing complete.'
您使用了不正确的参数组合:
args
是单个字符串并且 shell=True
。 shell 解析命令并将其拆分为单独的参数。args
是args和shell=False
的列表(序列)。