"In the world ye shall have tribulation: but be of good cheer; I have overcome the world." –John 16:33

San Pedro Garza Garcia

Tag: Python

Beansorter: GUI and live video up and running

The browser based GUI for the bean sorting project is now up and running and being served from the Raspberry Pi. I only have one camera running right now because I only have one camera, but it does all the things that need to be done. There is a lot underneath the hood on this thing, so it should serve as a good base for  other embedded machine vision projects beside this one.

In terms of particulars, I am using a Flask (Python3)/uWSGI/nginx based program that runs as a service in the Raspberry Pi. Users access this service wirelessly (anywhere from the internet). The service passes these access requests to the C++/OpenCV based vision application which is also running as a service on the Raspberry Pi. Currently, we can snap images show “live” video, read the C++ vision log, and do other such tasks. We probably will use something other than a Raspberry Pi for the final product with a USB 3.0 port and the specific embedded resources we need, but the Raspberry Pi as been great for development and will do a great job for prototypes and demonstration work.

The reason I put the “live” of “live” video in scare quotes is that I made the design decision not to stream the video with gstreamer. In the end applications I will be processing 1 mega-pixel images at 20-30 frames per second which is beyond the bandwidth available for streaming at any reasonable rate. The purpose of the live video is for camera setup and to provide a little bit of a reality check at runtime by showing results for each 30th to 100th image as a reality check along with sort counts. There is no way we could stream the images at processing rates and we want to see something better than the degraded streamed image.

BleAx rewrite: Introduction

My daughter Kelly drew a comic strip called Betty Blonde five days per week for two years starting when she was thirteen years old. I wrote a program called BleAx a few years back to help her accumulate the four hand drawn panels of her daily comic strip into a single image with a title, date, copyright, borders and that sort of thing. The program allowed the her to automatically upload the strip to a website for display. I did the whole thing by hand for about a year, then spent about six months writing BleAx whenever I had an hour or so free, here and there. BleAx stands for Betty Blonde Aggregator of Comix.

I wrote BleAx in Python and still have it, but have decided to rewrite it as a learning exercise. I normally write programs in C/C++ in my day job, but have recently been wrapping some of the time critical stuff I write in C++ in a Python wrapper so engineers who do not normally write in a “non-garbage-collected” language can use it easily. I now have started using a set of libraries called PySide to write Qt GUI’s in Python. It took me a bit of time and hassle to get my environment set up to automate the GUI development and C/C++ wrapping in so I did not have to go through a ton of manual processes to build the programs and put the results where they needed to be. I do a lot of work with OpenCV so I will talk about how to use that effectively in this environment, too.

I am sure my process is not perfect and that is part of the reason I am doing this publicly, so some of the people that might read this can beat up my process and tell me how to do it better.  To that end, I am going to start rewriting BleAx. I do not have a ton of time, so this will be a little bit of a slow process. I am mostly doing it just for fun and documentation, but if it helps anyone else, that will be great.

Betty Blonde #222 – 05/22/2009
Betty Blonde #222
Click 
here or on the image to see full size strip.

Programming and comic strips: An example

Kelly has decided she wants to start drawing her comic strip again as she has time. You can see an example of what she did before at the bottom of this post. She is going to do something different now, but plans to do a four panel strip like before. I wrote up some code to accumulate the hand draw single panels, add titles, copyright notices, borders, etc, and then post the results to our website. I could easily dust off the old code and use it again for her current efforts, but have decided to try to rewrite it as a way to improve some new skills on which I am working.

The idea will be to write a series of blog posts on how to set up an environment to write low level code that needs to go fast in C++ and the GUI and everything else in Python. I plan to talk about how I use Qt Creator, OpenCV, SWIG, PyCharm, PySide, some batch files, and a merge tool to automate this and make it easier. I am at the beginning of another series called Our Homeschool Story that I will also continue, but this is a very different thing that will provide some variety. I hope to start sometime over Christmas break.

