Add a date to the OpenCV output frames - no overwriting

This came up at the last meeting.

Here is a solution:

I have a file:

(jacked from

1. Added import datetime and import time (for good measure - I'm a hack)

2. Added variable for date_string

3. Added date_string variable to output name

4. Output the file


import datetime
import time
import cv2 as cv
# Load the model.
net = cv.dnn.readNet('face-detection-adas-0001.xml',
# Specify target device
# Read an image - used an image of Bob Swan for this example
frame = cv.imread('swanlarge.jpg')
if frame is None:
    raise Exception('Image not found!')
# Prepare input blob and perform an inference.
blob = cv.dnn.blobFromImage(frame, size=(672, 384), ddepth=cv.CV_8U)
out = net.forward()
# Draw detected faces on the frame.
for detection in out.reshape(-1, 7):
    confidence = float(detection[2])
    xmin = int(detection[3] * frame.shape[1])
    ymin = int(detection[4] * frame.shape[0])
    xmax = int(detection[5] * frame.shape[1])
    ymax = int(detection[6] * frame.shape[0])
    if confidence > 0.5:
        cv.rectangle(frame, (xmin, ymin), (xmax, ymax), color=(0, 255, 0))
# Save the frame to an image file:

# create a date_string variable with the format

date_string = time.strftime("%Y-%m-%d-%H:%M")

# create imageName variable that takes adds the date_string to the output file name - Found this 'clunky but works'
imageName = 'test' + date_string +'.png'

# output with name and frame
cv.imwrite( imageName, frame );


Now... the output files will not overwrite and will have date stamps. Not sure how long this extra processing will take and how that might impact any results.