簡體   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