繁体   English   中英

按顺序写入一些图像时,图像名称文件的毫秒数不会增加(cv2.imwrite)Python

[英]Millisecond for image name file not increasing when writing some images in sequence (cv2.imwrite) Python

我有人脸检测脚本,它按预期工作,但我也想提取检测到和裁剪的人脸。 然后我想用毫秒来区分图像名称文件。 例如:

  1. 17.564.jpg
  2. 17.777.jpg
  3. 17.987.jpg

我的问题是,毫秒没有增加/更新(所有其余的图像都具有相同的时间戳, i变量正在增加)。 我已经把strftime放在 for 循环中。

这是我的代码和输出:

i = 0
now = time.time()
mlsec = repr(now).split('.')[1][:3]

for (x, y, w, h) in faces:
    cv2.rectangle(readImg, (x, y), (x + w, y + h), (0, 255, 0), 1)
    crop = readImg[y:y + h, x:x + w]
    i += 1
    ms = "%d. " % i + time.strftime("%S.{}".format(mlsec))
    cv2.imwrite("crop\\%s.jpg" % ms, crop)

相同的图像名称文件

顺便说一句,对不起我的英语。 如果有人仍然不明白我在问什么,我会编辑我的问题。 先感谢您。

你的命令

mlsec = repr(now).split('.')[1][:3]

不在你的圈子里 放在里面:

i = 0

for (x, y, w, h) in faces:
    cv2.rectangle(readImg, (x, y), (x + w, y + h), (0, 255, 0), 1)
    crop = readImg[y:y + h, x:x + w]
    i += 1
    mlsec = repr(now).split('.')[1][:3]                         # <------ Here
    ms = "%d. " % i + time.strftime("%S.{}".format(mlsec))
    cv2.imwrite("crop\\%s.jpg" % ms, crop)

也许您不需要确切的毫秒数,您只需要不同的时间。 所以在你的循环中等待,例如 100 毫秒:

import time

i = 0

for (x, y, w, h) in faces:
    time.sleep(0.1)           # <------------------------------- wait 0.1s = 100 ms
    cv2.rectangle(readImg, (x, y), (x + w, y + h), (0, 255, 0), 1)
    crop = readImg[y:y + h, x:x + w]
    i += 1
    mlsec = repr(now).split('.')[1][:3]                         # <------ Here
    ms = "%d. " % i + time.strftime("%S.{}".format(mlsec))
    cv2.imwrite("crop\\%s.jpg" % ms, crop)

笔记:

您可以使用enumerate()内置函数(更像 Pythonic),而不是手动控制i变量:

import time

for i, (x, y, w, h) in enumerate(faces):       <--- Here; your i += 1 command I deleted 
    time.sleep(0.1)
    cv2.rectangle(readImg, (x, y), (x + w, y + h), (0, 255, 0), 1)
    crop = readImg[y:y + h, x:x + w]
    mlsec = repr(now).split('.')[1][:3]
    ms = "%d. " % i + time.strftime("%S.{}".format(mlsec))
    cv2.imwrite("crop\\%s.jpg" % ms, crop)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM