-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathliveGraph.py
More file actions
86 lines (64 loc) · 2.72 KB
/
liveGraph.py
File metadata and controls
86 lines (64 loc) · 2.72 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import cv2 as cv
import numpy as np
import threading
class plane(threading.Thread):
def __init__(self, *args, width: int, height: int, title: str,
bgcolor=(0, 0, 0), textcolor=(0, 150, 0),
scale=15, **kwags):
super().__init__(*args, **kwags)
self.title = title
self.textcolor = textcolor
self.scale = scale
margin = 35
self.x_plots = [margin+(i*(width//scale)) for i in range(scale)]
self.y_plots = [0 for i in range(scale)]
self.margin = margin
height = height+(2*margin)
width = width+(2*margin)
mask = np.ones([height, width, 3], dtype="uint8")
mask[:,:] = bgcolor
b, g, r = textcolor
lncolor = (b//2, g//2, r//2)
for x in range(margin, mask.shape[1]-margin, (mask.shape[1]-margin*2)//scale):
cv.line(mask, (x, margin), (x, mask.shape[0]-margin), lncolor, 1)
for y in range(mask.shape[0]-margin, margin, -(mask.shape[1]-margin*2)//scale):
cv.line(mask, (margin, y), (mask.shape[1]-margin, y), lncolor, 1)
self.mask = mask
self.maxm = 0
self.minm = 0
def plot(self, value):
self.y_plots.pop(0)
self.y_plots.append(value)
y_plots = self.y_plots
mask = self.mask
margin = self.margin
height = mask.shape[0]-(margin*2)
if max(y_plots)-min(y_plots) > self.maxm-self.minm:
self.maxm = max(y_plots)
self.minm = min(y_plots)
diff = self.maxm-self.minm
try:
y_plots = [(margin+height)-int((i/diff)*height) for i in y_plots]
except ZeroDivisionError:
y_plots = [margin+height//2 for i in y_plots]
x_plots = self.x_plots
frame = np.zeros(mask.shape, dtype="uint8")
frame[:,:] = mask[0]
for i in range(len(y_plots)-1):
cv.line(frame, (x_plots[i], y_plots[i]), (x_plots[i+1], y_plots[i+1]), self.textcolor, 1)
tcolor = (255, 255, 255)
cv.putText(frame, str(self.maxm), (mask.shape[1]-margin*2, margin), cv.FONT_HERSHEY_PLAIN, 1, tcolor, 1)
cv.putText(frame, str(self.minm), (mask.shape[1]-margin*2, mask.shape[0]-margin), cv.FONT_HERSHEY_PLAIN, 1, tcolor, 1)
cv.putText(frame, "period", (mask.shape[1]//2, mask.shape[0]-5), 4, .75, tcolor, 1)
self.frame = cv.bitwise_or(mask, frame)
def run(self):
self.plot(0)
title = self.title
cv.namedWindow(title)
cv.moveWindow(title, 0, 0)
while True:
cv.imshow(title, self.frame)
if cv.waitKey(1) &0xff == ord("q"):
break
cv.destroyAllWindows()
print("exiting live graphing")