Home > front end >  Bokeh Hover Showing ??? instead of Date and Time
Bokeh Hover Showing ??? instead of Date and Time

Time:07-05

I have read lots of questions and answers here and can not seem to fix my issue. I can not get the Hover tool in Bokeh to display the Date and Time correctly, I get ???, I understand this to mean that the Hover cannot read the column in my Dataframe.

I have implemented a lot of fixes and I think what I have here should work.

Here is my code:

This is the code for the data collection:

import cv2 as cv
from datetime import datetime
import pandas as pd

background = None
status_list = [None, None]
times = []
df = pd.DataFrame(columns = ['Start', 'End'])

video = cv.VideoCapture(0, cv.CAP_DSHOW)

while True:
    check, frame = video.read()

    status = 0

    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    gray = cv.GaussianBlur(gray, (21, 21), 0)

    if background is None:
        background = gray
        continue 

    delta_frame = cv.absdiff(background, gray)
    thresh_delta = cv.threshold(delta_frame, 40, 255, cv.THRESH_BINARY)[1]
    thresh_delta = cv.dilate(thresh_delta, None, iterations = 2)
                                            
    (cnts, _) = cv.findContours(thresh_delta.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
                                                       
    for contour in cnts:
        if cv.contourArea(contour) < 2000:
            continue

        status = 1

        (x, y, w, h) = cv.boundingRect(contour)
        cv.rectangle(frame, (x, y), (x   w, y   h), (0, 255, 0), 3)
    
    status_list.append(status)

    status_list = status_list[-2:]

    if status_list[-1] == 1 and status_list[-2] == 0:
        times.append(datetime.now())
    if status_list[-1] == 0 and status_list[-2] == 1:
        times.append(datetime.now())


    cv.imshow('Delta', delta_frame)
    cv.imshow('Threshold Frame', thresh_delta)
    cv.imshow('Colour Frame', frame)

    key = cv.waitKey(1)

    if key == ord('q'):
        if status == 1:
            times.append(datetime.now())
        break

for i in range(0, len(times), 2):
    df = df.append({'Start' :times[i], 'End' :times[i   1]}, ignore_index = True)


video.release()

cv.destroyAllWindows()

And here is the code for the Bokeh graph:

%run Motion_Detection_Plotting.ipynb

from bokeh.plotting import figure, show, output_file
from bokeh.models import HoverTool, ColumnDataSource, formatters, TickFormatter



df['Start'] = pd.to_datetime(df.Start, format='%Y-%m-%D %H:%M:%S')
df['End'] = pd.to_datetime(df.End, format='%Y-%m-%D %H:%M:%S')

Source=ColumnDataSource(df.Start, df.End)

p = figure(x_axis_type = 'datetime', height = 200, width = 1000, title = 'Motion Graph', sizing_mode = 'stretch_width')

p.yaxis.minor_tick_line_color = None
p.yaxis.major_tick_in = 0
p.xaxis.major_tick_in = 0
p.yaxis.ticker = [0, 1]
p.ygrid.grid_line_color = None

hover = HoverTool(tooltips=[('Start', '@Start{%D/%m %H:%M:%S}'), ('End','@End{%D/%m %H:%M:%S}')],
    formatters={'@Start':'datetime', '@End':'datetime'})
p.add_tools(hover)

q = p.quad(left = df['Start'], right = df['End'], bottom = 0, top = 1, color = 'green')

output_file('graph2.html')
show(p)

And here is my outputted graph

working HoverTool

  • Related