所以我找到了一个我认为对我非常有用的python script。据称,它会将照片分类到“模糊”或“不模糊”文件夹中。
我在很大程度上是一个python新手,但我在静止的Python3.7、numpy和openCV中做到了。我将脚本放在一个包含一堆.jpg图像的文件夹中,并通过在命令提示符中键入来运行它:
python C:\Users\myName\images\BlurDetection.py但是,当我运行它时,它会立即返回:
Done. Processed 0 files into 0 blurred, and 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))有什么想法为什么它可能不工作,或者哪里出了问题?请给我建议。谢谢!!
发布于 2019-11-19 16:39:08
你的脚本和照片在这里:
C:\Users\myName\images但这不是你的工作目录,也就是说,Python会在返回给你的任何目录中查找你的照片:
print(os.getcwd())要让Python在正确的文件夹中查找文件,只需执行以下操作:
os.chdir('C:\Users\myName\images')现在,它将能够命中文件。
发布于 2019-11-19 14:36:47
如果目录中已经存在模糊图像,请检查目录中的文件夹“.jpg”和"ok“。错误可能是因为这两个文件夹已存在于列出的目录中。我运行了这段代码,它对我有效,但当我在没有删除blurred and ok文件夹的情况下重新运行这段代码时,我得到了同样的错误。
https://stackoverflow.com/questions/58923389
复制相似问题