Caesar Cipher Generator

I wanted to post something kinda secret, but also on this blog. So I figured I’d do it in a cipher, so if people want to read the message they’d at least need to put in the work to decode it. ๐Ÿ˜‰

Here’s a quick script for generating a caesar cipher, where each letter in the word is shifted by some common number. I’ve included a fast implementation of a decoder in circumstances where the shift seed is unknown. It takes advantage of the letter frequencies of the English language to make some guesses at possible shifts. It’s definitely imperfect, so I might go back and edit this post later if it comes to it.

Python A* path search algorithm with wall destruction

I recently stumbled across Google’s foo.bar challenge, and it’s quite an interesting set of problems! I got through the first couple levels without much difficultly, but level 3 is kicking my butt. I finally solved this maze problem after a day, so I’m posting it here to explain my thought process and because I’m pretty proud that I learned how to A* in the 5 hours or so I spent on the question.

Anyhow, the original challenge is twofold. First, given a maze represented by a grid of cells, find the shortest path you can take to get from the beginning to the end of the maze. Secondly, solve the maze again with the additional ability to now destroy one wall in your path. Though I’m sure there are many approaches one could take to solve this, I went with modified A*.

(No cheating on the challenge if you’re currently taking it! I really enjoyed the learning process I went though while taking the challenge, so don’t rob yourself of that opportunity! ยฐห–โœงโ—(โฐโ–ฟโฐ)โ—œโœงห–ยฐ)
[Read more…]

Smile detection using OpenCV + Designing a ROS node

An implementation of smile detection using OpenCV, cv_bridge, ROS and Python is available in full at the bottom of this post.

I’ve been developing in ROS for awhile now, and I’ve settled into a pattern for creating nodes that I find works really well for me. It has three components:

  1. Create a single class that encapsulates the entirety of what the node is supposed to do
  2. All subscriber callbacks are restricted to saving the content of the message to a member variable to hold until ready to be processed
  3. Have a run function in the code that is the main ROS loop, and calls the update functions to process data

Functionally, it looks like this:
(I’m using FaceFinder as an example)


[Read more…]

Image Steganography in Python

Second Title: Hiding Secret Messages in Cute Pictures of Dogs

What is this pupper hiding???

I developed this activity to as an interesting way to introduce students taking the foundation level software class at Olin to image manipulation and binary math. Since I spent quite a bit of time working on it, I wanted to publish the entire thing as a blog post in case other people on the web are interested in steganography. This exercise was modified from a similar one found at Interactive Python, though this version encodes an image into another image instead of ASCII text.

Because I wrote this as a learning exercise, there’s a neat little repo that holds starter code and solution code. You can clone/fork it here. The starter code is in steganography.py and has some missing functions for you to fill out, while the full solution is in solution.py. There are two main functions to this code. The first is a decoding function that can extract secret information from an image file, while the second is a function that can encode secret messages into images.

This code uses the Python Pillow library. You can install it using pip if the package is missing from your computer by typing sudo pip install pillow into your terminal window.

If you don’t wanna learn about how to do steganography and just want a neat script that will hide messages in images for you, you can use the code in solution.py. It is also embedded at the end of this post.

What is steganography?
In a nutshell, the main goal of steganography is to hide information within data that doesn’t appear to be secret at a glance. For example, this sentence:

Since everyone can read, encoding text in neutral sentences is definitely effective

turns into

Secret inside

if you take the first letter of every word. Steganography is really handy to use, because people won’t even suspect that they’re looking at a secret message–making it less likely that they’ll want to try to crack your code. In the past, you may have tried to accomplish this kind of subterfuge using invisible inks or using special keywords with your friends. However, as fearless coders we have access to fancier ways to sneak data around. *evil laughter here*
[Read more…]

Algorithm for solving Knights Tour with Python(revisited)

The original Algorithm For Knights Tour was a pretty popular post, so I decided to revisit it thanks to an interesting email I got. This implementation has a couple changes:

  • You can define a ruleset for locations the knight must be at any given move
  • You can define whether the path needs to be closed or not (Knight returns to starting position)
  • There’s a visualizer now! (You’ll need to have pygame installed for it to work though)
  • No more ugly exiting using sys.exit(), we catch custom exceptions this time around

Here’s what the visualizer looks like:
(pink because pink is the best color obvs)

tour.py

The tour code is mainly the same as the original post, but with the added custom exception class and booleans. Lines 137-146 deal with obeying to a custom tour ruleset 
[Read more…]

Sync iTunes playlists to Spotify

iTunes is dumb and has a super restrictive API. Spotify is awesome, and has an API for all my things, like my PS4 and Amazon Echo. But I like the iTunes UI, and it plays better with my local library than Spotify. As a result, I subscribe to both services, but it’s such a pain managing playlists between the two.

