-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTaskController.py
144 lines (128 loc) · 5.66 KB
/
TaskController.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from tasks import *
import json
import cv2 as cv
from Utils import drawLandmarks, extractLandmarks, generateNullLandmarks
import mediapipe.python.solutions as sol
import time
from collections import deque
import math
class TaskController:
FPS_COUNT_FRAME = 10
def __init__(self):
self.tasks = {}
self.activate = {}
def listen(self, x):
print("\033[H\033[J")
for i in self.tasks.keys():
if self.activate[i]:
self.tasks[i].listen(x)
def activateTask(self, id: str, x):
if not id in self.activate.keys():
print(f"Unknown task id {id}")
return
self.activate[id] = True
self.tasks[id].activate(x)
def deactivateTask(self, id: str, x):
if not id in self.activate.keys():
print(f"Unknown task id {id}")
return
self.activate[id] = False
self.tasks[id].deactivate(x)
def removeTask(self, id: str):
self.activate.pop(id)
def addTask(self, task: Task):
self.tasks[task.id] = task
self.activate[task.id] = False
if task.start:
self.activateTask(task.id, generateNullLandmarks())
def clear(self):
self.tasks = {}
self.Activate = {}
def readConfig(self, path: str):
with open(path, "r") as f:
config = json.loads(f.read())
for task in config:
taskType = task['type']
if taskType == "command":
taskObject = CommandTask(self, task['id'], task['command'],
task.get('timeout',[]),
task.get('nextTasks', []),
task.get('start', False))
elif taskType == "keypress":
taskObject = KeyTask(self, task['id'], task['keys'],
task.get('nextTasks', []),
task.get('start', False))
elif taskType == "detect":
taskObject = DetectTask(self, task['id'], task['bodyPart'],
task['frames'],
task.get('nextTasks', []),
task.get('start', False))
elif taskType == "match":
taskObject = MatchTask(self, task['id'], task['bodyPart'],
task['poseFile'],
task['sensetive'], task['frames'],
task.get('nextTasks', []),
task.get('start', False))
elif taskType == "timeout":
taskObject = TimeoutTask(self, task['id'], task['timeout'],
task.get('nextTasks', []),
task.get('start', False))
elif taskType == "socketsend":
taskObject = SocketSendTask(self, task['id'],
task.get('ip', "127.0.0.1"),
task['port'],
task.get('extra', None),
task.get('nextTasks', []),
task.get('start', False))
self.addTask(taskObject)
def startListen(self, targetFPS, modelComplexity):
camera = cv.VideoCapture(0, cv.CAP_DSHOW)
camera.set(cv.CAP_PROP_FRAME_WIDTH, 1920)
camera.set(cv.CAP_PROP_FRAME_HEIGHT, 1080)
camera.set(cv.CAP_PROP_FPS, 60)
q = deque([], self.FPS_COUNT_FRAME + 10)
framecnt = 0
with sol.holistic.Holistic(
min_detection_confidence=0.5,
min_tracking_confidence=0.5,
model_complexity=modelComplexity) as holistic:
while camera.isOpened():
ret, frame = camera.read()
waitTime = 1
if ret:
start = time.time() * 1e3
frame = frame[:, ::-1, :]
image = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
image.flags.writeable = False
results = holistic.process(image)
image.flags.writeable = True
image = cv.cvtColor(image, cv.COLOR_RGB2BGR)
drawLandmarks(image, results)
if framecnt >= self.FPS_COUNT_FRAME * 3:
self.listen(extractLandmarks(results))
now = time.time() * 1e3
if framecnt >= self.FPS_COUNT_FRAME:
last = q.pop()
cv.putText(
image,
f"FPS: {self.FPS_COUNT_FRAME*1e3/(now-last):.3f}",
(10, 30),
cv.FONT_HERSHEY_COMPLEX,
1.0, (255, 0, 0),
bottomLeftOrigin=False)
if targetFPS != 0:
waitTime = max(
1,
math.floor(1e3 / targetFPS - (now - start)) -
3)
else:
waitTime = 1
q.appendleft(now)
cv.imshow('OpenCV Feed', image)
framecnt += 1
if cv.waitKey(waitTime) & 0xFF == ord('q'):
for task in self.tasks.keys():
self.deactivateTask(task, generateNullLandmarks())
break
camera.release()
cv.destroyAllWindows()