Betty Blonde #220 – 05/20/2009
Betty Blonde #220
Click 
here or on the image to see full size strip.

More C++, Python, PySide, SWIG, OpenCV and an early Christmas gift

We received a great Christmas gift last night. The big, big boss of our company in Sydney (not just the big boss from Prescott) wrote a letter and gave us an extra five days of vacation over the holidays because we had such a tough year and because of some health issues in our new executive team. The reality is that no one has taken much of a vacation over the last two years and most of us have worked just about every weekend. It will be nice to spend a couple of unfettered weeks with the family.

Just as good, I have been put on a project that involves writing programs in two different languages using a couple of libraries I really like in both of those languages.

“Why two languages?” you ask.

Well, C++ is a language that is very good for doing things very efficiently and effectively, but that can really get you in trouble if you do not know what you are doing. Well written C++ code generally runs much faster than code written in higher level languages like Python. It lets you do just about anything you want and does not provide any restrictions with respect to leaking memory or jumping off into areas of memory that are totally unrelated to what you are doing. Python is a great language for people who are not so comfortable with the freedom of C++. It also allows user to write a lot of functionality fast and has lots and lots of add-on libraries to do lots and lots of things easily.

I normally use C++ because of the need for speed. Other members of my team need to use my code in programs they can develop rapidly for use in scientific experiments and production code for the instruments we make. So, we have decided that I will write my machine vision code in C++, then wrap it up in a Python wrapper using a tool called SWIG. All the tools I normally use in C++ to build GUI’s (Qt) and perform image processing tasks (OpenCV) are available in Python as libraries. The Qt libraries we use are called PySide and the OpenCV libraries are just called Python OpenCV.

I have set up my environment so that whenever I write a C++ library, the Python wrapped results are automatically built and stuck into the correct directory for use by the rest of the team. In addition, when I build a GUI with Qt Designer, I can run a batch file that turns the C++ code into a Python program. I have to do a little merging with that if I change the GUI, but it is all quite painless. I think I might write up what I have done and post it here. I am sure I have some inefficiencies and someone might be able to make some suggestions.

Betty Blonde #219 – 05/19/2009
Betty Blonde #219
Click 
here or on the image to see full size strip.

No Shave November (more than Movember), Python, C++, PySide and SWIG

What you see below is a little program I have worked on over the last week or so. It does not look like much, but there is quite a bit under the hood. The GUI is built with something called PySide which is an open source version of the wonderful C++ Qt libraries in a Python wrapper. It is amazing how easy it is to use. I knew nothing about it less than a week ago. In addition, I built a C++ capture library based on OpenCV (I will switch to camera specific libraries when we decide what camera we want to use for our project). I then used SWIG to add a Python wrapper around the library. I write this, not because I think it is particularly interesting to anyone, but because I am having a great time learning some new things, there appears to be a lot more coming and it will be good to remember what a good time I had doing it a few years from now.

You might notice that I got a little jump on No Shave November. I decided to go with the beard, partly because I have been away from home quite a bit over the last month, so Lorena has not been able to wrestle me down and shave it all off. If I can just last until Thanksgiving…

Python/C++ GUI for Machine Vision Development

Betty Blonde #212 – 05/08/2009
Betty Blonde #212
Click 
here or on the image to see full size strip.

Getting out the product

The small team of engineers with whom I work spent last week, the entire weekend and yesterday putting the final touches on two of the retinal imaging cameras we designed so they can be shown at a conference in Colorado starting tomorrow. Everyone who has done real engineering has participated in this type of sprint to the finish. Really, I do not get a lot of satisfaction out of my work unless I get to do something like this a couple of times per year. We worked 16 hour days and barely made it, but the cameras will do well at the show.

