Commit 35534c45 authored by Vikrant Rajan's avatar Vikrant Rajan
Browse files

Bokeh Motion Detection

parent c70d9a5c
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bokeh Plot</title>
<script type="text/javascript" src="https://cdn.pydata.org/bokeh/release/bokeh-1.4.0.min.js"></script>
<script type="text/javascript">
Bokeh.set_log_level("info");
</script>
</head>
<body>
<div class="bk-root" id="b03d2ed8-a7a1-499f-aa5f-c22c979ff227" data-root-id="1001"></div>
<script type="application/json" id="1282">
{"149cb75b-76f2-4300-b404-e01539db6719":{"roots":{"references":[{"attributes":{"mantissas":[1,2,5],"max_interval":500.0,"num_minor_ticks":0},"id":"1047","type":"AdaptiveTicker"},{"attributes":{"base":60,"mantissas":[1,2,5,10,15,20,30],"max_interval":1800000.0,"min_interval":1000.0,"num_minor_ticks":0},"id":"1048","type":"AdaptiveTicker"},{"attributes":{"days":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]},"id":"1050","type":"DaysTicker"},{"attributes":{"base":24,"mantissas":[1,2,4,6,8,12],"max_interval":43200000.0,"min_interval":3600000.0,"num_minor_ticks":0},"id":"1049","type":"AdaptiveTicker"},{"attributes":{"months":[0,1,2,3,4,5,6,7,8,9,10,11]},"id":"1054","type":"MonthsTicker"},{"attributes":{"months":[0,2,4,6,8,10]},"id":"1055","type":"MonthsTicker"},{"attributes":{"days":[1,4,7,10,13,16,19,22,25,28]},"id":"1051","type":"DaysTicker"},{"attributes":{"days":[1,15]},"id":"1053","type":"DaysTicker"},{"attributes":{"months":[0,6]},"id":"1057","type":"MonthsTicker"},{"attributes":{"formatter":{"id":"1043","type":"BasicTickFormatter"},"minor_tick_line_color":{"value":null},"ticker":{"id":"1018","type":"BasicTicker"}},"id":"1017","type":"LinearAxis"},{"attributes":{"days":[1,8,15,22]},"id":"1052","type":"DaysTicker"},{"attributes":{"desired_num_ticks":1},"id":"1018","type":"BasicTicker"},{"attributes":{},"id":"1058","type":"YearsTicker"},{"attributes":{},"id":"1008","type":"LinearScale"},{"attributes":{"ticker":{"id":"1013","type":"DatetimeTicker"}},"id":"1016","type":"Grid"},{"attributes":{"callback":null},"id":"1004","type":"DataRange1d"},{"attributes":{"months":[0,4,8]},"id":"1056","type":"MonthsTicker"},{"attributes":{"callback":null,"tooltips":[["Start: ","@Start"],["End: ","@End"]]},"id":"1035","type":"HoverTool"},{"attributes":{"dimension":1,"ticker":{"id":"1018","type":"BasicTicker"}},"id":"1021","type":"Grid"},{"attributes":{"overlay":{"id":"1061","type":"BoxAnnotation"}},"id":"1024","type":"BoxZoomTool"},{"attributes":{"num_minor_ticks":5,"tickers":[{"id":"1047","type":"AdaptiveTicker"},{"id":"1048","type":"AdaptiveTicker"},{"id":"1049","type":"AdaptiveTicker"},{"id":"1050","type":"DaysTicker"},{"id":"1051","type":"DaysTicker"},{"id":"1052","type":"DaysTicker"},{"id":"1053","type":"DaysTicker"},{"id":"1054","type":"MonthsTicker"},{"id":"1055","type":"MonthsTicker"},{"id":"1056","type":"MonthsTicker"},{"id":"1057","type":"MonthsTicker"},{"id":"1058","type":"YearsTicker"}]},"id":"1013","type":"DatetimeTicker"},{"attributes":{},"id":"1059","type":"UnionRenderers"},{"attributes":{},"id":"1010","type":"LinearScale"},{"attributes":{"bottom":{"value":0},"fill_alpha":{"value":0.1},"fill_color":{"value":"#1f77b4"},"left":{"field":"left"},"line_alpha":{"value":0.1},"line_color":{"value":"#1f77b4"},"right":{"field":"right"},"top":{"value":1}},"id":"1039","type":"Quad"},{"attributes":{},"id":"1060","type":"Selection"},{"attributes":{},"id":"1027","type":"HelpTool"},{"attributes":{"active_drag":"auto","active_inspect":"auto","active_multi":null,"active_scroll":"auto","active_tap":"auto","tools":[{"id":"1022","type":"PanTool"},{"id":"1023","type":"WheelZoomTool"},{"id":"1024","type":"BoxZoomTool"},{"id":"1025","type":"SaveTool"},{"id":"1026","type":"ResetTool"},{"id":"1027","type":"HelpTool"},{"id":"1035","type":"HoverTool"}]},"id":"1028","type":"Toolbar"},{"attributes":{"formatter":{"id":"1045","type":"DatetimeTickFormatter"},"ticker":{"id":"1013","type":"DatetimeTicker"}},"id":"1012","type":"DatetimeAxis"},{"attributes":{},"id":"1022","type":"PanTool"},{"attributes":{"text":"Motion Graph"},"id":"1002","type":"Title"},{"attributes":{"callback":null},"id":"1006","type":"DataRange1d"},{"attributes":{},"id":"1023","type":"WheelZoomTool"},{"attributes":{"below":[{"id":"1012","type":"DatetimeAxis"}],"center":[{"id":"1016","type":"Grid"},{"id":"1021","type":"Grid"}],"left":[{"id":"1017","type":"LinearAxis"}],"plot_height":300,"plot_width":1000,"renderers":[{"id":"1040","type":"GlyphRenderer"}],"title":{"id":"1002","type":"Title"},"toolbar":{"id":"1028","type":"Toolbar"},"x_range":{"id":"1004","type":"DataRange1d"},"x_scale":{"id":"1008","type":"LinearScale"},"y_range":{"id":"1006","type":"DataRange1d"},"y_scale":{"id":"1010","type":"LinearScale"}},"id":"1001","subtype":"Figure","type":"Plot"},{"attributes":{},"id":"1025","type":"SaveTool"},{"attributes":{},"id":"1026","type":"ResetTool"},{"attributes":{"callback":null,"data":{"left":{"__ndarray__":"5e5YM3P0dkLnGRg0c/R2Qg==","dtype":"float64","shape":[2]},"right":{"__ndarray__":"xbjoM3P0dkLhFpw1c/R2Qg==","dtype":"float64","shape":[2]}},"selected":{"id":"1060","type":"Selection"},"selection_policy":{"id":"1059","type":"UnionRenderers"}},"id":"1037","type":"ColumnDataSource"},{"attributes":{"bottom":{"value":0},"fill_color":{"value":"green"},"left":{"field":"left"},"line_color":{"value":"green"},"right":{"field":"right"},"top":{"value":1}},"id":"1038","type":"Quad"},{"attributes":{},"id":"1043","type":"BasicTickFormatter"},{"attributes":{"bottom_units":"screen","fill_alpha":{"value":0.5},"fill_color":{"value":"lightgrey"},"left_units":"screen","level":"overlay","line_alpha":{"value":1.0},"line_color":{"value":"black"},"line_dash":[4,4],"line_width":{"value":2},"render_mode":"css","right_units":"screen","top_units":"screen"},"id":"1061","type":"BoxAnnotation"},{"attributes":{"source":{"id":"1037","type":"ColumnDataSource"}},"id":"1041","type":"CDSView"},{"attributes":{},"id":"1045","type":"DatetimeTickFormatter"},{"attributes":{"data_source":{"id":"1037","type":"ColumnDataSource"},"glyph":{"id":"1038","type":"Quad"},"hover_glyph":null,"muted_glyph":null,"nonselection_glyph":{"id":"1039","type":"Quad"},"selection_glyph":null,"view":{"id":"1041","type":"CDSView"}},"id":"1040","type":"GlyphRenderer"}],"root_ids":["1001"]},"title":"Bokeh Application","version":"1.4.0"}}
</script>
<script type="text/javascript">
(function() {
var fn = function() {
Bokeh.safely(function() {
(function(root) {
function embed_document(root) {
var docs_json = document.getElementById('1282').textContent;
var render_items = [{"docid":"149cb75b-76f2-4300-b404-e01539db6719","roots":{"1001":"b03d2ed8-a7a1-499f-aa5f-c22c979ff227"}}];
root.Bokeh.embed.embed_items(docs_json, render_items);
}
if (root.Bokeh !== undefined) {
embed_document(root);
} else {
var attempts = 0;
var timer = setInterval(function(root) {
if (root.Bokeh !== undefined) {
clearInterval(timer);
embed_document(root);
} else {
attempts++;
if (attempts > 100) {
clearInterval(timer);
console.log("Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing");
}
}
}, 10, root)
}
})(window);
});
};
if (document.readyState != "loading") fn();
else document.addEventListener("DOMContentLoaded", fn);
})();
</script>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bokeh Plot</title>
<script type="text/javascript" src="https://cdn.pydata.org/bokeh/release/bokeh-1.4.0.min.js"></script>
<script type="text/javascript">
Bokeh.set_log_level("info");
</script>
</head>
<body>
<div class="bk-root" id="c856ed81-fc51-4993-a543-b1953e0f27f1" data-root-id="1002"></div>
<script type="application/json" id="1283">
{"1e600da5-6854-4e5d-8c7a-fbc2592df22a":{"roots":{"references":[{"attributes":{"days":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]},"id":"1051","type":"DaysTicker"},{"attributes":{"mantissas":[1,2,5],"max_interval":500.0,"num_minor_ticks":0},"id":"1048","type":"AdaptiveTicker"},{"attributes":{"base":24,"mantissas":[1,2,4,6,8,12],"max_interval":43200000.0,"min_interval":3600000.0,"num_minor_ticks":0},"id":"1050","type":"AdaptiveTicker"},{"attributes":{"months":[0,2,4,6,8,10]},"id":"1056","type":"MonthsTicker"},{"attributes":{"days":[1,4,7,10,13,16,19,22,25,28]},"id":"1052","type":"DaysTicker"},{"attributes":{"months":[0,1,2,3,4,5,6,7,8,9,10,11]},"id":"1055","type":"MonthsTicker"},{"attributes":{"days":[1,15]},"id":"1054","type":"DaysTicker"},{"attributes":{"months":[0,6]},"id":"1058","type":"MonthsTicker"},{"attributes":{"days":[1,8,15,22]},"id":"1053","type":"DaysTicker"},{"attributes":{},"id":"1059","type":"YearsTicker"},{"attributes":{"formatter":{"id":"1047","type":"BasicTickFormatter"},"minor_tick_line_color":{"value":null},"ticker":{"id":"1019","type":"BasicTicker"}},"id":"1018","type":"LinearAxis"},{"attributes":{"months":[0,4,8]},"id":"1057","type":"MonthsTicker"},{"attributes":{"callback":null},"id":"1005","type":"DataRange1d"},{"attributes":{},"id":"1045","type":"DatetimeTickFormatter"},{"attributes":{"num_minor_ticks":5,"tickers":[{"id":"1048","type":"AdaptiveTicker"},{"id":"1049","type":"AdaptiveTicker"},{"id":"1050","type":"AdaptiveTicker"},{"id":"1051","type":"DaysTicker"},{"id":"1052","type":"DaysTicker"},{"id":"1053","type":"DaysTicker"},{"id":"1054","type":"DaysTicker"},{"id":"1055","type":"MonthsTicker"},{"id":"1056","type":"MonthsTicker"},{"id":"1057","type":"MonthsTicker"},{"id":"1058","type":"MonthsTicker"},{"id":"1059","type":"YearsTicker"}]},"id":"1014","type":"DatetimeTicker"},{"attributes":{"ticker":{"id":"1014","type":"DatetimeTicker"}},"id":"1017","type":"Grid"},{"attributes":{"desired_num_ticks":1},"id":"1019","type":"BasicTicker"},{"attributes":{"dimension":1,"ticker":{"id":"1019","type":"BasicTicker"}},"id":"1022","type":"Grid"},{"attributes":{},"id":"1011","type":"LinearScale"},{"attributes":{"overlay":{"id":"1060","type":"BoxAnnotation"}},"id":"1025","type":"BoxZoomTool"},{"attributes":{"bottom":{"value":0},"fill_color":{"value":"green"},"left":{"field":"Start"},"line_color":{"value":"green"},"right":{"field":"End"},"top":{"value":1}},"id":"1039","type":"Quad"},{"attributes":{"formatter":{"id":"1045","type":"DatetimeTickFormatter"},"ticker":{"id":"1014","type":"DatetimeTicker"}},"id":"1013","type":"DatetimeAxis"},{"attributes":{},"id":"1028","type":"HelpTool"},{"attributes":{"bottom_units":"screen","fill_alpha":{"value":0.5},"fill_color":{"value":"lightgrey"},"left_units":"screen","level":"overlay","line_alpha":{"value":1.0},"line_color":{"value":"black"},"line_dash":[4,4],"line_width":{"value":2},"render_mode":"css","right_units":"screen","top_units":"screen"},"id":"1060","type":"BoxAnnotation"},{"attributes":{},"id":"1009","type":"LinearScale"},{"attributes":{"active_drag":"auto","active_inspect":"auto","active_multi":null,"active_scroll":"auto","active_tap":"auto","tools":[{"id":"1023","type":"PanTool"},{"id":"1024","type":"WheelZoomTool"},{"id":"1025","type":"BoxZoomTool"},{"id":"1026","type":"SaveTool"},{"id":"1027","type":"ResetTool"},{"id":"1028","type":"HelpTool"},{"id":"1036","type":"HoverTool"}]},"id":"1029","type":"Toolbar"},{"attributes":{},"id":"1023","type":"PanTool"},{"attributes":{},"id":"1047","type":"BasicTickFormatter"},{"attributes":{"callback":null},"id":"1007","type":"DataRange1d"},{"attributes":{},"id":"1024","type":"WheelZoomTool"},{"attributes":{"text":"Motion Graph"},"id":"1003","type":"Title"},{"attributes":{"callback":null,"tooltips":[["Start","@Start_string"],["End","@End_string"]]},"id":"1036","type":"HoverTool"},{"attributes":{},"id":"1026","type":"SaveTool"},{"attributes":{},"id":"1027","type":"ResetTool"},{"attributes":{"base":60,"mantissas":[1,2,5,10,15,20,30],"max_interval":1800000.0,"min_interval":1000.0,"num_minor_ticks":0},"id":"1049","type":"AdaptiveTicker"},{"attributes":{"source":{"id":"1001","type":"ColumnDataSource"}},"id":"1042","type":"CDSView"},{"attributes":{"bottom":{"value":0},"fill_alpha":{"value":0.1},"fill_color":{"value":"#1f77b4"},"left":{"field":"Start"},"line_alpha":{"value":0.1},"line_color":{"value":"#1f77b4"},"right":{"field":"End"},"top":{"value":1}},"id":"1040","type":"Quad"},{"attributes":{"data_source":{"id":"1001","type":"ColumnDataSource"},"glyph":{"id":"1039","type":"Quad"},"hover_glyph":null,"muted_glyph":null,"nonselection_glyph":{"id":"1040","type":"Quad"},"selection_glyph":null,"view":{"id":"1042","type":"CDSView"}},"id":"1041","type":"GlyphRenderer"},{"attributes":{},"id":"1061","type":"UnionRenderers"},{"attributes":{"callback":null,"data":{"End":{"__ndarray__":"0xn4m3X0dkI=","dtype":"float64","shape":[1]},"End_string":["2019-12-27 12:34:21"],"Start":{"__ndarray__":"iasXm3X0dkI=","dtype":"float64","shape":[1]},"Start_string":["2019-12-27 12:34:18"],"index":[0]},"selected":{"id":"1062","type":"Selection"},"selection_policy":{"id":"1061","type":"UnionRenderers"}},"id":"1001","type":"ColumnDataSource"},{"attributes":{},"id":"1062","type":"Selection"},{"attributes":{"below":[{"id":"1013","type":"DatetimeAxis"}],"center":[{"id":"1017","type":"Grid"},{"id":"1022","type":"Grid"}],"left":[{"id":"1018","type":"LinearAxis"}],"plot_height":300,"plot_width":1000,"renderers":[{"id":"1041","type":"GlyphRenderer"}],"title":{"id":"1003","type":"Title"},"toolbar":{"id":"1029","type":"Toolbar"},"x_range":{"id":"1005","type":"DataRange1d"},"x_scale":{"id":"1009","type":"LinearScale"},"y_range":{"id":"1007","type":"DataRange1d"},"y_scale":{"id":"1011","type":"LinearScale"}},"id":"1002","subtype":"Figure","type":"Plot"}],"root_ids":["1002"]},"title":"Bokeh Application","version":"1.4.0"}}
</script>
<script type="text/javascript">
(function() {
var fn = function() {
Bokeh.safely(function() {
(function(root) {
function embed_document(root) {
var docs_json = document.getElementById('1283').textContent;
var render_items = [{"docid":"1e600da5-6854-4e5d-8c7a-fbc2592df22a","roots":{"1002":"c856ed81-fc51-4993-a543-b1953e0f27f1"}}];
root.Bokeh.embed.embed_items(docs_json, render_items);
}
if (root.Bokeh !== undefined) {
embed_document(root);
} else {
var attempts = 0;
var timer = setInterval(function(root) {
if (root.Bokeh !== undefined) {
clearInterval(timer);
embed_document(root);
} else {
attempts++;
if (attempts > 100) {
clearInterval(timer);
console.log("Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing");
}
}
}, 10, root)
}
})(window);
});
};
if (document.readyState != "loading") fn();
else document.addEventListener("DOMContentLoaded", fn);
})();
</script>
</body>
</html>
\ No newline at end of file
,Start,End
0,2019-12-27 12:34:18.106721,2019-12-27 12:34:21.697614
"""
MOTION DETECTION APPLICATION
"""
import cv2, time, pandas
from datetime import datetime
first_frame = None
status_list = [None, None]
times = []
df = pandas.DataFrame(columns=["Start", "End"])
video = cv2.VideoCapture(0)
while True:
check, frame = video.read()
# 0 = nothing is moving, 1 = motion detected
status = 0
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
if first_frame is None:
first_frame = gray
continue
delta_frame = cv2.absdiff(first_frame, gray)
thresh_frame = cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1]
thresh_frame = cv2.dilate(thresh_frame, None, iterations=2)
(cnts,__) = cv2.findContours(thresh_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in cnts:
# checking the area of the objects in camera
if cv2.contourArea(contour) < 10000:
continue
status = 1
(x, y, w, h) = cv2.boundingRect(contour)
cv2.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())
cv2.imshow("Gray Frame", gray)
cv2.imshow("Delta Frame",delta_frame)
cv2.imshow("Threshold Frame",thresh_frame)
cv2.imshow("Color Frame", frame)
key = cv2.waitKey(1)
if key == ord('q'):
if status == 1:
times.append(datetime.now())
break
print(status_list)
print(times)
for i in range(0, len(times), 2):
df = df.append({"Start":times[i], "End": times[i+1]}, ignore_index=True)
df.to_csv("Times.csv")
video.release()
cv2.destroyAllWindows
\ No newline at end of file
from capture import df
from bokeh.plotting import figure, show, output_file
from bokeh.models import HoverTool, ColumnDataSource
df["Start_string"] = df["Start"].dt.strftime("%Y-%m-%d %H:%M:%S")
df["End_string"] = df["End"].dt.strftime("%Y-%m-%d %H:%M:%S")
cds = ColumnDataSource(df)
p = figure(x_axis_type = "datetime", height = 300, width = 1000, title = "Motion Graph")
p.yaxis.minor_tick_line_color = None
p.ygrid[0].ticker.desired_num_ticks = 1
hover = HoverTool(tooltips = [("Start", "@Start_string"),("End", "@End_string")])
p.add_tools(hover)
q = p.quad(left="Start", right = "End", bottom = 0, top = 1, color = "green", source = cds)
output_file("Graph1.html")
show(p)
\ No newline at end of file
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:code id: tags:
``` python
# Making a basic Bokeh Line Graph
from bokeh.plotting import figure
from bokeh.io import output_file, show
# getting some data
x = [3, 4, 5]
y = [6, 2, 7]
# creating an output file
output_file("datavisualization.html")
# creating a figure object
f = figure()
# creating a line plot with ^ data above ^
f.line(x, y)
show(f)
```
%% Cell type:code id: tags:
``` python
# Making a basic Bokeh Line Graph
from bokeh.plotting import figure
from bokeh.io import output_file, show
import pandas
# getting some data
df = pandas.read_csv("data.csv")
x = df["x"]
y = df["y"]