python如何在循环中保存文件_python-用不同的名称保存循环中的图像

我在循环中保存裁剪的图像时遇到问题.我的代码:

def run(self, image_file):

print(image_file)

cap = cv2.VideoCapture(image_file)

while(cap.isOpened()):

ret, frame = cap.read()

if ret == True:

img = frame

min_h = int(max(img.shape[0] / self.min_height_dec, self.min_height_thresh))

min_w = int(max(img.shape[1] / self.min_width_dec, self.min_width_thresh))

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = self.face_cascade.detectMultiScale(gray, 1.3, minNeighbors=5, minSize=(min_h, min_w))

images = []

for i, (x, y, w, h) in enumerate(faces):

images.append(self.sub_image('%s/%s-%d.jpg' % (self.tgtdir, self.basename, i + 1), img, x, y, w, h))

print('%d faces detected' % len(images))

for (x, y, w, h) in faces:

self.draw_rect(img, x, y, w, h)

# Fix in case nothing found in the image

outfile = '%s/%s.jpg' % (self.tgtdir, self.basename)

cv2.imwrite(outfile, img)

if cv2.waitKey(1) & 0xFF == ord('q'):

break

else:

break

cap.release()

cv2.destroyAllWindows()

return images, outfile

我在脸上裁剪的每一帧都有一个循环.问题在于,对于每张裁剪的图像和图片,它都具有相同的名称,最后,我只有最后一帧的脸.我应该如何修复此代码以保存所有裁剪的面部和图像?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值