forked from interfacew/GestureMate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaskController.py
89 lines (79 loc) · 3.43 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
from tasks import *
import json
import cv2 as cv
from Utils import drawLandmarks, extractLandmarks
import mediapipe.python.solutions as sol
class TaskController:
def __init__(self):
self.tasks = {}
self.activate = {}
def listen(self, x):
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[id] = task
self.activate[id] = task.start
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 = DetectTask(
self, task['id'], task['command'], task['nextTasks'], task['start'])
elif taskType == "keypress":
taskObject = TimeoutTask(
self, task['id'], task['keys'], task['nextTasks'], task['start'])
elif taskType == "detect":
taskObject = DetectTask(
self, task['id'], task['bodyPart'], task['frames'], task['nextTasks'], task['start'])
elif taskType == "match":
taskObject = MatchTask(self, task['id'], task['bodyPart'], task['poseFile'],
task['sensetive'], task['frames'], task['nextTasks'], task['start'])
elif taskType == "timeout":
taskObject = TimeoutTask(
self, task['id'], task['timeout'], task['nextTasks'], task['start'])
self.addTask(taskObject)
def startListen(self):
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)
with sol.holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5, model_complexity=2) as holistic:
while camera.isOpened():
ret, frame = camera.read()
if ret:
frame = frame[:, ::-1, :]
# COLOR CONVERSION BGR 2 RGB
image = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
image.flags.writeable = False # Image is no longer writeable
# Make prediction
results = holistic.process(image)
image.flags.writeable = True # Image is now writeable
# COLOR COVERSION RGB 2 BGR
image = cv.cvtColor(image, cv.COLOR_RGB2BGR)
drawLandmarks(image, results)
self.listen(extractLandmarks(results))
cv.imshow('OpenCV Feed', image)
if cv.waitKey(20) & 0xFF == ord('q'):
break
camera.release()
cv.destroyAllWindows()