【问题标题】:(OpenCV) put date in filename(OpenCV)将日期放在文件名中
【发布时间】:2017-08-19 08:09:10
【问题描述】:

我想在某些情况下保存 VideoCapture, 所以我写了以下代码:

  date = datetime.now()
  detector = dlib.get_frontal_face_detector() 
  cap = cv2.VideoCapture(1)    #I use second camera
  global count, no_face
  total_number = 0
  count = 0
  no_face = 0
  num_bad_posture = 0
  not_detected_posture = 0
  while True:

        ret, frame = cap.read()
        frame = cv2.resize(frame,None,fx=sf,fy=sf, interpolation=cv2.INTER_AREA)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        dets = detector(gray,1) 

        if not dets:
              print('no face')
              no_face += 1
              if no_face > 15:
                    print('no face!! for real!!')
                    now = str(date.now())
                    not_detected = cv2.resize(frame,None,fx=5,fy=5,interpolation = cv2.INTER_CUBIC)
                    not_detected_posture += 1
                    print(type(now))
                    cv2.imwrite('./images/non_detected/non_detected({0},{1}).jpg'. format(not_detected_posture,now),not_detected)
                    no_face=0
        for i, d in enumerate(dets):
              no_face = 0
              w = d.width()
              h = d.height()
              x = d.left()
              y = d.top()

如果我运行此代码,则不会保存文件。 此外,如果我删除date.now() 并只输入num_detected,它会正确保存 我不知道文件名有什么问题(因为它的类型是str,而其他strs 已正确保存。

如果我这样做了

 print(type(now),now)

出现了

我需要帮助。

【问题讨论】:

  • 感谢您编辑内容!我不知道它是如何工作的......我应该多学习......对此感到抱歉
  • 您传递给.format 方法的参数不正确/数量过多。字符串中的{0} 表示传递给format第一个 参数,在您的情况下为date.now()。你传递了三个参数,但只消耗了 2 个({0}{1})。
  • 我已更改代码并再次编辑它。因此,对于 {0},它是“not_detected_posture”,对于 {1},它是“str(date.now())”,但我仍然不知道发生了什么
  • 我们需要更多的代码来使用它。尤其是您导入/声明date 的部分。因为在当前代码中,没有什么特别的错误会导致问题。
  • 没有日期时间的文件被正确保存,如'non_detected(1).jpg'。但是,如果我添加“str(date.now())”,则不会保存文件。 (我也检查了 type(str(date.now())) 它的类型是

标签: python python-2.7 opencv filenames


【解决方案1】:

我会为文件名使用更有限的字符集,避免使用空格、冒号、括号等内容。使用自定义日期时间格式以您想要的形式生成时间戳。

例子:

from datetime import datetime
from os import path


BASE_DIR = './images/non_detected'
PREFIX = 'non_detected'
EXTENSION = 'jpg'
file_name_format = "{:s}-{:d}-{:%Y%m%d_%H%M%S}.{:s}"


date = datetime.now()
not_detected_posture = 0


file_name = file_name_format.format(PREFIX, not_detected_posture, date, EXTENSION)

file_path = path.normpath(path.join(BASE_DIR, file_name))

# ---------

print("File name = '%s'" % file_name)
print("File path = '%s'" % file_path)

输出:

File name = 'non_detected-0-20170819_152459.jpg'
File path = 'images/non_detected/non_detected-0-20170819_152459.jpg'

简单、明确且可移植。


也许还有一项改进。如果您对将生成多少张图像有一个粗略的了解,您可以对数字进行零填充,以便文件名按顺序排序。例如,如果您知道最多有 10000 个,那么您可以这样做

file_name_format = "{:s}-{:04d}-{:%Y%m%d_%H%M%S}.{:s}"

给你一个类似的文件名

File name = 'non_detected-0000-20170819_152459.jpg'

【讨论】:

    【解决方案2】:

    由于包含日期时间,问题可能出在文件名中的 ':''.' 上。我不确定是哪一个,因为您可以拥有包含这些文件名的文件名,并且您可以从 python AFAIK 中编写此类文件名。因此,这可能是 cv2 特有的问题。

    【讨论】:

      猜你喜欢
      • 2016-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-20
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多