Save & load metrics when outputdir is set

This commit is contained in:
Ruben van de Ven 2019-02-05 20:06:22 +01:00
parent 0de8e62300
commit d6e54cf59a

View file

@ -88,6 +88,11 @@ c.set(4, 720)
predictor_path = "shape_predictor_68_face_landmarks.dat" predictor_path = "shape_predictor_68_face_landmarks.dat"
if args.output_dir:
lastMetricsFilename = os.path.join(args.output_dir, 'last_metrics.p')
else:
lastMetricsFilename = None
detector = dlib.get_frontal_face_detector() detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path) predictor = dlib.shape_predictor(predictor_path)
@ -97,7 +102,20 @@ screenDrawCorners = np.array([[10,60], [90, 60], [10, 110], [90, 110]])
metricsSize = [1920,1080] metricsSize = [1920,1080]
metricsSize = [1280,800] metricsSize = [1280,800]
dataframe = pd.DataFrame(columns=['x','y']) dataframe = pd.DataFrame(columns=['x','y'])
metrics = np.zeros((metricsSize[1], metricsSize[0])) # (y, x)
metrics = None
if lastMetricsFilename and os.path.isfile(lastMetricsFilename):
try:
with open(lastMetricsFilename, "rb") as fp:
metrics = pickle.load(fp)
logger.warn("Loaded metrics from {}".format(lastMetricsFilename))
except Exception as e:
logger.exception(e)
if metrics is None:
metrics = np.zeros((metricsSize[1], metricsSize[0])) # (y, x)
logger.warn("New metrics")
screenDrawCorners = np.array([[0,0], [1919,0], [0, 1079], [1919,1079]]) screenDrawCorners = np.array([[0,0], [1919,0], [0, 1079], [1919,1079]])
def create_perspective_transform_matrix(src, dst): def create_perspective_transform_matrix(src, dst):
@ -461,12 +479,12 @@ while True:
tm21 = time.time() tm21 = time.time()
normalisedMetrics = metrics / (np.max(metrics)) normalisedMetrics = metrics / (np.max(metrics))
# convert to colormap, thanks to: https://stackoverflow.com/a/10967471 # convert to colormap, thanks to: https://stackoverflow.com/a/10967471
normalisedMetrics = np.uint8(cm.plasma(normalisedMetrics)*255) normalisedMetricsColored = np.uint8(cm.plasma(normalisedMetrics)*255)
tm22 = time.time() tm22 = time.time()
logger.debug("Max normalised metrics: %f", np.max(normalisedMetrics)) logger.debug("Max normalised metrics: %f", np.max(normalisedMetrics))
# logger.info(normalisedMetrics) # logger.info(normalisedMetrics)
tm23 = time.time() tm23 = time.time()
image = Image.fromarray(normalisedMetrics) image = Image.fromarray(normalisedMetricsColored)
wpercent = (imageWindowSize[0] / float(image.size[0])) wpercent = (imageWindowSize[0] / float(image.size[0]))
hsize = int((float(image.size[1]) * float(wpercent))) hsize = int((float(image.size[1]) * float(wpercent)))
image = image.resize((imageWindowSize[0], hsize)) image = image.resize((imageWindowSize[0], hsize))
@ -507,9 +525,12 @@ while True:
) )
) )
image.save(filename) image.save(filename)
with open(lastMetricsFilename, 'wb') as fp:
pickle.dump( metrics, fp )
logger.debug("Saved frame to {}".format(filename)) logger.debug("Saved frame to {}".format(filename))
lastSaveTime = now lastSaveTime = now
pass
# (optionally) very slowly fade out previous metrics: # (optionally) very slowly fade out previous metrics:
metrics = metrics * .9997 metrics = metrics * .9997