NiklasRosenstein / myo-python

Python bindings for the Myo SDK
Other
262 stars 102 forks source link

TypeError: expected callable or DeviceListener #68

Closed hassanyousufx closed 6 years ago

hassanyousufx commented 6 years ago

File "C:\Python35\lib\site-packages\myo_python-1.0.3-py3.5.egg\myo_ffi.py", line 527, in run TypeError: expected callable or DeviceListener

import collections
import myo as libmyo
from myo import Hub
import threading
import time
import numpy as np
import tensorflow as tf
from include.model import model

x, y, output, global_step, y_pred_cls = model()

saver = tf.train.Saver()
_SAVE_PATH = "./data/tensorflow_sessions/myo_armband/"
sess = tf.Session()

try:
    print("Trying to restore last checkpoint ...")
    last_chk_path = tf.train.latest_checkpoint(checkpoint_dir=_SAVE_PATH)
    print(last_chk_path)
    saver.restore(sess, save_path=last_chk_path)
    print("Restored checkpoint from:", last_chk_path)
except:
    print("Failed to restore checkpoint. Initializing variables instead.")
    sess.run(tf.global_variables_initializer())

class MyListener(libmyo.DeviceListener):

    def __init__(self, queue_size=8):
        self.lock = threading.Lock()
        self.emg_data_queue = collections.deque(maxlen=queue_size)

    def on_connect(self, device, timestamp, firmware_version):
        device.set_stream_emg(libmyo.StreamEmg.enabled)

    def on_emg_data(self, device, timestamp, emg_data):
        with self.lock:
            self.emg_data_queue.append((timestamp, emg_data))

    def get_emg_data(self):
        with self.lock:
            return list(self.emg_data_queue)

libmyo.init()
hub = Hub()
start = time.time()
temp = []
try:
    listener = MyListener()
    hub.run(2000, listener)     ----> ERROR

What could the reason? Please help.

NiklasRosenstein commented 6 years ago

You use old code with the new library.

Since I've been getting this a lot recently, I've added a more obvious notice to the README.

https://github.com/NiklasRosenstein/myo-python

hassanyousufx commented 6 years ago

I've followed the "Migrating from v0.2.x" section, but I still get the same error. Can you please tell me what changes are required in this code? I will be grateful.

NiklasRosenstein commented 6 years ago

Also just added that to the README -- the order of arguments to run() is reversed. Use hub.run(listener, 2000) instead.

hassanyousufx commented 6 years ago

I already reversed the arguments, but I still get "File "C:\Python35\lib\site-packages\myo_python-1.0.3-py3.5.egg\myo_ffi.py", line 527, in run TypeError: expected callable or DeviceListenerTypeError: expected callable or DeviceListener".

NiklasRosenstein commented 6 years ago

Please post the full modified code again

hassanyousufx commented 6 years ago
import collections
import myo
import threading
import time
import numpy as np
import tensorflow as tf
from include.model import model

x, y, output, global_step, y_pred_cls = model()

saver = tf.train.Saver()
_SAVE_PATH = "./data/tensorflow_sessions/myo_armband/"
sess = tf.Session()

try:
    print("Trying to restore last checkpoint ...")
    last_chk_path = tf.train.latest_checkpoint(checkpoint_dir=_SAVE_PATH)
    print(last_chk_path)
    saver.restore(sess, save_path=last_chk_path)
    print("Restored checkpoint from:", last_chk_path)
except:
    print("Failed to restore checkpoint. Initializing variables instead.")
    sess.run(tf.global_variables_initializer())

class Listener(myo.DeviceListener):

    def __init__(self, queue_size=8):
        self.lock = threading.Lock()
        self.emg_data_queue = collections.deque(maxlen=queue_size)

    def on_connect(self, device, timestamp, firmware_version):
        device.set_stream_emg(myo.StreamEmg.enabled)

    def on_emg_data(self, device, timestamp, emg_data):
        with self.lock:
            self.emg_data_queue.append((timestamp, emg_data))

    def get_emg_data(self):
        with self.lock:
            return list(self.emg_data_queue)

myo.init()
hub = myo.Hub()
start = time.time()
temp = []
try:
    listener = Listener()
    hub.run(2000, listener)
    while True:
        data = listener.get_emg_data()
        if time.time() - start >= 1:
            response = np.argmax(np.bincount(temp))
            print("Predicted gesture: {0}".format(response))
            temp = []
            start = time.time()
        if len(data) > 0:
            tmp = []
            for v in listener.get_emg_data():
                tmp.append(v[1])
            tmp = list(np.stack(tmp).flatten())
            if len(tmp) >= 64:
                pred = sess.run(y_pred_cls, feed_dict={x: np.array([tmp])})
                temp.append(pred[0])
        time.sleep(0.01)
finally:
    sess.close()
NiklasRosenstein commented 6 years ago

I already reversed the arguments, but I still get "File "C:\Python35\lib\site-packages\myo_python-1.0.3-py3.5.egg\myo_ffi.py", line 527, in run TypeError: expected callable or DeviceListenerTypeError: expected callable or DeviceListener".

But apparently you didn't.

hub.run(2000, listener)

hassanyousufx commented 6 years ago

Oops! Sorry. I thought I did. I reversed them just now. Now I am getting an error at response = np.argmax(np.bincount(temp)). The error says "ValueError: attempt to get argmax of an empty sequence".

So I am guessing that it was connected/paired successfully and the problem lies in my prediction code now?

NiklasRosenstein commented 6 years ago

It's an error in your code. The error message is quite explicit. ;)

vikramharindranath commented 4 years ago

@NiklasRosenstein Hello, Can i know the units in which accelerometer and gyroscope values are recorded? (Not regarding to this topic but i didnt know how to reach you)

NiklasRosenstein commented 4 years ago

@vikramharindranath You can just create a new Github issue instead of commenting on an almost 2 years old one 😉 For this question though, the answer is to check the Myo documentation. The values come from the device via the Myo C library, there's no unit conversion or adaption happening in the Python library.