## Python speech to text with PocketSphinx

I’ve wanted to use speech detection in my personal projects for the longest time, but the Google API has gradually gotten more and more restrictive as time passes. In order to ensure that my projects could work even without an internet connection, I looked for another speech recognition package that would preferably be easier to use. I found the Sphinx voice recognition suite of CMU to be a really great speech to text package. However, documentation and sample code is non-existent, so it took me forever to get anything done. Finally, I’ve figured it out! The example code is at the bottom of this post, but you can directly download it from Github here.

Here are the steps to take to get this working:

4. Run the code below

## Equations of motion for a planar simple double pendulum

To provide some background information for my N-link pendulum project, I’ve broken the methodology for solving the equations of motion (EOM) for a simple double pendulum into a separate post. I’ll explain to the best of my abilities!

The pendulum that we are solving for is composed of two masses, m1 and m2, suspended from each other by strings of length l1 and l2. Their positions relative to 0 is represented by theta1 and theta2. The entire pendulum is supported by point 0, which is a frictionless, massless point. The whole thing is a pain to create digitally, so I drew the setup below:

This will be a pretty long post, so scroll to the bottom for the cool graphs!

For our Mechanical Dynamics final project, a friend and I decided to generalize the equations for compound pendulums to write a simulation program that could generate animations for n-link compound pendulums with user specified lengths, masses, angular displacement and velocities. We originally wanted to write a full blown physics engine, but quickly realized that two weeks was not enough time to do something of that scale, haha.

First things first, a demonstration! The following is an simulation of a 5 link compound pendulum:

Read our final report for a more formal/comprehensive explanation of the process!

## PseudOS — A baremetal RaspPi OS

Now that I’ve got a bit of time, I thought I’d do a debrief of a couple of cool projects that I’ve worked on this past year. First up, PseudOS, a baremetal Raspberry PI OS that I wrote with some friends for our Software Systems final project.

(We were really sleep deprived when making the final video, apologies for any strange noises we make.)

It doesn’t look really amazing, because we didn’t really implement any OS features other than UART and a basic calculator program. However, we did do this all baremetal. What is baremetal, you ask? Well, it means that the code we wrote interacted with the RaspPi at the hardware level. We modified an Assembly boot file to give us control over the hardware registers, and wrote a kernel in C to provide the actual OS functionality. This website was an amazing resource for us through the whole process.

So delicious
Write a review
Print
Prep Time
20 min
Cook Time
15 min
Prep Time
20 min
Cook Time
15 min
Dry
1. 1 1/3 c AP flour
2. 1/2 tsp baking soda
3. 1/2 tsp baking powder
4. 1/2 tsp salt
5. 1/2 tsp cocoa powder (optional)
Wet
1. 1/2 c softened butter
2. 1/2 c brown sugar
3. 1/2 c white sugar
4. 1 egg
5. 1 tsp vanilla extract
Extras
1. 1 c semisweet chocolate chips
2. walnuts? almonds? gummy bears?? whatever extra stuff you like!
Instructions
1. 1. Preheat oven to 350F
2. 2. Mix dry ingredients together well
3. 3. Cream butter with sugar until you can't see clumps of butter anymore
4. 4. Mix in egg and vanilla
5. 5. Mix dry in wet
6. 6. Pour in chocolate chips
7. 7. Shape into vaguely ball shaped objects, and place on baking sheet well spaced.
8. 8. Bake for 15 minutes, but check the oven around 10 minutes or so. Cookies should still be soft when coming out of the oven, as long as the bottom of the cookies are brown
Notes
1. Depending on the condition of the butter, the resulting dough may be either too crumbly or too watery. If too crumbly, use 1/2 tsp of milk to get the dough to form. If too watery, refrigerate the dough for 30 minutes or so until firm.
2. Watery dough will cause the cookies to spread a lot and be really thin. Crumbly dough will make your cookies taste like rocks. Your preference though, haha.
sophie's blog http://blog.justsophie.com/
Chocolate chip cookies were the first things I ever baked. It was for a bake sale back in 4th grade, and I made the cookies from a mix. Because even back then I was a super baker and totally great at reading instructions, I left the cookies in the oven to cool instead of taking them out and rock hard cookies the next day, haha. (My teacher told me they were delicious anyways… Man, he was a great teacher. ;_;)

A decade later, I have refined the cookie baking process, and have come up with a recipe that seems to be received pretty well. The card is at the top of the post, but more detailed instructions and pictures will be below.

## A Reflection on 2015

Since it’s the last day of the year, it’s prime time to do some reflecting!

All in all, this year was pretty great. I got a summer internship at Apple, which has opened up an insane amount of doors. Though I’ve still got far to go and much to learn, being selected for a summer internship at such a big company has given me quite an ego boost. Still, since confidence easily turns into pride, I hope that I can rein it in a bit in the coming months. Quiet confidence is what I’m aiming for, the kind that manifests itself as an aura instead of hot air.

I’d really like to make blogging a habit, but the lack of free time in my schedule makes it difficult. And it’s not just time, but motivation as well. I enter each semester brimming with ambitions and ideas, but always end up burning out near the end. I’d like to put more effort into work life balance next semester, impossible as it may sound. I’ll pour more free time into working on Edwin instead of fooling around on the internet.

Look at how cute he is!

There’s a paper deadline coming up in March, and I really need to buckle down and really think about how I’m going to write this and present it. My robotics professor has poured a lot of time into my project, so I need to repay him in kind. Slacking off would not only be a detriment to me, but an insult to him.

I promise that we’ll do great things together Edwin!

On a separate note entirely, I haven’t been able to bake or cook as much as I’d like to this past semester, but I’m hoping to change that by having some new posts dedicated to cool recipes that I have. I have a lot of interests, but I think with proper time management I can succeed at all of them. It’s a matter of arbitration and passion I suppose. I made some really great green tea almond cookies last year, and I’m hoping I can dig up the recipe for it again.

I’ve also bought a new camera, and I’ve been playing around with the features. Photography is a really interesting art, one that I’m hoping to learn a bit about. One way I practiced was following my friends around and taking pictures of their lives. It was pretty fun and an interesting way to spend more time with people. I think I’ll try to do more of that next year too.

She’s super talented. (Taken and published with permission.)

Whether its music, photography, or technical expertise, I’m going to continue working on learning all the things and doing all the stuff. Next year is going to be great. 😀

-Sophie

## POE: The Gif Collection

We made a little desktop companion for our PoE (Principles of Engineering) final project. The goal of PoE is to create a final project that integrates electrical, mechanical, and software engineering to make something really cool out of it. Our project is inspired by Strandbeests, and does a really cool wave motion.

## Installing OpenCV for C++ and Python

Copying the instructions from my awesome robotics professor Paul Ruvolo from here so I don’t lose these steps:

To install OpenCV, go ahead and type each of these steps into your terminal window. Note that the “make” step will take forever, so go grab some popcorn or tea once you get there.

\$ cd /tmp
\$ wget https://github.com/Itseez/opencv/archive/2.4.11.zip
\$ unzip 2.4.11.zip
\$ cd opencv-2.4.11
\$ mkdir build
\$ cd build
\$ cmake .. -DCMAKE_BUILD_TYPE=RELEASE \
-DBUILD_PYTHON_SUPPORT=ON \
-DWITH_XINE=ON \
-DWITH_OPENGL=ON \
-DWITH_TBB=ON \
-DBUILD_EXAMPLES=ON \
-DBUILD_NEW_PYTHON_SUPPORT=ON \
-DWITH_V4L=ON \
-DOPENCV_EXTRA_MODULES_PATH=./modules
\$ make -j2
\$ sudo make install

In a nutshell, we download OpenCV 2.4 to the temporary folder, build it, make with two CPU cores, then install. It seems like those cmake parameters are actually pretty important to get stuff working!

-Sophie

## She’s a circle.

