Files
Flask_scripts/germany/make4axelsgraph.py

157 lines
6.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import cv2
import numpy as np
datadir = "log"
logid = "2"
log = "/home/shurik/SmartBed/src/%s/sb_%s.log" % (datadir, logid)
axels4graph=[4, 7, 14, 37, 18]
maxaxels4graphcnt = 4
axels4graphcnt = min(maxaxels4graphcnt, len(axels4graph))
#print(axels4graphcnt)
scale = 5 # увеличение сигнала на графике
log_lines = 15
axel_cnt = 39 # количество акселерометров
steps = 10 # количество измерений в одной строке
pdCnt = 39 # кол-во датчиков давления
data_delta = 1 # пропускаем первое значение - таймер
pd_delta = steps * axel_cnt * 3 + data_delta
input_frm_rates = 17
log_recs_delta = 1.5 # интервал между записями в логе
pd = []
ax = [[[],[],[]] for n in range(axel_cnt)]
times = []
cc=0
# read data from system pipe
from subprocess import Popen, PIPE
databuf = Popen(["tail", "-n%i" % log_lines, log], stdout=PIPE, encoding='utf-8').communicate()[0]
#print(log)
#print("databuf:\n", databuf)
lines = [line.rstrip() for line in databuf.split("\n")]
for l in lines:
if len(l) <=1 :
break
times.append(l.split("\t")[0])
args = l.split("\t")[1].split(",")
#pd.append([int(args[pd_delta]), int(args[pd_delta+1]), int(args[pd_delta+2]), int(args[pd_delta+3]), int(args[pd_delta+4])])
for sn in range(steps):
dlt = sn * 3 * axel_cnt
for i in range(axel_cnt):
# если уберут первое значение (метку таймера), то прибавку надо начинать не с 1, а с 0
ax[i][0].append(int(args[dlt + i*3+1]) if int(args[dlt + i*3+1]) < 32768 else int(args[dlt + i*3+1]) - 65536)
ax[i][1].append(int(args[dlt + i*3+2]) if int(args[dlt + i*3+2]) < 32768 else int(args[dlt + i*3+2]) - 65536)
ax[i][2].append(int(args[dlt + i*3+3])-280 if int(args[dlt + i*3+3]) < 32768 else int(args[dlt + i*3+3]) - 65536)
cc+=1
#if cc == 2:
# print(ax)
ax_shape = np.shape(ax)
axMax = [[0, 0, 0] for n in range(axel_cnt)]
axMin = [[0, 0, 0] for n in range(axel_cnt)]
deltaMax = 0
for ai in range(axel_cnt):
for i in range(3):
axMax[ai][i] = max(ax[ai][i])
axMin[ai][i] = min(ax[ai][i])
if axMax[ai][i] == axMin[ai][i]:
axMax[ai][i] += 1
deltaMax = (axMax[ai][i] - axMin[ai][i]) if (axMax[ai][i] - axMin[ai][i]) > deltaMax else deltaMax
a1,a2,glen = np.shape(ax)
imH, imW = [1080, 1920]
image_shape = (imH, imW, 3)
image = np.zeros(image_shape, dtype=np.uint8)
gpoints = ax_shape[2]
gphalf = gpoints / 2
gdelta = 20 # смещение графика от левого/правого края
hStep = (imH - 10) / (4 / 2) - 10
wStep = (imW - gdelta*2) / gpoints /2
pdHStep = (imH - 40) / pdCnt
hpix = 2 # int(65000 / hStep) # вес одного вертикального пикселя
isClosed = False
c0 = (255, 0, 0)
c1 = (0, 255, 0)
c2 = (0, 0, 255)
thickness = 2
vline = [[imW/2, 20], [imW/2, imH-20]]
vline = np.array(vline, np.int32).reshape((-1, 1, 2))
hline = [[20, imH/2], [imW-20, imH/2]]
hline = np.array(hline, np.int32).reshape((-1, 1, 2))
radius = 20
color = [(0, 0, 225), (225, 225, 255)]
aiH = [1, 1, 0, 0]
aiW = [0, 1, 0, 1]
cnt = 0
fc = 0
pdcnt = 0
ret_val = True
printflag=True
gstep = 5
dsize = 20 # размер анализируемого на движение участка
msize = 3 # meter size / интервал измерения от текущей точки назад
body = ["nogi", "ruki", "telo"]
for istep in range(1):
image *= 0
image -=1
acnt = int(cnt / input_frm_rates * (steps / log_recs_delta) )
pdcnt = int(acnt/10)
dstart = acnt-dsize if acnt> dsize else 0
mstart = acnt-msize if acnt> msize else 0
gstart = 0 # int((acnt-gphalf) if (acnt>gphalf) else 0)
gstop = glen #int((acnt+gphalf) if (acnt<(glen-gphalf)) else glen)
chartlen = int(gstop - gstart)
ldelta = int(gdelta) #int(gdelta if (gstart>0) else gdelta + (gphalf - acnt)*wStep)
ax4ch = [[[],[],[]] for n in range(axels4graphcnt)]
j=0
for i in range(gstart, gstop):
Wval = [int(ldelta + wStep*j) , int(imW/2+ldelta + wStep*j)]
for ai in range(axels4graphcnt):
#print(ai)
ax4ch[ai][0].append([Wval[aiW[ai]], int((ax[axels4graph[ai]][0][i]*scale+80)/hpix + 10 + aiH[ai]*hStep+150)])
ax4ch[ai][1].append([Wval[aiW[ai]], int((ax[axels4graph[ai]][1][i]*scale+80)/hpix + 10 + aiH[ai]*hStep+150)])
ax4ch[ai][2].append([Wval[aiW[ai]], int((ax[axels4graph[ai]][2][i]*scale+80)/hpix + 10 + aiH[ai]*hStep+150)])
j += 1
for i in range(axels4graphcnt):
ax4ch[i][0] = np.array(ax4ch[i][0], np.int32).reshape((-1, 1, 2))
ax4ch[i][1] = np.array(ax4ch[i][1], np.int32).reshape((-1, 1, 2))
ax4ch[i][2] = np.array(ax4ch[i][2], np.int32).reshape((-1, 1, 2))
for i in range(axels4graphcnt):
image = cv2.polylines(image, [ax4ch[i][0]], isClosed, c0, thickness)
image = cv2.polylines(image, [ax4ch[i][1]], isClosed, c1, thickness)
image = cv2.polylines(image, [ax4ch[i][2]], isClosed, c2, thickness)
image = cv2.polylines(image, [vline], isClosed, (200, 200, 200), thickness)
image = cv2.polylines(image, [hline], isClosed, (200, 200, 200), thickness)
if acnt > 3 :
pdsum = np.sum(pd[pdcnt])
if pdsum < pdCnt :
# считаем, что тело лежит на кровати, т.е. (pdsum < 5)
inumax = 0
imark = 0
for iax in range(12):
anum = int (iax / 3) # номер акселя
aline = iax % 3 # три оси акселя
imin, imax, imean = (np.min(ax[anum][aline][mstart:acnt]), np.max(ax[anum][aline][mstart:acnt]), int(np.mean(ax[anum][aline][dstart:acnt])))
if abs(imax - imean) > sens or abs(imean - imin) > sens :
inumax += 1
imark |= (int(iax/6)+1)
for i in range(axels4graphcnt):
labH = int(50+imH*aiH[i]/2)
labW = int(20+imW*aiW[i]/2)
cv2.putText(image, "axel %i" % axels4graph[i], (labW, labH), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
imH, imW
cv2.imwrite("4acsels_chart.png",image)