仅批处理 reading/saving 个文件夹中的最后一张图像
Batching only reading/saving last image in folder
我正在尝试将批处理添加到我正在制作的 OpenCV python 脚本中,但我终究无法看到我做错了什么。我是这方面的初学者,所以它可能有些愚蠢。最后,我希望脚本读取脚本当前工作目录中的每个图像文件,然后根据来自 openCV 的人脸检测进行裁剪,并将裁剪后的同名图像输出到 CWD 内的文件夹中。现在它所做的只是将文件夹中的最后一张图像输出到输出文件夹中。知道自己在做什么的人有什么想法吗?
import cv2
import sys
import os.path
import glob
#Cascade path
cascPath = 'haarcascade_frontalface_default.xml'
# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)
# Read Images
images = glob.glob('*.jpg')
for i in images:
image = cv2.imread(i,1)
#Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Find face(s) using cascade
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1, #size of groups
minNeighbors=5, #How many groups around are detected as face for it to be valid
minSize=(300, 300) #Min size in pixels for face
)
# Outputs number of faces found in image
print('Found {0} faces!'.format(len(faces)))
# Places a rectangle on face (For debugging, wont be in crop version)
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 255, 255), 4)
# Resizes image to fit monitor and displayes it
imOut = cv2.resize(image, (750, 1142))
#cv2.imshow("Faces found", imS)
#cv2.waitKey(0)
#Saves image to output folder and creates folder if it doesnt exist
if not os.path.exists('output'):
os.makedirs('output')
os.chdir('output')
cv2.imwrite(i, imOut)
images = glob.glob('*.jpg')
for i in images:
image = cv2.imread(i,1)
#Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
...
您正在做的是:一张一张地打开每张图片,当您到达最后一张图片时,对最后一张图片应用操作。
如果您只在第一个 for 循环下包含要应用于一张图像的所有操作,则可以轻松解决此问题。注意缩进,这基本上就是你在这里做错的地方。
images = glob.glob('*.jpg')
for i in images:
image = cv2.imread(i,1)
#Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#do all your operations here
我在代码中进行了多次更正
-
- 您需要给出
haarcascade_frontalface_default.xml
的完整路径
例如:在Unix系统中:
cascPath = 'opencv/data/haarcascades/haarcascade_frontalface_default.xml'
-
- 您不应在循环期间创建目录。您应该在循环之前创建它。
if not os.path.exists('output'):
os.makedirs('output')
-
- 您不需要更改目录来保存图像。只需在图片前添加路径即可。
img_name = "output/out_{}.png".format(c) # c is the counter
-
- 缩进很重要,否则,您可能会遇到困难。
示例代码:
import cv2
import os.path
import glob
# Cascade path
cascPath = '/opencv/data/haarcascades/haarcascade_frontalface_default.xml'
# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)
if not os.path.exists('output'):
os.makedirs('output')
# Read Images
images = glob.glob('images/*.jpg')
for c, i in enumerate(images):
image = cv2.imread(i, 1)
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Find face(s) using cascade
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1, # size of groups
minNeighbors=5, # How many groups around are detected as face for it to be valid
minSize=(300, 300) # Min size in pixels for face
)
# Outputs number of faces found in image
print('Found {0} faces!'.format(len(faces)))
# Places a rectangle on face (For debugging, wont be in crop version)
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 255, 255), 4)
if len(faces) > 0:
# Resizes image to fit monitor and displayes it
imOut = cv2.resize(image, (750, 1142))
# cv2.imshow("Faces found", imS)
# cv2.waitKey(0)
# Saves image to output folder and creates folder if it doesnt exist
# os.chdir('output')
img_name = "output/out_{}.png".format(c)
cv2.imwrite(img_name, imOut)
示例输出:
我正在尝试将批处理添加到我正在制作的 OpenCV python 脚本中,但我终究无法看到我做错了什么。我是这方面的初学者,所以它可能有些愚蠢。最后,我希望脚本读取脚本当前工作目录中的每个图像文件,然后根据来自 openCV 的人脸检测进行裁剪,并将裁剪后的同名图像输出到 CWD 内的文件夹中。现在它所做的只是将文件夹中的最后一张图像输出到输出文件夹中。知道自己在做什么的人有什么想法吗?
import cv2
import sys
import os.path
import glob
#Cascade path
cascPath = 'haarcascade_frontalface_default.xml'
# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)
# Read Images
images = glob.glob('*.jpg')
for i in images:
image = cv2.imread(i,1)
#Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Find face(s) using cascade
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1, #size of groups
minNeighbors=5, #How many groups around are detected as face for it to be valid
minSize=(300, 300) #Min size in pixels for face
)
# Outputs number of faces found in image
print('Found {0} faces!'.format(len(faces)))
# Places a rectangle on face (For debugging, wont be in crop version)
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 255, 255), 4)
# Resizes image to fit monitor and displayes it
imOut = cv2.resize(image, (750, 1142))
#cv2.imshow("Faces found", imS)
#cv2.waitKey(0)
#Saves image to output folder and creates folder if it doesnt exist
if not os.path.exists('output'):
os.makedirs('output')
os.chdir('output')
cv2.imwrite(i, imOut)
images = glob.glob('*.jpg')
for i in images:
image = cv2.imread(i,1)
#Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
...
您正在做的是:一张一张地打开每张图片,当您到达最后一张图片时,对最后一张图片应用操作。
如果您只在第一个 for 循环下包含要应用于一张图像的所有操作,则可以轻松解决此问题。注意缩进,这基本上就是你在这里做错的地方。
images = glob.glob('*.jpg')
for i in images:
image = cv2.imread(i,1)
#Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#do all your operations here
我在代码中进行了多次更正
-
- 您需要给出
haarcascade_frontalface_default.xml
的完整路径
例如:在Unix系统中:
cascPath = 'opencv/data/haarcascades/haarcascade_frontalface_default.xml'
- 您需要给出
-
- 您不应在循环期间创建目录。您应该在循环之前创建它。
if not os.path.exists('output'): os.makedirs('output')
-
- 您不需要更改目录来保存图像。只需在图片前添加路径即可。
img_name = "output/out_{}.png".format(c) # c is the counter
-
- 缩进很重要,否则,您可能会遇到困难。
示例代码:
import cv2
import os.path
import glob
# Cascade path
cascPath = '/opencv/data/haarcascades/haarcascade_frontalface_default.xml'
# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)
if not os.path.exists('output'):
os.makedirs('output')
# Read Images
images = glob.glob('images/*.jpg')
for c, i in enumerate(images):
image = cv2.imread(i, 1)
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Find face(s) using cascade
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1, # size of groups
minNeighbors=5, # How many groups around are detected as face for it to be valid
minSize=(300, 300) # Min size in pixels for face
)
# Outputs number of faces found in image
print('Found {0} faces!'.format(len(faces)))
# Places a rectangle on face (For debugging, wont be in crop version)
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 255, 255), 4)
if len(faces) > 0:
# Resizes image to fit monitor and displayes it
imOut = cv2.resize(image, (750, 1142))
# cv2.imshow("Faces found", imS)
# cv2.waitKey(0)
# Saves image to output folder and creates folder if it doesnt exist
# os.chdir('output')
img_name = "output/out_{}.png".format(c)
cv2.imwrite(img_name, imOut)
示例输出: