forked from interfacew/GestureMate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetHandConfig.py
65 lines (59 loc) · 2.92 KB
/
GetHandConfig.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
import cv2 as cv
from Utils import drawLandmarks, extractLandmarks
import mediapipe.python.solutions as sol
import json
from tasks import MatchTask
def getData():
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)
res = {}
alert1 = "press s to save to result"
alert2 = "press q to write to json file"
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)
cv.putText(image, f"{alert1}", (0, 25),
cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
cv.putText(image, f"{alert2}", (0, 50),
cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
if not res == {}:
now = extractLandmarks(results)
bodyDelta = MatchTask.calcDelta(['body'], now, res)
leftHandDelta = MatchTask.calcDelta(['leftHand'], now, res)
rightHandDelta = MatchTask.calcDelta(
['rightHand'], now, res)
faceDelta = MatchTask.calcDelta(['face'], now, res)
cv.putText(image, f"body delta : {bodyDelta:.5f}", (
0, 75), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
cv.putText(image, f"left hand delta : {leftHandDelta:.5f}", (
0, 100), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
cv.putText(image, f"right hand delta : {rightHandDelta:.5f}", (
0, 125), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
cv.putText(image, f"face delta : {faceDelta:.5f}", (
0, 150), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
cv.imshow('OpenCV Feed', image)
key = cv.waitKey(10)
if key & 0xFF == ord('q'):
break
if key & 0xFF == ord('s'):
res = extractLandmarks(results)
alert1 = "saved to result, press s to recover"
camera.release()
cv.destroyAllWindows()
with open("./output.json", "w") as f:
f.write(json.dumps(res))
if __name__ == "__main__":
getData()