简体   繁体   English

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

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

I have face detection script, and it's working as intended but I want to extract detected and cropped face as well.我有人脸检测脚本,它按预期工作,但我也想提取检测到和裁剪的人脸。 Then I want to differentiate the image name file with millisecond.然后我想用毫秒来区分图像名称文件。 eg:例如:

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

My problem is, the millisecond not increasing/updating (All the rest of the images have same timestamp, the i variable is increasing).我的问题是,毫秒没有增加/更新(所有其余的图像都具有相同的时间戳, i变量正在增加)。 I already put strftime inside the for loop.我已经把strftime放在 for 循环中。

This is my code, and the output:这是我的代码和输出:

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)

相同的图像名称文件

Btw, sorry for my english.顺便说一句,对不起我的英语。 I'll edit my question if someone still not understand what I'm asking.如果有人仍然不明白我在问什么,我会编辑我的问题。 Thank you in advance.先感谢您。

Your command你的命令

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

is out of your loop .不在你的圈子里 Put it inside:放在里面:

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)

Maybe you don't need the exact milliseconds, you only want different times.也许您不需要确切的毫秒数,您只需要不同的时间。 So put a wait in your loop, eg 100 milliseconds:所以在你的循环中等待,例如 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)

Note:笔记:

Instead of manually control your i variable, you may use the enumerate() built-in function (which is more Pythonic):您可以使用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