Test Seaborn graph, and add timings

This commit is contained in:
Ruben 2018-04-29 15:09:15 +02:00
parent 1b049b9e5a
commit da7f57185b
2 changed files with 132 additions and 19 deletions

View File

@ -7,9 +7,19 @@ import os
import pickle
import logging
from scipy.ndimage.filters import gaussian_filter
import Tkinter
from PIL import Image, ImageDraw,ImageTk
import pandas as pd
import seaborn as sns
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import sys
if sys.version_info[0] < 3:
import Tkinter as Tk
else:
import tkinter as Tk
import time
logging.basicConfig( format='%(asctime)-15s %(name)s %(levelname)s: %(message)s' )
logger = logging.getLogger(__name__)
@ -27,6 +37,7 @@ screenDrawCorners = np.array([[10,60], [90, 60], [10, 110], [90, 110]])
# metrics matrix
metricsSize = [1920,1080]
dataframe = pd.DataFrame(columns=['x','y'])
metrics = np.zeros(metricsSize)
screenDrawCorners = np.array([[0,0], [1919,0], [0, 1079], [1919,1079]])
@ -146,22 +157,37 @@ else:
coordinates = {'tl': None, 'tr': None, 'bl': None, 'br': None}
transform = None
windowRoot = Tkinter.Tk()
windowRoot = Tk.Toplevel()
windowSize = (1000,1000)
windowRoot.geometry('%dx%d+%d+%d' % (windowSize[0],windowSize[1],0,0))
canvas = Tkinter.Canvas(windowRoot,width=1000,height=1000)
canvas.pack()
figure = Figure(figsize=(16, 9), dpi=100)
axes = figure.add_subplot(111)
axes.set_title('Tk embedding')
axes.set_xlabel('X axis label')
axes.set_ylabel('Y label')
# canvas = Tk.Canvas(windowRoot,width=1000,height=1000)
canvas = FigureCanvasTkAgg(figure,master=windowRoot)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
imageWindowRoot = Tk.Toplevel()
imageWindowSize = (1000,1000)
imageWindowRoot.geometry('%dx%d+%d+%d' % (imageWindowSize[0],imageWindowSize[1],0,0))
imageCanvas = Tk.Canvas(imageWindowRoot,width=1000,height=1000)
while True:
t1 = time.time()
_, im = c.read()
size = im.shape
t2 = time.time()
logger.debug("Captured frame in %fs", t2-t1)
# Docs: Ask the detector to find the bounding boxes of each face. The 1 in the
# second argument indicates that we should upsample the image 1 time. This
# will make everything bigger and allow us to detect more faces.
dets = detector(im, 1)
logger.debug("Number of faces detected: {}".format(len(dets)))
t3 = time.time()
logger.debug("Number of faces detected: {} - took {}s".format(len(dets), t3-t2))
# We use this later for calibrating
currentPoint = None
@ -170,7 +196,10 @@ while True:
if len(dets) > 0:
for d in dets:
td1 = time.time()
shape = predictor(im, d)
td2 = time.time()
logger.debug("Found face points in %fs", td2-td1)
#2D image points. If you change the image, you need to change vector
image_points = np.array([
@ -280,10 +309,13 @@ while True:
currentPoint = point
currentPoints.append(point)
td3 = time.time()
logger.debug("Timer: All other face drawing stuff in %fs", td3-td2)
# TODO only draw nose line now, so we can change color depending whether on screen or not
# processed all faces, now draw on screen:
te1 = time.time()
# draw little floorplan for 10 -> 50, sideplan 60 -> 100 (40x40 px)
cv2.rectangle(im, (9, 9), (51, 51), (255,255,255), 1)
cv2.rectangle(im, (59, 9), (101, 51), (255,255,255), 1)
@ -298,8 +330,14 @@ while True:
cv2.putText(im, "2", (85,70), cv2.FONT_HERSHEY_PLAIN, .7, (255,255,255) if coordinates['tr'] is not None else (0,0,255))
cv2.putText(im, "3", (10,110), cv2.FONT_HERSHEY_PLAIN, .7, (255,255,255) if coordinates['bl'] is not None else (0,0,255))
cv2.putText(im, "4", (85,110), cv2.FONT_HERSHEY_PLAIN, .7, (255,255,255) if coordinates['br'] is not None else (0,0,255))
tm1 = 0
tm2 = 0
tm3 = 0
tm4 = 0
else:
tm1 = time.time()
newMetrics = np.zeros(metricsSize)
tm2 = time.time()
for point in currentPoints:
# check if within coordinates:
# dot1 = np.dot(coordinates['tl'] - point, coordinates['tl'] - coordinates['br'])
@ -316,33 +354,53 @@ while True:
targetInt = (int(targetPoint[0]), int(targetPoint[1]))
# check if point fits on screen:
# if so, measure it
if targetInt[0] >= 0 and targetInt[1] >= 0 and targetInt[0] < metrics.shape[0] and targetInt[1] < metrics.shape[1]:
if targetInt[0] >= 0 and targetInt[1] >= 0 and targetInt[0] < metricsSize[0] and targetInt[1] < metricsSize[1]:
dataframe = dataframe.append({'x':targetInt[0],'y':targetInt[1]}, ignore_index=True)
newMetrics[targetInt[0],targetInt[1]] += 1
# after we collected all new metrics, blur them foor smoothness
# and add to all metrics collected
tm3 = time.time()
metrics = metrics + gaussian_filter(newMetrics, sigma = 8)
tm4 = time.time()
logger.debug("Updated matrix with blur in %f", tm4 - tm3 + tm2 - tm1)
# Display webcam image with overlays
te2 = time.time()
logger.debug("Drew on screen in %fs", te2-te1)
cv2.imshow("Output", im)
logger.debug("showed webcam image")
te3 = time.time()
logger.debug("showed webcam image in %fs", te3-te2)
# blur smooth the heatmap
logger.debug("Max blurred metrics: %f", np.max(metrics))
# logger.debug("Max blurred metrics: %f", np.max(metrics))
# update the heatmap output
tm21 = time.time()
normalisedMetrics = metrics / (np.max(metrics)/255)
tm22 = time.time()
logger.debug("Max normalised metrics: %f", np.max(normalisedMetrics))
print(normalisedMetrics)
# print(normalisedMetrics)
tm23 = time.time()
image = Image.fromarray(normalisedMetrics)
wpercent = (windowSize[0] / float(image.size[0]))
wpercent = (imageWindowSize[0] / float(image.size[0]))
hsize = int((float(image.size[1]) * float(wpercent)))
image = image.resize((windowSize[0], hsize))
image = image.resize((imageWindowSize[0], hsize))
tkpi = ImageTk.PhotoImage(image)
canvas.delete("IMG")
imagesprite = canvas.create_image(500,500,image=tkpi, tags="IMG")
imageCanvas.delete("IMG")
imagesprite = imageCanvas.create_image(500,500,image=tkpi, tags="IMG")
imageWindowRoot.update()
tm24 = time.time()
logger.debug("PIL iamge generated in %fs", tm24 - tm23)
logger.debug("Total matrix time is %fs", tm4 - tm3 + tm2 - tm1 + tm24 - tm21)
te4 = time.time()
axes.clear()
if(len(dataframe) > 2):
g = sns.kdeplot(dataframe['x'], dataframe['y'],ax=axes, n_levels=30, shade=True, cmap="rainbow")
canvas.draw()
windowRoot.update()
logger.debug("updated window")
te5 = time.time()
logger.debug("Drew graph & updated window in %fs", te5-te4)
# (optionally) very slowly fade out previous metrics:
# metrics = metrics * .999

57
test.py
View File

@ -1,6 +1,18 @@
import helpers
import numpy as np
import pickle
import random
from PIL import Image, ImageDraw,ImageTk
import pandas as pd
import seaborn as sns
import time
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import sys
if sys.version_info[0] < 3:
import Tkinter as Tk
else:
import tkinter as Tk
screenDrawCorners = np.array([[10,60], [90, 60], [10, 110], [90, 110]])
coordinates = pickle.load(open("coordinates.p", "rb"))
@ -17,4 +29,47 @@ print("Transformed point %s", transform(a))
print("Test point %s", midpointTop )
print("Transformed point %s", transform(midpointTop))
print("Test point %s", midpointCenter )
print("Transformed point %s", transform(midpointCenter))
print("Transformed point %s", transform(midpointCenter))
windowRoot = Tk.Tk()
windowSize = (1000,1000)
windowRoot.geometry('%dx%d+%d+%d' % (windowSize[0],windowSize[1],0,0))
figure = Figure(figsize=(16, 9), dpi=100)
t = np.arange(0.0, 3.0, 0.01)
s = np.sin(2*np.pi*t)
d = {'col1': [1, 2,4], 'col2': [3, 4,9]}
dataframe = pd.DataFrame(data=d)
print(dataframe['col1'])
a = figure.add_subplot(111)
# a = sns.jointplot(x="col1", y="col2", data=dataframe, kind="kde")
# g = sns.jointplot(x="col1", y="col2", data=dataframe, kind="kde")
# g = sns.JointGrid(x="col1", y="col2", data=dataframe)
a.set_title('Tk embedding')
a.set_xlabel('X axis label')
a.set_ylabel('Y label')
# canvas = Tk.Canvas(windowRoot,width=1000,height=1000)
canvas = FigureCanvasTkAgg(figure,master=windowRoot)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
for b in range(0,1000):
dataframe = dataframe.append({'col1':b,'col2':random.random()*100}, ignore_index=True)
a.clear()
# g.fig =
# a.plot(t*b, s)
# g = sns.jointplot(x="col1", y="col2", data=dataframe, kind="kde", size=1000)
# g = sns.kdeplot(dataframe['col1'], dataframe['col2'],ax=a, n_levels=30, shade=True, cmap="hsv")
g = sns.kdeplot(dataframe['col1'], dataframe['col2'],ax=a, n_levels=30, shade=True, cmap="rainbow")
# print(g, g.fig)
# g = g.plot_joint(sns.kdeplot, cmap="Blues_d")
# print(g.fig)
# canvas.figure = g.figure
canvas.draw()
windowRoot.update()
time.sleep(1)
# Tk.mainloop()