Python3.6中的变量列表

Python3.6中的变量列表,python,list,variables,cv2,Python,List,Variables,Cv2,基本上,我想从两个变化的变量中列出一个列表。我使用Adrian Rosebrock的代码来查找图片中拉普拉斯的方差,但我希望它在图片更改时列出两列“file_name”“fm”。下面是脚本的当前代码 from imutils import paths import argparse import cv2 def variance_of_laplacian(image): # compute the Laplacian of the image and then return the f

基本上,我想从两个变化的变量中列出一个列表。我使用Adrian Rosebrock的代码来查找图片中拉普拉斯的方差,但我希望它在图片更改时列出两列“file_name”“fm”。下面是脚本的当前代码

from imutils import paths
import argparse
import cv2

def variance_of_laplacian(image):
    # compute the Laplacian of the image and then return the focus
    # measure, which is simply the variance of the Laplacian
    return cv2.Laplacian(image, cv2.CV_64F).var()

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--images", required=True,
    help="path to input directory of images")
ap.add_argument("-t", "--threshold", type=float, default=100.0,
    help="focus measures that fall below this value will be considered 'blurry'")
args = vars(ap.parse_args())

# loop over the input images
for imagePath in paths.list_images(args["images"]):
    # load the image, convert it to grayscale, and compute the
    # focus measure of the image using the Variance of Laplacian
    # method
    image = cv2.imread(imagePath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    fm = variance_of_laplacian(gray)
    text = "Not Blurry"

    # if the focus measure is less than the supplied threshold,
    # then the image should be considered "blurry"
    if fm < args["threshold"]:
        text = "Blurry"

    # show the image
    cv2.putText(image, "{}: {:.2f}".format(text, fm), (10, 30),
        cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 3)
    cv2.imshow("Image", image)
    key = cv2.waitKey(0)
从imutils导入路径
导入argparse
进口cv2
定义拉普拉斯的方差(图):
#计算图像的拉普拉斯函数,然后返回焦点
#度量,它只是拉普拉斯函数的方差
返回cv2.Laplacian(图,cv2.CV_64F).var()
#构造参数并解析参数
ap=argparse.ArgumentParser()
ap.add_参数(“-i”,“--images”,required=True,
help=“图像输入目录的路径”)
ap.add_参数(“-t”,“--threshold”,type=float,default=100.0,
help=“低于此值的聚焦度量值将被视为“模糊”)
args=vars(ap.parse_args())
#循环输入图像
用于路径中的imagePath。列出_图像(args[“图像”]):
#加载图像,将其转换为灰度,然后计算
#基于拉普拉斯方差的图像聚焦测量
#方法
image=cv2.imread(imagePath)
灰色=cv2.CVT颜色(图像,cv2.COLOR\u BGR2GRAY)
fm=拉普拉斯方差(灰色)
text=“不模糊”
#如果焦点测量值小于提供的阈值,
#那么图像应该被认为是“模糊的”
如果fm

所以我想让它生成一个.txt或任何带有文件名和fm值的文件。我希望你们能理解!谢谢

要将结果写入文件,请执行以下操作:

# .... skipping initial setup
with open("results.txt", "w") as results_file:
    for imagePath in paths.list_images(args["images"]):
        # .... skip image analysis, results stored in variable 'text'
        # write result to file
        results_file.write("{} {}\n".format(imagePath, text))
# results_file will be closed when the with loop ends

啊!非常感谢。别担心。人们可能会否决您的问题,因为不清楚您已经尝试了什么,以及在实现过程中遇到了什么问题。阅读将帮助你在将来形成更好的问题。祝你好运