python-用不同的名称保存循环中的图像
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-用不同的名称保存循环中的图像,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1363字,纯文字阅读大概需要2分钟。
内容图文
我在循环中保存裁剪的图像时遇到问题.我的代码:
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
我在脸上裁剪的每一帧都有一个循环.问题在于,对于每张裁剪的图像和图片,它都具有相同的名称,最后,我只有最后一帧的脸.我应该如何修复此代码以保存所有裁剪的面部和图像?
解决方法:
您将使用相同的名称保存每个文件.因此,您将覆盖以前保存的图像
outfile = '%s/%s.jpg' % (self.tgtdir, self.basename)
更改此行以在名称中添加随机字符串
outfile = '%s/%s.jpg' % (self.tgtdir, self.basename + str(uuid.uuid4()))
您也需要在文件顶部导入uuid
内容总结
以上是互联网集市为您收集整理的python-用不同的名称保存循环中的图像全部内容,希望文章能够帮你解决python-用不同的名称保存循环中的图像所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。