-
Notifications
You must be signed in to change notification settings - Fork 0
/
posture1.py
76 lines (59 loc) · 2.26 KB
/
posture1.py
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
# TDA4vm Edge Impulse posture enforcer
# Roni Bandini @RoniBandini June 2023
# MIT License
# https://bandini.medium.com
import subprocess
import time
import RPi.GPIO as GPIO
import requests
import json
# Relay output pin
output_pin = 18
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(output_pin, GPIO.OUT)
# Runner output file
output_file = open('output.txt', 'w')
print("Posture enforcer with Texas Instruments TDA4VM and Edge Impulse")
print("Roni Bandini, June 2023, Argentina, @RoniBandini")
print("")
print("Testing relay")
GPIO.output(output_pin, GPIO.HIGH)
time.sleep(3)
GPIO.output(output_pin, GPIO.LOW)
print("Stop with CTRL-C")
# Launch Impulse Runner with parameters using subprocess, output to file
subprocess.Popen(["edge-impulse-linux-runner", "--force-engine tidl", "-force-target", "runner-linux-aarch64-tda4vm"], stdout=output_file)
with open("output.txt", "r") as f:
lines_seen = set()
while True:
line = f.readline()
if not line:
time.sleep(1)
continue
if ("[]" in line):
print("No posture detected")
if ("height" in line):
if ("notok" in line) and line not in lines_seen:
print("Incorrect posture")
#print("Complete line: "+line)
parts = line.split()
myLine = parts[2][1:-1]
#print("Json part: "+str(myLine))
myJson = json.loads(myLine)
print("Confidence: "+str(myJson["value"]))
print(" At X: "+str(myJson["x"])+ " Y: "+str(myJson["y"]))
lines_seen.add(line)
# Disable machine
GPIO.output(output_pin, GPIO.LOW)
if ("ok" in line) and line not in lines_seen:
print("Correct posture")
parts = line.split()
myLine = parts[2][1:-1]
myJson = json.loads(myLine)
print("Confidence: "+str(myJson["value"]))
print(" At X: "+str(myJson["x"])+ " Y: "+str(myJson["y"]))
lines_seen.add(line)
# Enable machine
curr_value = GPIO.HIGH
GPIO.output(output_pin, curr_value)