Python Forum
Script for a good cause
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script for a good cause
#1
Hi!

I was hoping you could help me with a short script that will likely take you guys 5 Minutes to put together while I would spend 20 nights and get inferior results.

The goal is to provide a sequence of values that will enable Arduino users to animate a drop on an addressable LED strip. I will upload a video of the result to youtube and provide the values to anyone who wants to do something similar.

The physics of free falling are simple but for the formation of a new drop, I'd like to use this real life example: youtube.com/watch?v=c4MUTij8f6I&t=202s

Of course I will center the video and clip it to contain only one drop, coming from the top (like around the 3:30 mark).

I believe Python is ideal for the task but my knowledge is very basic. I can read most code and fix simple stuff that doesn't work but I don't know any of the libraries' capabilities so I'd be searching forever to do this. Algorithm looks like this:

Script that accepts 3 parameters: (x, y, Path and Filename to a video file)
Take every xth frame from a given video (or sequence of pictures, if that is easier)
Convert picture to black and white
Every yth line (row) in the picture report back the first occurance from the left (column) of a black pixel. (-1 if no black pixel in that line).
output on stdout: current X [tab] current Y [tab] Position of black pixel
Once I have the data, I will analyse if it can be approximated by a 3D function or whether a look-up table will be better and make the arduino part public.

Can you help me with that?

Thank you very much!
Hajo
Reply
#2
(Jul-04-2017, 09:58 AM)Rapefruit Wrote: I was hoping you could help me with a short script that will likely take you guys 5 Minutes to put together while I would spend 20 nights and get inferior results.

Most likely, but we need to see what you've tried so far. It helps us gauge the correct answer to your specific question. Right now, we don't know what OS your using (as Arduino's native language is not Python), what version of Python you are using or plan to use or if you even know how to use the command line. This is not a slam, just pointing out basics that we prefer to know.

As for searching, many will tell you, myself included, they spend a great deal of time searching, in my case, I probably spend more time searching than writing actual code. :-D
As always, the first place to start is the Python Documents themselves. Another good place is our own Scripts and Tutorial sections.

In short, help us to help you.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
Makes sense.

I use Windows 10 64bit. I have a very old 2.7.3 installed that came with another software but I'll gladly update if it makes a difference.

Honestly, I haven't even started. I struggel with the "everything is an object" philosophy. I come from a procedural background back when...

If I had to do it with what I know, I would try to throw together ffmpeg, imagemagick and maybe some hex find routine to get the data. But I guess I would fail to wrap all these tools in a working script and end up doing much of the handling manually. To keep it manageable, I would reduce the amount of frames and the number of lines evaluated and that is what I meant by "inferior results".
When thinking what would be a good way to tie it all together, I remembered Python and I guess the tasks of extracting a frame and isolating a line from a picture can probably be done by it as well as by the external tools I would invoke.

I know I'll be able to adjust and use the script to debug it to fulfill it's one-off prototype job and I am comfortable with using the command line and pipes etc. to do so. I think a framework .py file, some pseudo-code and pointers in the direction of the right libraries would enable me to finish it.

Thank you very much!
Hajo
Reply
#4
(Jul-04-2017, 10:40 PM)Rapefruit Wrote: I use Windows 10 64bit. I have a very old 2.7.3 installed that came with another software but I'll gladly update if it makes a difference.
Follow this
Then pip will work better for hint that i am gone give.
3.6 is also a big release for Python.

Quote:Take every xth frame from a given video (or sequence of pictures, if that is easier)
MoviePy pip install moviepy
Desaturates the picture, makes it black and white.

An other option.
Creating and exporting video clips

After export to images,can use Pillow
MoviePy also install Pillow.
>>> from PIL import Image
>>> from PIL import ImageEnhance

>>> im = Image.open("logo.png")
>>> enhancer = ImageEnhance.Color(im)
>>> black_white = enhancer.enhance(0.0)
>>> black_white.save('logo_black.png')
[Image: ZPq4Wo.jpg]

Quote:Every yth line (row) in the picture report back the first occurance from the left (column) of a black pixel. (-1 if no black pixel in that line).
Some tools.
Pillow, OpenCV(countNonZero(img == scalar_value_black),
scikit-image, scipy.ndimage.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020