I present to you, the super not janky at all sync script/setup that I wrote up in a 3 hour burst of inspiration last night. The main flow of the idea is this:

  1. CRON job runs AppleScript periodically
  2. Applescript queries iTunes for playlist information and saves each playlist and its songs to a .txt file
  3. Python script compares .txt files to Spotify playlists – information retreived via API calls
  4. Python script updates Spotify playlists with differences found between the two services

AppleScript is a very interesting programming language, where the code is incredibly human readable at the cost of ease of use when coding. Here’s what I wrote for extracting playlist information out of iTunes:

This extracts playlist information and saves each playlist as a .txt file to a specified directory. The Python component does most of the legwork in terms of finding which tracks needed to be added to Spotify. You’ll need to create a new application at the Spotify Developers page to get a client_id and client_secret_key.
[Read more…]

Teaching cars to drive with genetic algorithms

“What the heck Sophie?!” You say, “Why are you writing a debrief for a project you did 3 years ago???”

Hahaha, please bear with me dear reader. If I don’t, I’ll forget about the cool thing I did as a freshman in college still stumblin’ through Python. And how cool and easy genetic algorithms are. ๐Ÿ˜€

Anyhow, carrying on….


For our software design final project, we wrote a simulation program that would allow you to draw a track, then generate cars that would eventually learn how to drive around said track. It was a pretty fun project, and we got a pretty cool result.

Here’s our website!
Here’s the code!

Demo video:

What’s happening is that the cars are starting with random parameters for their wheels relative to its distance from the wall. As the the cars progress through the generations, these parameters are selected using our parameters, such that the car that make it around the track is the only one that survives.

[Read more…]

Installing and setting up ROS in 5 mins

[*disclaimer: not really 5 mins. Downloading ROS will probably take longer than that lol]

ROS is a really great library for robotics research. It might even be good for not robotic applications, I don’t know, but my general feel is that I like ROS about as much as I like nutella. (read: a lot). The ROS install tutorial is really good at explaining the process, but it’s spread over too many pages and requires too much scrolling. I’ve consolidated the information here in one page for ease of use. Our robotics lab only uses ROS Indigo, so I’m writing install instructions with that in mind. Also, ROS only works well with Ubuntu, so I’m assuming you’re also using the correct OS.

Everything that follows, unless explicitly stated, should be typed in a terminal window. Okay, so the steps:

1. Set up sources.list
We need to add the ROS package sources to our sources list so we can actually find the ROS packages using the apt package manager. Open up a terminal window and paste the following:
sudo sh -c ‘echo “deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main” > /etc/apt/sources.list.d/ros-latest.list’

2. Set up your keys
Next we need to set up keys so Ubuntu will actually allow us to download + install the packages.
sudo apt-key adv –keyserver hkp://ha.pool.sks-keyservers.net –recv-key 0xB01FA116
If you get a gpg: keyserver timed out error, use this instead:
sudo apt-key adv –keyserver hkp://ha.pool.sks-keyservers.net:80 –recv-key 0xB01FA116

3. Install ROS via apt-get
Make sure the apt package index is up to date from when we added those new sources at step 1.
sudo apt-get update
Then install ROS!
sudo apt-get install ros-indigo-desktop-full
There are a couple of flavors of ROS that you can install, but for most applications, you might as well do the full install to make sure you get all the packages you’ll ever need.
[Read more…]

Algorithm for knight’s tour in Python

I’ve written an updated version of this code here. Please take a look! It adds a visualizer, ability to solve for closed tours and step requirements, and has a better exiting strategy. ๐Ÿ™‚


In Discrete class, we’ve been talking about planar graphs and stuff like Hamilton traversals. One of our in-class exercises involved the knight’s tour, and whether we could find a rule that would allow us to decided if a knight’s tour was possible given a chessboard of a certain dimension. I have a hard time understanding graphs, but the “Knight’s Tour” sounded really cool so I looked it up on Wikipedia. The problem is actually a pretty interesting one, so I decided to try my hand at implementing an algorithm for solving it in Python.

From Wikipedia:
A knight’s tour is a sequence of moves of a knight on a chessboard such that the knight visits every square only once. If the knight ends on a square that is one knight’s move from the beginning square (so that it could tour the board again immediately, following the same path), the tour is closed, otherwise it is open.

The .gif below is an example of what knight’s tour would look like on an 8×8 board.
Gif of knight's tour

In writing my own implementation, I heavily referenced this site and this site. I’ll do a walkthrough of my code below.

[Read more…]

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:

  1. Download SphinxBase and follow the install instructions
  2. Download PocketSphinx and follow the install instructions
  3. Download PocketSphinx-python and follow the install instructions
  4. Run the code below

[Read more…]