Recursive batch Script to get latest/static file name – SyedTips – 7

If you followed my earlier tip, notice how I always passed in Image.png as an image file into tesseract to get a text value? This is totally fine if you can source static Image files and make sure they are available at a particular location.

However, what if you’re using a live camera that saves a stream of Images? I used mjpg-streamer to live stream images on a web browser and also save them on a Raspberry Pi.

Following is the bash command that captures images from a USB WebCam and streams it over:

./mjpg_streamer -i "./input_uvc.so -r 640x480 -f 1" -o "./output_http.so -w /usr/local/www" -o "./output_file.so -f /home/pi/Camera_Output -c /usr/bin/mjpg-streamer-overwrite.sh"

The above command does the following 3 things:

  1. Stream Images over the Web/Browser
  2. Save Images to a local folder on the Raspberry Pi
  3. Invoke a custom Script

Here’s how the local folder containing the saved streamed image files looks like:

filenames_sample

Now here’s the challenge: Firstly, tesseract can only take in a defined fileName as an argument and since we’re invoking tesseract from Python, we can’t simply use a script to get the latest file name and pass it as a parameter.

So here’s what I did to solve this: Get the latest file and rename it to Image.png in that same folder. So at any instance, the latest captured image file is always Image.png. Now, simply pass this as an argument into tesseract and all’s done 🙂

I used the following code snippet to get latest file pointed as Image.png:

# move the temporary file to a given filename
cd /home/pi/syed

cp -f -T `find -type f -name '2015*' -printf "%C@\t%P\n" |sort -r -k1,1 |head -1 |cut -f 2-` /home/pi/syed/Image.png

I saved this snippet into mjpg-streamer-overwrite.sh which is being invoked on every Image capture as shown earlier.

P.S: A more detailed script that shows debug messages:

#!/bin/bash

# comment out the following line to disable messages
#VERBOSE=1

if [ $VERBOSE ]; then
  echo -e "Rename script started at:\t$(date)";
  echo -e "Parameter \$1 is:\t\t$1"
  echo -e "current directory is:\t\t$(pwd)"
  echo -e "move command is:\t\tmv -T -f \"$1\" 'Image.jpg'"
fi

# move the temporary file to a given filename
cd /home/pi/syed
cp -f -T `find -type f -name '2015*' -printf "%C@\t%P\n" |sort -r -k1,1 |head -1 |cut -f 2-` /home/pi/syed/Image.png

RES=$?
if [ $VERBOSE ]; then
  echo -e "move command returned:\t\t$RES"
fi

Thanks 🙂

Invoking Shell commands from Python – SyedTips – 6

While building an ALPR (Automatic License Plate Recognition) System on a Raspberry Pi in 2013, I came across tesseract  which is a very powerful OCR (Optical Character Recognition) tool.

Tesseract is a command line tool which means it has to be invoked as a command from the shell/terminal (and it works on both Windows and *nix):

tesseract imagename outputbase [-l lang] [-psm pagesegmode] [configfiles...]

Since my main image capture and trim script was based on Python (triggered from a WebPage), I had to find a way to invoke tesseract from within Python. At first, I looked out for python libraries and wrappers of tesseract and had a hard time getting them to run on the Raspberry Pi.

Eventually, I wondered if there was a way to invoke shell commands from Python; and yeah!

Using subprocess, you can do just that:

import subprocess
subprocess.call(["tesseract", "/home/pi/syed/Image.png", "/home/pi/OpenCV/OCR_Data"])

Pass each argument to the invoked command separated with , in “”

Thanks.

Buying Raspberry Pi 2 in India, Hyderabad

I get most of my Raspberry Pi and related stuff from PotentialLabs (where I also consult on Tech stuff). If you guys are looking at buying the Raspberry Pi 2 at a reasonable price, you can buy it off from here: http://potentiallabs.com/cart/raspberry-pi-2-hyderabad-india Continue reading “Buying Raspberry Pi 2 in India, Hyderabad”

Eclipse release IoT stack

The IoT (Internet of Things :P) space is getting pretty interesting. Eclipse (one of my fav IDEs) has now released a powerful IoT stack for Java developers to build awesome IoT applications.

Eclipse IoT

 

Check out more details here: http://iot.eclipse.org/java/

This is getting interesting 🙂 I need to try out a demo… Hey, they even have a live demo page setup here that’s playing with the Raspberry Pi: http://iot.eclipse.org/demo/#/devices/%23

Happy IoTing 😛

Raspberry Pi Radio

Came across this interesting (and minimal) Raspberry Pi project on Makezine:

makezine.com/projects/raspberry-pirate-radio/

This is pretty interesting as all you now need to have a broadcast FM channel is a Pi and a Wire 😉

Note: Unauthorized sending of signals over FM bands is restricted and also a punishable offense in some countries. I take no responsibility whatsoever.

Using Raspberry Pi to snoop into Wearables

So,

The world of wearables is taking the world by storm. Android Wear, iWatch, Smartbands, Leychal (Smartshoes, proudly made in Hyderabad, India), etc.

Here’s a nice anatomy of a wearable device:

And here’s the many sensors found in a generic Smartphone:

Imagine if you could grab all those data using off the shelf devices?

Introducing…..Blueberry

Symantec wrote a nice post about how insecure these new wearables are and how they can be snooped into.

Disclaimer: All images used above have been taken from here and may be copyrighted by the owners.

Android 4.4 on the Raspberry Pi

Awesome!!!

trevd just released his work on porting Android 4.4.4 to the Raspberry Pi. He wrote an elaborate post on the XDA forum here detailing the kinky features of what he’s done.

Instructions for getting started can be found on his github repository.

I’ll try this out myself and hopefully let you guys know how it goes! This is super exciting for me 🙂

[tag Raspberry Pi, Android]