【发布时间】: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