She tells me that she feels like a circular puzzle piece.

And though I know she’d never tell me why, I have a pretty good idea of the reason anyways. She yearns for his love, and brandishes an eraser as proof of her dedication.

“I like girls that are nice” He says, so she demolishes the castle of her convictions and redraws her beliefs in sand.

“You have nice smile” He says, so she pulls the edges of her tears into those upward crescents, smiling, always smiling.

I tell her she’s over-reacting. “People say that stuff all the time.”
She smiles back, “I know.”

But she doesn’t. Her soul has no edges, she’s blurred away anything that could possibly maim his perception of her virtue. People say that lovers are puzzle pieces that fit together prettily. How does a circle fit with anything?

-Sophie

## Basic Color Tracking with OpenCV

OpenCV is a fantastic library for use in everything computer vision. It has pretty good documentation and support, as well as being completely open source. Here’s an introduction to implementing a basic OpenCV program, which we’ll use to track a red circle.

A complete version of the code is at the end of this post.

```import cv2

def main():
cap = cv2.VideoCapture(0)
while True:
cv2.imshow("camera", frame)
c = cv2.waitKey(1)

if __name__ == '__main__':
main()
```

We open a connection to the webcam at index 0 (this number changes if you have multiple camera devices on your system) returning the image to the frame variable that we’ve set from before. We use `imshow` to show the frame to the screen, then pause for a second for the image to display. Run the code, and you should see a window with your webcam input pop up on the screen.

Cool! So we have a frame for our OpenCV script to work with. Now, in image processing, we have to treat images as an array of numbers. In our case, the camera returns a 640×480 array of pixels with values from (0,0,0) to (255,255,255)–RGB color space. We’ll want to convert the RGB image into HSV for color filtering.

Typically, we visualize RGB(right) as a cube and HSV(left) as a cone.

Note that RBG combines intensity and color. However, HSV separates the intensity of the color from the hue, which means we can simply filter one channel of the HSV image to get the color we want. Super useful!

**Note that OpenCV orders the channels as BRG instead of RGB**

Let’s convert the image to HSV, then filter for red.

```import cv2
import cv2.cv as cv
import numpy as np

def main():
cap = cv2.VideoCapture(0)
while True:

hsv_image = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
red_image = cv2.inRange(hsv_image, (0,50,50), (8,170,200))
cv2.imshow("camera", frame)
cv2.imshow("red image", red_image)
c = cv2.waitKey(1)

if __name__ == '__main__':
main()
```

Woah! So cool right!? Note that there’s a bit of red from my face, as well as my my little red friend in the background is highlighted as well. However, the majority of the filtered image is around the red pokeball. As a brief aside: The reason why color filtering isn’t the best way do object recognition is that color changes drastically with the lighting conditions. (Remember the blue/black, gold/white dress scandal earlier this year? Hahaha). To change the color that the code will filter for, change the value inside the cv2.inRange function.

With the red image filtered out now, we can do some basic shape detection to find where our ball is. The function can use for this is HoughCircles. The method behind circle detection is rather complex, but we can treat it like a black box for now and just run the code over our image.

```import cv2
import cv2.cv as cv
import numpy as np

def main():
cap = cv2.VideoCapture(0)
while True:

hsv_image = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
red_image = cv2.inRange(hsv_image, (0,50,50), (8,170,200))

if circles != None:
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(frame,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(frame,(i[0],i[1]),2,(0,0,255),3)

cv2.imshow("camera", frame)
cv2.imshow("red image", red_image)
c = cv2.waitKey(1)

if __name__ == '__main__':
main()
```

The result is a function that can largely find red circular objects in a scene.

A caveat:
This is a crazy basic implementation of color filtering + HoughCircles. As a result, this code only filters for a limited range of red colors, then runs the hough algorithm with fairly loose parameters over the resulting image. Due to this, this code is inflexible in different lightning conditions and is unreliable for the circle detected output. There are several ways to do object detection/color filtering, the intracacies of which I’ll cover another day. 🙂