The times when I work hard like this are the times I remember and cherish most about my work life. These times are when big advances are made in product understanding and, more importantly, lasting friendships are built. I love that. At the same time, now we are all completely fried. One of the guys took the day off. I am going to work on my really fun programming project (cameras, Python, SWIG and C/C++) and get back to blogging. We will have to work hard until Christmas, then take it up again for a rush to another product milestone in March, then it will be on to a new product for me.

So, that is why I have missed a few days of blogging. I hope to continue my Our Homeschool Story tomorrow.

Betty Blonde #208 – 05/04/2009
Betty Blonde #208
Click 
here or on the image to see full size strip.

Programming Python (again)

I have been given a project at work to write a program to control a camera, some motors and I/O and perform some machine vision. That is pretty much what I do anyway, but the high level parts of the project are to be developed in Python with the low-level, “need to go fast” parts to be written in C/C++ with a Python wrapper. I am going to be able to develop some new skills and play with some new toys, but will have to slow down my Our Homeschool Story posts a bit. I hope to keep going but not a post a day, at least for awhile.

Betty Blonde #207 – 05/01/2009
Betty Blonde #207
Click 
here or on the image to see full size strip.

How to teach computer programming to kids (Part 9) A suggested curriculum

Day 617 of 1000

This is the ninth in a series of posts on how we taught our children to program, what we did wrong and how we think we could have done better.  You can see the introductory post and index to the series by clicking here.

[Previous post]

This post describes how I think I would teach an eleven year old kid how to program.  I explained the many things we did wrong and the few things we did right in teaching our kids how to program when they were young In the previous posts in this series.  We really did not do so badly with our son.  He is a good programmer with a solid knowledge of the fundamentals of object oriented programming in C#, C/C++, and Python.  His college level Introduction to Java class was a trivial exercise for him.  We did horribly with our daughter.  So much so that she struggled mightily with the same Introduction to Java class at which our son had excelled.

We have a good (certainly not perfect, but good) track record teaching our kids things like reading, math, art, typing, and all kinds of other stuff, so I have tried to think about what I did wrong and how I might do it if I had it to do over.  I think the main reason we did not do so well is that we did not see programming as an essential skill.  We were wrong.  All STEM majors need to know how to program and that knowledge is a huge advantage to kids who enter a STEM degree in the University.

What language should you chose?

So, what could we have done to teach our kids programming at an early age?  There are a lot of small concepts a new programmer has to learn before any attempt is possible to learn the big picture programming ideas, particularly object oriented programming.  There are lots of books that explain these details quite well.  A language that handles some of the minutiae for the new programmer so they can learn these small concepts one at a time is a big advantage.  Languages like C#, Python, and Java all qualify.  C/C++ does not for reasons #3 and #4. There are good reasons for selecting one of these languages:

  1. These are serious languages for serious programmers that can perform anything from small embedded applications for single board computers to game programs running on personal computers to full-blown, big data, internet enabled monster programs running on “big iron”.
  2. They have large sets of libraries that the new programmer can use without a lot of knowledge about what is going on under the hood.
  3. Garbage collection (a technical detail we don’t need to go into here) is managed by the language so the new programmer can concentrate on learning the small concepts without the program breaking for mystifying reasons.
  4. There are very good Integrated Development Environments (IDE) for all of these options.  An IDE is a program that is used to write programs.  It has an editor, a compiler, a linker–everything need to write, debug, and run a program in an easy to use environment that usually has a hint systems (intelli-sense) for computer commands in case the programmer forgets what he should type next.
  5. There is a TON of documentation, tutorials, examples, and books on the internet and in print for all these languages.

We started in C# because it meets all the criteria above, I had good knowledge of the language, and we found what looked like a good tutorial book on C# that featured something of interest to my son.  That was a great reason to go that direction at the time.  I think if I had to do it over, I might start with Python.  There is no particular reason that Python would be better than C# or Java, but I am aware of a number of cool, hobby projects written in Python and I would like to get a little deeper knowledge of Python myself.

What book should you chose?

