I'm working on some energy computation about the sounds perceived by the Nao robot.
I'm using naoqi 2-1-4 and the following snippet of code to show the values:
import time
from naoqi import ALProxy
robotIP = "194.119.214.185"
port = 9559
soundDevice = ALProxy("ALAudioDevice", robotIP, port)
soundDevice.enableEnergyComputation()
try:
header = 'Left\t\t\t\tRight\t\t\t\tFront\t\t\t\tRear'
fmt = '{Left:.2f}\t\t\t\t{Right:.2f}\t\t\t\t{Front:.2f}\t\t\t\t{Rear:.2f}'
while True:
time.sleep(0.5)
left = soundDevice.getLeftMicEnergy()
right = soundDevice.getRightMicEnergy()
front = soundDevice.getFrontMicEnergy()
rear = soundDevice.getRearMicEnergy()
print header
print fmt.format(Left=left,Right=right,
Front=front,Rear=rear)
except KeyboardInterrupt:
print "Stopped by user."
I was not able to understand what is the nature of this values.
I've looked into this code from this page (at the bottom. Yeah I know it's c++ code but I could not find more, I assume that is the same concept, except for the language) and found in a comment that the RMS power is calculated.
I can't understand how are these values possible (tried to express them in dB but they made no sense anyway).
Does anyone have any idea of what these values stand for? How can I relate them to some "real" measuring units?
By the way here is a list of all methods.
Thanks in advance
Looking at the source, it seems to be just the sum of the square of each sample, then divided by the length of the buffer, finally squared.
sqrt(sum(each_sample*each_sample)/len(samples))
Hope it helps you...
As you don't provide us what are the strange values you've got (numbers?letters?) All I can do to help you is this link about another way to compute those values: NAO robot remote audio problems
Related
So I need to get some mocking+testing done on a very specific kind of python code: it is an eBPF(Berkely packet filter) program using python, which in essence runs a C program in kernel space through a python script via the BPF Compiler Collection BCC
eg code:
from bcc import BPF
# define BPF program
prog = """
int hello(void *ctx) {
bpf_trace_printk("Hello, World!\\n");
return 0;
}
"""
# load BPF program
b = BPF(text=prog)
b.attach_kprobe(event=b.get_syscall_fnname("clone"), fn_name="hello")
# header
print("%-18s %-16s %-6s %s" % ("TIME(s)", "COMM", "PID", "MESSAGE"))
# format output
while 1:
try:
(task, pid, cpu, flags, ts, msg) = b.trace_fields()
except ValueError:
continue
print("%-18.9f %-16s %-6d %s" % (ts, task, pid, msg))
I want to now write tests for the C and the Python code without the 2 interfering (I would even be satisfied with some basic python unit testing). Any & all possible approaches to the problem are welcome, or even a general direction to look for info on this kind of testing, really just looking to address the following concerns:
Can I easily mock the kernel interactions when testing the python code?
Can I emulate a message coming from the kernel to user-space?
Can I test a message that’s just larger or just smaller than the expected message size?
I tried looking into the topic for days, to no avail so even pointing me in the right direction would be greatly appreciated...
So it turns out there is no quick way to really get into the testing of the C code in a basic way like you would using pytest to test the python logic. However if you absolutely need solid testing done then browsing through here and making sense of how BCC does their version of "unit testing" may be of help...
Currently I'm making a python bot for whatsapp manually without APIs or that sort because I am clueless. As such, I'm using Selenium to take in messages and auto reply. Currently, I'm noticing that every few messages, one message doesn't get picked up because the loops ran are too slow and my computer is already pretty fast. Here's the code:
def incoming_msges():
msges = driver.find_elements_by_class_name("message-in")
msgq = []
tq = []
try:
for msg in msges:
txt_msg = msg.find_elements_by_class_name("copyable-text")
time = msg.find_elements_by_class_name("_18lLQ")
for t in time:
tq.append(t.text.lower())
for txt in txt_msg:
msgq.append(txt.text.lower())
msgq = msgq[-1]
tq = tq[-1]
if len(msgq) > 0:
return (msgq, tq)
except StaleElementReferenceException:
pass
return False
Previously, I didn't add the time check thing, and the message sent would be saved, with this loop continuously running such that even if the other party sent the same thing again, the code would not recognise it as a new message because it thinks it's the same one as before. So now, the problem is that my code is super time consuming and I have no idea how to speed it up. I tried doing this:
def incoming_msges():
msges = browser.find_elements_by_class_name("message-in")
try:
msg = msges[-1]
txt_msg = msg.find_element_by_xpath("/span[#class=\"copyable-text\"]").text.lower()
time = msg.find_element_by_xpath("/span[#class=\"_18lLQ\"]").text.lower()
return (txt_msg, time)
except Exception:
pass
return False
However, like this, the code just doesn't find any messages. I have gotten the elements' types and classes correct according to the whatsapp web website but it just doesn't run. What's the correct way of rewriting my first code block as it is still correct? Thanks in advance.
First thing first ...
I definitely recommend using API ... Because what you are trying to do here is to reinvent the wheel. API has the power of telling you if there is a change in your status and you can queue these changes ... So I definitely recommend to use API ... It might be hard at the beginning, but trust me, its worth it ...
Next I would recommend you to use normal variable names. msges msgq tq (these are kindof unreadable and I still dont get what they are supposed to be after reading the code twice ...)
But to your speed problem ... "try - catch (aka except)" blocks are really heavy on a performance ... I would recommend to use safe programming if possible (20 if statements might be faster, but might not a same time) ... Also I think you are kind of unaware of a python language (atleast from what i can see here)
msgq = msgq[-1] # you are telling it to take the last element and change array variable to string .. to be more specific...
msgq ([1,2,3,4]) = msgq[-1] (4) will result to -> msgq = 4 (which in my option hits you performance as well)
tq = tq[-1] # same here
This would be better :)
if len(msgq[-1]) > 0:
return (msgq[-1], tq[-1])
If I understand your code correctly, you are trying to scrape the messages, but if its like you are saying that you want to make auto-reply bot, I would recommend you to eighter get ready for some JS magic or switch tool. I personally noticed that the selenium has a problem with dynamic content ... to be more specific ... once its at the end of the file it does not scrape it again ... so if you do not want to auto refresh every 5-10 seconds to get the latest HTML file, I recommend eighter to create this bot in JS (that will trigger everytime that an element changes) or use the API and use selenium just for responses. I was told that Selenium was created to simulate the common user to check if user interface works as it should (if buttons exists, if the website contains all what it should etc.) ... I think that selenium is for this job something like a flower small sponge for a car clean ... you can do it ... buts gonna cost you alot of time and you might miss some spots (like you missed those messages) ...
Lastly ... the work with strings in general is really costly. you are doing O(n^2) of operations in a try block ... which i can imagine can be really costly ... if its possible, I would reduce the number of inner for loops.
I wish you good luck in this project and I hope you find the answer you seek, while I hope my answer was at least a little helpful.
I just got into Python (Jython) coding a few hours ago and I'm trying to automate Kik messenger (using an Android emulator) using Sikuli IDE.
I am trying to make a region observer that scans for changes, if a change is made, it will check if any commands are found. I am not really sure what I'm doing, but this is the code I got with some help all around the web and documentations:
cmdScanLoc = Region(Region(65,762,167,59))
def cmdHelp():
type("Help")
type(Key.ENTER)
cmdScanLoc.stopObserver()
def cmdPing():
type("Pong.")
type(Key.ENTER)
cmdScanLoc.stopObserver()
def changeDetected(event):
print("Change")
if cmdScanLoc.exists("1440090739688.png"):
cmdHelp()
elif cmdScanLoc.exists("1440090725124.png"):
cmdPing()
else:
print("No Command Found")
def startObserver():
cmdScanLoc.onChange(50,changeDetected)
cmdScanLoc.observe(10,background=False)
Settings.ObserveScanRate = 10
startObserver()
Here is the log, after typing !ping:
Change
!help
[log] TYPE "Help"
[log] TYPE "#ENTER."
It seems to go to cmdHelp(), even though I typed !ping. How is that possible? It just completely ignores the if-statement.
And here is an image of the region I'm scanning:
http://i.imgur.com/QAP9OnV.png
And an image of the images I'm scanning for:
http://i.imgur.com/wXxphQN.png (code in this image is no longer accurate as you can see)
I would greatly appreciate it if someone could guide me in the right direction with this "command scanner" where if a certain command is detected, the appropiate function is called.
Thanks a lot in advance and sorry if this is a really nooby question, I've just been trying for hours and hours, looking up documentation of Sikuli and Python and I just can't get it to work...
It's much smarter and much faster to do this kind of thing with the region observer than with an if-statement. Example code:
def cmd1(event):
print("Command One")
event.cmdRegion.stopObserver()
waitCmdAppear()
def cmd2(event):
print("Command Two")
event.cmdRegion.stopObserver()
waitCmdAppear()
def cmd3(event):
print("Command Three")
event.cmdRegion.stopObserver()
waitCmdAppear()
def waitCmdAppear():
cmdRegion.onAppear(Pattern("1.png").exact(), cmd1)
cmdRegion.onAppear(Pattern("2.png").exact(), cmd2)
cmdRegion.onAppear(Pattern("3.png").exact(), cmd3)
cmdRegion.observe(FOREVER)
waitCmdAppear()
Things to not forget:
The (event) part when defining a function that's going to be called by the region observer.
Stopping the Observer in the event, even if you are going to need it again. Just restart it.
In the onAppear, (region.onAppear([PS], [handler])) type the handler (ex. cmd3) not the function (ex. cmd3())
I hope this will help other people. :)
I am very new to programming. I have only touched the surface of one language, Python, which is what I am working with at the moment. I am trying to write a program that can display random rolling numbers between a range like 1-100. For lack of being able to explain it, I would rather show a video of what I am looking to do.
http://www.youtube.com/watch?v=88SENZe6Z3I
At about 33 seconds in you can see rolling numbers that the player must stop in order to assign it to a character trait. Less all the pretty graphics and everything, What I want to know is if it is possible to write a program that serves this same type of function in Python? If not Python, the only other 2 languages I am becoming a little familiar with are C# and Java. Would it be possible to write it with one or a combination of those?
If it is possible, can you point me in the direction for resources to this effort. I did do a search before posting this but I found myself coming up empty for lack of knowing what to search for.
Thanks!
The “problem” is that this is not directly possible using the command line interface. If you are looking into game development you are probably going to have some graphical interface anyway, so you should look if you find a library that gives you more options in animating things.
Nevertheless, a possible solution for the command line would involve multi-threading. The reason for that is that you cannot both print (continuously changing) numbers and also wait for keyboard input. The normal command line is actually quite limited in that way.
Below you can find a possible solution with a threading approach. But again, if you are going for some game development, you should rather check out actual graphic libraries, or even game libraries that can help you.
from random import randint
from time import sleep
from threading import Thread
import sys
class RollAnimation ( Thread ):
activated = True
number = None
def run ( self ):
while self.activated:
self.number = randint( 1, 100 )
sys.stdout.write( '\r{: >3}'.format( self.number ) )
sys.stdout.flush()
sleep( 0.05 )
t = RollAnimation()
t.start()
# Waiting for enter
input() # use raw_input() for Python 2.x
t.activated = False
print( 'Final roll result:', t.number )
You haven't formulated your question correctly if people has to watch a Youtube video to understand what you want to do. Without watching the video, I can only assume you want to know how random integers work.
import random
x = random.randint(1, 100)
If you want it rolling, you can simply either make three variables or display three random integers inside a for loop and place that in a while or a for loop.
Here's an example.
import random
import sys
try:
rolls = int(sys.argv[1])
except IndexError:
print "Usage: "+sys.argv[0]+" [rolls]"
sys.exit(1)
for i in range(1, rolls+1):
print "Roll "+str(i)
for i in range(0, 3):
print random.randint(1, 100)
For the graphical part of your application, you should look at pygame: http://pygame.org
Is there anyway to encorporate Dragon NaturallySpeaking into an event driven program? My boss would really like it if I used DNS to record user voice input without writing it to the screen and saving it directly to XML. I've been doing research for several days now and I can not see a way for this to happen without the (really expensive) SDK, I don't even know that it would work then.
Microsoft has the ability to write a (Python) program where it's speech recognizer can wait until it detects a speech event and then process it. It also has the handy quality of being able to suggest alternative phrases to the one that it thinks is the best guess and recording the .wav file for later use. Sample code:
spEngine = MsSpeech()
spEngine.setEventHandler(RecoEventHandler(spEngine.context))
class RecoEventHandler(SpRecoContext):
def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result):
res = win32com.client.Dispatch(Result)
phrase = res.PhraseInfo.GetText()
#from here I would save it as XML
# write reco phrases
altPhrases = reco.Alternates(NBEST)
for phrase in altPhrases:
nodePhrase = self.doc.createElement(TAG_PHRASE)
I can not seem to make DNS do this. The closest I can do-hickey it to is:
while keepGoing == True:
yourWords = raw_input("Your input: ")
transcript_el = createTranscript(doc, "user", yourWords)
speech_el.appendChild(transcript_el)
if yourWords == 'bye':
break
It even has the horrible side effect of making the user say "new-line" after every sentence! Not the preferred solution at all! Is there anyway to make DNS do what Microsoft Speech does?
FYI: I know the logical solution would be to simply switch to Microsoft Speech but let's assume, just for grins and giggles, that that is not an option.
UPDATE - Has anyone bought the SDK? Did you find it useful?
Solution: download Natlink - http://qh.antenna.nl/unimacro/installation/installation.html
It's not quite as flexible to use as SAPI but it covers the basics and I got almost everything that I needed out of it. Also, heads up, it and Python need to be downloaded for all users on your machine or it won't work properly and it works for every version of Python BUT 2.4.
Documentation for all supported commands is found under C:\NatLink\NatLink\MiscScripts\natlink.txt after you download it. It's under all the updates at the top of the file.
Example code:
#make sure DNS is running before you start
if not natlink.isNatSpeakRunning():
raiseError('must start up Dragon NaturallySpeaking first!')
shutdownServer()
return
#connect to natlink and load the grammer it's supposed to recognize
natlink.natConnect()
loggerGrammar = LoggerGrammar()
loggerGrammar.initialize()
if natlink.getMicState() == 'off':
natlink.setMicState('on')
userName = 'Danni'
natlink.openUser(userName)
#natlink.waitForSpeech() continuous loop waiting for input.
#Results are sent to gotResultsObject method of the logger grammar
natlink.waitForSpeech()
natlink.natDisconnect()
The code's severely abbreviated from my production version but I hope you get the idea. Only problem now is that I still have to returned to the mini-window natlink.waitForSpeech() creates to click 'close' before I can exit the program safely. A way to signal the window to close from python without using the timeout parameter would be fantastic.