Why I’M Really into python Right now

Primarily, I’m an embedded software guy. With that said, most of my programming experience has been with C/C++. Don’t get me wrong, I’ve used other languages, but over the last 10+ years I’d say that C/C++ are what I have spent the most amount of time using. In fact, there are still good reasons for only using C in certain projects. One of the foremost reasons is that C is typically the first language (other than assembly) that receives support from chip manufacturers. This makes C an ideal candidate for making sure your software is cross platform (Hello RISCV). If you’re a Linux developer, then the C ingrained into the culture itself, with Linus Torvalds talking about why C++ is a poor choice for a Linux kernel.

With that said, I’m probably never going to give up C. At least not in the near future.

So if I’m an embedded guy, why do I like Python so much? Up until recently there wasn’t much choice in connecting software to hardware in under 8 hours. Sure, you could do everything in C, but that can take considerable effort. There was C#, but up until recently, .NET wasn’t open source and you had to use Mono and implement your own GUI. There was LabView, but the problem with that is that it’s a visual programming language (which makes it very difficult to maintain over the course of a decade), and you might find yourself locked into the National Instruments ecosystem. There are probably hundreds of ways to connect your software to hardware, but none of them stand out as being particularly easy.

Python is probably the first language I’ve used where things start to get stupidly easy. In about ten minutes I was able to download and install OpenCV, Tesseract OCR, and get demo code running. I wanted to do some thresholding on an image, to later send it into an optical character recognizer (OCR) for further processing. I’ve actually done this before entirely in C, and while this not particularly difficult to do in C, it did take me an entire afternoon before I started seeing any meaningful results. In part this was because the pure C side of Tesseract doesn’t seem to be as well maintained as the C++ side, but that’s neither here nor there.

I took two examples for Python OpenCV and Tesseract and just kind of smashed them together… and it just works. I had to make some minor modifications to the examples, but nothing extreme

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

from imutils.object_detection import non_max_suppression
import numpy as np
import pytesseract
import argparse
import cv2

try:
    from PIL import Image
except ImportError:
    import Image
import pytesseract

img = cv.imread('sudoku 001.jpg',0)

blur = cv.GaussianBlur(img,(5,5),0)
ret3,th3 = cv.threshold(blur,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)

cv.imwrite( "output.jpg", th3 );

strImg = pytesseract.image_to_string(Image.open("output.jpg"))

print('Detected this:')
print(strImg)

Before and after processing:

The output result was the following:

Detected this:
~ SUDOKU
To solve the puzzle, all the blank
cells must be filled in using num-
bers from 1 to 9. Each number
Can appear once in each row, col-
umn and in the nine 3×3 blocks.
You can successfully solve the
puzzle just by using logic and the
process of elimination.
ls

Yesterday’s Answer:
Solution to 2/10/11
© 2011 Janric Enterprises Dist. by creators.com
: 7
RB. Ry q
Ln AN
rrr .
Be ah S
Pap 5
Process returned 0 (0x0) execution time : 1.024 s
Press [ENTER] to continue…

What I was really hoping for was to get more information on the numbers themselves and not the text on the right, but honestly, for 10 minutes of work this is fantastic. At this point I’d probably start making a GUI to make it easier to tweak inputs, or maybe even connect to a web camera to start live processing… Which by the way, python makes it stupidly simple to connect to a webcam.

There are some drawbacks to Python… like formatting being part of the language itself, implicit type declarations and casting, pretty gnarly and (in my opinion) difficult to read syntax for things like lambdas, interfaces are a separate library entirely and not part of the language, ect… Overall though, it’s a pretty awesome language with a lot of support and an ecosystem which makes incredibly easy to use.

Leave a Reply

Your email address will not be published. Required fields are marked *