Programming the Raspberry Pi, Getting Started with PythonThese days, it is possible to get all the information required to program online.  I have learned languages that way (Python and R).  Nevertheless, for a sit down class with an 11 or 12 year old kid, it is nice to have a book to follow.  In addition to the book we used to get started (Beginning C# Programming),  I have been looking at a book with great reviews titled Programming the Raspberry Pi, Getting Started with Python.  The thing that is great about this book is that, if you already have a USB cable and a reasonably capable PC, you can be complete set up with an embedded computer to write robotic, internet, game, and other kinds of programming for less than $100.  Here is the blurb from Amazon for the book, a Rasberry Pi embedded computer, and the Raspberry Pi user manual:

Rasberry Pi + Manual + Python book

The reason both the books are good is that they start from ground zero, they feature good starting languages as defined above, and they have project goals that are likely to interest kids of the age of 11 or 12.  The Raspberry Pi is particularly good because it features a chunk of hardware to which the kids can hook up other stuff and/or connect to the internet.  These are just examples, but the concepts behind both these books are great.

How should I do the teaching?

This is an especially good question if you, yourself know nothing about programming.  The beauty of the above setup is that the books walk you through the set up of the computer, the download of the language and IDE, etc.  They give button push by button push instructions.  If you do not know anything about programming, this will teach you.  As for your kid, it has been my experience that at 11 or 12, kids generally love to do stuff one-on-one with their parents, even if they sometimes do not admit it.  The one-on-one approach is the right way to go.  Here were my rules for a structured program like this:

  • Go by the book.  You might think you have a better idea.  You might ACTUALLY have a better idea, but if you stick to the book you will be assured you and your kid have all the materials to go on to the next chapter.
  • Sit down with your kid for 20-30 minutes per day, at least four days per week.  Be present with him the whole time, but quit after the alloted time so you want to come back for more.
  • Let him do ALL the reading.  Have him read every word, aloud.  Talk about it when you or he don’t get it.  Look up explanations on the Internet when needed.
  • Sit on your hands.  Let your kid hold the book and turn the pages.  Let you kid plug in the computer. Let your kid download and install the programs and software.  Let your kid do ALL the typing.

That is all I have.  I wish I would have done it that way.

How to teach computer programming to kids (Part 6) C++, Python, and browsing the web with a dumb phone

Day 612 of 1000

This is the sixth in a series of posts on how we taught our children to program, what we did wrong and how we think we could have done better.  You can see the introductory post and index to the series by clicking here.

[Previous post]
[Next post]

C++ Primer PlusPart four of this series describes how Christian studied C# during the first half of the homeschool year when he was 11. Part five describes several computer software projects he performed during the second half of that same year.  At that point, he had gotten to know quite a lot about the build process (compiling and linking).  He could do some procedural programming in C# with a fairly top level understanding of object oriented programming.  He could program pretty well in PHP and use Drupal to build a good website.  He had modified a pretty sophisticated C program to run on a new platform.

All that he accomplished was great, but he had such a strong interest in the topic, it was pretty shameful I had let him do virtually all of this on his own with almost no tutoring from me.  So the next year, when he was 12, I decided I would help him learn C++ so that he could learn more about pointers and garbage collection as well as get a deeper understanding of object oriented programming.  We did something similar with C++ as what we did with C#.  Christian began to work his way through the book for about fifteen or twenty minutes per day.

We set up Christian’s computer with the Unbuntu Linux, the Qt libraries, and Qt Creator as an IDE.  I should him how to work in that environment and he got started.  I sat down with him more than previously, but more of my time explaining concepts and performing code reviews would have helped the process a great deal.  After two or three months, we decided he needed a project.  He decided he wanted to be able to be able to send commands from his SMS phone.  It was not a smart phone, just a phone that could make calls and send and receive SMS and MMS messages.

He created a gmail account just to pass messages between his cell phone and his Linux computer running as a server at home.  When he wanted to do something on his computer from his phone, he would send an SMS message to the gmail account.  The Linux computer continually looked for message on the gmail account.  When it received a message, it interpreted it, ran the command on the Linux computer and sent the message back to the phone via SMS message.  He got his program up and running fairly well about half way through the school year.  About then, he was getting a little bored because he had finished this project and moved further up the curve with respect to object oriented programming and the lower level aspects of C++ programming.  He was not great at it, but was no longer a beginner.

I knew we needed to do something to maintain his interest because we had started to bog down.  He had the idea that he would like to figure out a way to use his SMS/MMS phone (not a smart phone) to browse the internet.  He found some Python libraries that would make this task easier, so he asked if he could switch to Python for awhile.  I thought that was a great idea.  Again, the thing that keeps him engaged is a project he really wants to make work.  Python is an object oriented language that would serve to continue solidification of his object oreinted programming skills.  At this point, for reasons mostly having to do with work and the kids other homeschool subject matter, I disengaged from my teaching efforts with respect to program.

Over the rest of the year, Christian was able to get a fairly amazing program up and running on his Linux server.  It did the following:

  • An SMS/MMS phone sends an SMS message that holds a URL for a specific web page to a specific gmail account.
  • Christian’s Python program running on a Linux server (at home) continually checks for emails arriving at the specific gmail account.
  • If an arriving message holds the URL of a web page, the following sequence is performed by the Python program:
    •  A web browser is opened to the web page specified by the URL.
    • A screenshot of the web page is captured and saved as a jpeg image.
    • Numbers for the links are added to the image.
    • The image is transmitted back to the calling phone as an MMS message.
  • When the phone receives the image of the web page back, the user can send back the number of a link on the page and the Linux program will transmit that web page back to the phone as an image.
  • If the user selects an input field by selecting its number from the web page image and adds some text after the number, the phone will enter the input text into the input field.  That is how he could enter the username and password to check his email accounts and/or enter restricted web sites.

Again, the first part of the year was a little bit tedious for Christian until he got to the point where he could do a project that interested him.  He came way up the programming curve specifically because of that.  He was not a great programmer by the end of the year, but he was pretty good for a 12 year old and had two great little programs and some cool stuff to show his friends to show for it.

Computer programming for kids – a new series of posts

Day 583 of 1000

It is my daughter, Kelly’s, birthday today.  She is now a 19 year old Statistics major at North Carolina State University and is taking her first two formal programming classes, Statistical programming with SAS and Java.  She has also programmed with the R statistical programming language.  She enjoys programming a great deal, but is a little frustrated with her Java class.  Kelly is not frustrated with the material; she enjoys that.  She is frustrated because I taught a lot of programming to Christian, but virtually none to Kelly.

She said, “Dad, why did you teach Christian how to program and not me?”

I said, “Because you enjoyed other stuff like art and crafts and Christian wanted to know how to program his Palm Pilot.”

She said, “You should have taught me, too.  I need to know how to program now and I am having to learn it from scratch.”

“You really have to have something you want to do with programming or it is really boring,” I replied.

“You made us do Mavis Beacon Typing 15 minutes every day for two years and we didn’t have any real use for it until years later.  It was really boaring at the time, but got A LOT out of being able to type faster and better than everyone else.  We are really glad you made us do that.”

All this was true.  I think I failed Kelly in this.  Christian learned how to program on his own, but I bought him the learning materials, made computers available to him, and vmade a program of study that was both systematic and and integrated part of his homeschool curricula.  The reason we did all this for Christian was because he had something he wanted to accomplish. I should have thought to teach them both how to program whether they wanted to or not.  The program we put together for Chrisian has given him a huge leg up both in class and with work opportunities.  Any student who plans to get a hard (STEM) degree, would benefit from such a study program.  I am just sorry I did not do this for Kelly.

I have decided that, when I finish my current series on Why not skip high school?, I will write a series on how we taught Christian to program.  I will link to that series from this post as soon as it is started.

Powered by WordPress & Theme by Anders Norén