gpt4 book ai didi

python - 为什么这个使用 openCV 的简单 python 脚本不能工作?

转载 作者:行者123 更新时间:2023-12-01 06:52:56 24 4
gpt4 key购买 nike

所以我找到了python script我认为这对我来说非常有用。据称,它会将照片分类到“模糊”或“不模糊”文件夹中。

我是一个Python新手,但我仍然使用Python 3.7、numpy和openCV。我将脚本放在包含一堆 .jpg 图像的文件夹中,然后通过在命令提示符中键入来运行它:

python C:\Users\myName\images\BlurDetection.py

当我运行它时,它立即返回:

完成。将0个文件处理成0个模糊,0个ok。

没有错误消息或任何内容。它只是没有做它应该做的事情。

这是脚本。

#
# Sorts pictures in current directory into two subdirs, blurred and ok
#

import os
import shutil
import cv2

FOCUS_THRESHOLD = 80
BLURRED_DIR = 'blurred'
OK_DIR = 'ok'

blur_count = 0
files = [f for f in os.listdir('.') if f.endswith('.jpg')]

try:
os.makedirs(BLURRED_DIR)
os.makedirs(OK_DIR)
except:
pass

for infile in files:

print('Processing file %s ...' % (infile))
cv_image = cv2.imread(infile)

# Covert to grayscale
gray = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)

# Compute the Laplacian of the image and then the focus
# measure is simply the variance of the Laplacian
variance_of_laplacian = cv2.Laplacian(gray, cv2.CV_64F).var()

# If below threshold, it's blurry
if variance_of_laplacian < FOCUS_THRESHOLD:
shutil.move(infile, BLURRED_DIR)
blur_count += 1
else:
shutil.move(infile, OK_DIR)

print('Done. Processed %d files into %d blurred, and %d ok.' % (len(files), blur_count, len(files)-blur_count))

有什么想法为什么它可能不起作用或者出了什么问题吗?请指教。谢谢!!

最佳答案

您的脚本和照片在这里:

C:\Users\myName\images

但这不是您的工作目录,即Python会在返回给您的任何内容中查找您的照片:

print(os.getcwd())

要让 Python 在正确的文件夹中查找文件,只需执行以下操作:

os.chdir('C:\Users\myName\images')

现在它将能够命中文件。

关于python - 为什么这个使用 openCV 的简单 python 脚本不能工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58923389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com