把日期我要救VideoCapture在一定條件下, 所以我寫了下面的代碼:(OpenCV的),在文件名
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
等str
s的正確保存。
另外,如果我做
print(type(now),now)
我需要幫助。
文件名謝謝你編輯的內容!我不知道它是如何工作的... 我應該學習更多..對此感到抱歉 –
您傳遞給'.format'方法的參數數量不正確/額外。字符串中的「{0}」代表傳遞給'format'的* first *參數,在你的情況下它是'date.now()'。而且你傳遞了三個參數,但只消耗2個('{0}','{1}')。 –
我已更改了代碼並再次進行了編輯。 所以對於{0},它是'not_detected_posture' ,對於{1}它是'str(date.now())' ,我仍然不知道發生了什麼 –