Skip to content

Commit bdf6aee

Browse files
committed
Added dino example, moved everything into a subfolder
1 parent e47b1c5 commit bdf6aee

30 files changed

+104
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
__pycache__/*
2+
*/__pycache__/*
23
.ipynb_checkpoints/*
34

45
data/*
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

examples/dino_jump.py

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
'''
2+
Instructions:
3+
1. Run python dino_jump.py - This launches the training tool.
4+
2. Click on the pygame window thats opened to make sure windows sends the keypresses to that process.
5+
3. Relax the Myo arm, and with your other hand press 0 - This labels the incoming data as class 0
6+
4. Make a fist with your hand and press 1, to label the fist as 1.
7+
5. Try making a closed and open fist and watching the bars change.
8+
6. Once you've gathered enough data, exit the pygame window. This saves the data in data/vals0.dat and vals1.dat
9+
7. If you make a mistake and wrongly classify data, delete vals0 and vals1 and regather
10+
8. If your happy it works, change TRAINING_MODE to False.
11+
9. Goto https://trex-runner.com/ and rerun dino_jump.py with TRAINING_MODE set to false.
12+
10. Click in the brower to start the game and tell windows to send keypresses there
13+
11. Try making a fist and seeing if the dino jumps
14+
15+
If it doesn't work, feel free to let me know in the discord:
16+
https://discord.com/invite/mG58PVyk83
17+
18+
- PerlinWarp
19+
'''
20+
21+
22+
import sys
23+
sys.path.append('../')
24+
25+
import pygame
26+
from pygame.locals import *
27+
from pynput.keyboard import Key, Controller
28+
from pyomyo import Myo, emg_mode
29+
import pyomyo.simple_classifier as sc
30+
31+
TRAINING_MODE = True
32+
33+
def dino_handler(pose):
34+
print("Pose detected", pose)
35+
if ((pose == 1) and (TRAINING_MODE == False)):
36+
for i in range(0,10):
37+
# Press and release space
38+
keyboard.press(Key.space)
39+
keyboard.release(Key.space)
40+
41+
42+
if __name__ == '__main__':
43+
keyboard = Controller()
44+
45+
pygame.init()
46+
w, h = 800, 320
47+
scr = pygame.display.set_mode((w, h))
48+
font = pygame.font.Font(None, 30)
49+
50+
m = sc.MyoClassifier(sc.Classifier())
51+
hnd = sc.EMGHandler(m)
52+
m.add_emg_handler(hnd)
53+
m.connect()
54+
55+
m.add_raw_pose_handler(dino_handler)
56+
57+
try:
58+
while True:
59+
m.run()
60+
61+
r = m.history_cnt.most_common(1)[0][0]
62+
63+
for ev in pygame.event.get():
64+
if ev.type == QUIT or (ev.type == KEYDOWN and ev.unicode == 'q'):
65+
raise KeyboardInterrupt()
66+
elif ev.type == KEYDOWN:
67+
if K_0 <= ev.key <= K_9:
68+
hnd.recording = ev.key - K_0
69+
elif K_KP0 <= ev.key <= K_KP9:
70+
hnd.recording = ev.key - K_Kp0
71+
elif ev.unicode == 'r':
72+
hnd.cl.read_data()
73+
elif ev.type == KEYUP:
74+
if K_0 <= ev.key <= K_9 or K_KP0 <= ev.key <= K_KP9:
75+
hnd.recording = -1
76+
77+
scr.fill((0, 0, 0), (0, 0, w, h))
78+
79+
for i in range(10):
80+
x = 0
81+
y = 0 + 30 * i
82+
83+
clr = (0,200,0) if i == r else (255,255,255)
84+
85+
txt = font.render('%5d' % (m.cls.Y == i).sum(), True, (255,255,255))
86+
scr.blit(txt, (x + 20, y))
87+
88+
txt = font.render('%d' % i, True, clr)
89+
scr.blit(txt, (x + 110, y))
90+
91+
92+
scr.fill((0,0,0), (x+130, y + txt.get_height() / 2 - 10, len(m.history) * 20, 20))
93+
scr.fill(clr, (x+130, y + txt.get_height() / 2 - 10, m.history_cnt[i] * 20, 20))
94+
95+
pygame.display.flip()
96+
97+
except KeyboardInterrupt:
98+
pass
99+
finally:
100+
m.disconnect()
101+
print()
102+
pygame.quit()

pyomyo/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from pyomyo.pyomyo import *

pyomyo/data/vals0.dat

Whitespace-only changes.

pyomyo/data/vals1.dat

Whitespace-only changes.

pyomyo/data/vals2.dat

Whitespace-only changes.

pyomyo/data/vals3.dat

Whitespace-only changes.

pyomyo/data/vals4.dat

Whitespace-only changes.

pyomyo/data/vals5.dat

Whitespace-only changes.

pyomyo/data/vals6.dat

Whitespace-only changes.

pyomyo/data/vals7.dat

Whitespace-only changes.

pyomyo/data/vals8.dat

Whitespace-only changes.

pyomyo/data/vals9.dat

Whitespace-only changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)