1

So I've been wanting to make a timelapse while using my resin printer. There are some known ways to do this this this: https://www.hackster.io/ryanjgill2/msla-smooth-time-lapse-199d87

or by using a DLSR camera and a photoresistor via a 2.5mm jack to remote trigger.

My wife is very guarding of her DLSR so that is out of the question.

I am by all means not a coder, I know how to setup configs for printers, and I want to believe that I can understand some lines, but I'll just stick to what I do best, mechanical design engineering and modelling!

So back to the Hackster model.
I have a Pi already connected to the printer as I run it off OctoPrint.

Though included timelapse in OctoPrint won't work as I can only call on a given time interval the result is lacking.

The Hackster model runs of a PiCamera, I do not have such and I think 90 € is a high price for what it does, so I must rely on my C920HD USB camera.

Looking at the code made by Ryan:

from gpiozero import Button
from picamera import PiCamera
from signal import pause

import time

camera = PiCamera()
camera.resolution = '3280x2464'
currentTime = int

def capture():

    currentTime = int(round(time.time() * 1000))
    image_path = '/mnt/usb/photos/image_%s.jpg' % currentTime
    camera.capture(image_path)
    print('Image captured: %d' % currentTime)

button = Button(14)
button.when_pressed = capture

pause()

I can conclude that I need to edit some of it.
I already know that my C920 can be used via fswebcam or ffmpeg and I know the resolution, though I do not know what is actually needed to be defined here?

Either way the full command to take a picture could/would look like this using fswebcam:

fswebcam -r 1920x1080 --no-banner /images/image1.jpg

I also know that I can use os.system to handle this.

So with the little to no knowledge I actually hold, I came up with this, but I'm obviously here because it didn't work.

import time 
import os 
from gpiozero import Button
from signal import pause

currentTime = int

timelapse = os.system('fswebcam -r 1920x1080 -S 3 --jpeg 50 --save /mnt/usb/photos/image_%s.jpg' % currentTime)

def capture():
    currentTime = int(round(time.time() * 1000))
    image_path = '/mnt/usb/photos/image_%s.jpg' % currentTime
    camera.capture(image_path)
    print('Image captured: %d' % currentTime)

while True:
    
    os.system('fswebcam -r 1920x1080 -S 3 --jpeg 50 --save /mnt/usb/photos/image_%s.jpg' % currentTime)

button = Button(14)
button.when_pressed = timelapse

pause()

And I would like to create a folder for every new timelapse I run, for the ease of it.

After shooting the pictures I would use:

ffmpeg -framerate 120 -pattern_type glob -i "photos/*.jpg" -s:v 1920x1080 -c:v libx264 -crf 20 -pix_fmt yuv420p timelapse.mp4

But as of writing this, it just occurred to me that I'm trying to use both fswebcam and ffmpeg combined.

Have I screwed up completely?

I can clarify the GPIO etc. if this needs but assuming that the lot here has an idea of what's going on.

Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117

1 Answers1

0

There's certainly nothing wrong with using appropriate tools for different parts of the job - fswebcam is great for grabbing images from the USB web cam, and ffmpeg is great a converting media from one sort to another.

There are some other options for accessing a USB webcam through Python, but those are somewhat more involved.

If you're happy stringing commands together as you've done there, then the following should work:

import time
import os
from gpiozero import Button
from signal import pause

imageFolder = '/mnt/usb/photos/'

# Method to capture an image from the webcam
def capture():
    timeStamp = int(round(time.time() * 1000))
    image_path = '$simage_%s.jpg' % (imageFolder, timeStamp)

    # Just call fswebcam to store images
    os.system('fswebcam -r 1920x1080 -S 3 --jpeg 50 --save %s' % image_path)
    print('Image captured: %d' % currentTime)

# Method to create a video from a folder of images
def createVideo():
   os.system('ffmpeg -framerate 120 -pattern_type glob -i "$s*.jpg" -s:v 1920x1080 -c:v libx264 -crf 20 -pix_fmt yuv420p timelapse.mp4' %imagepath)

button = Button(14)
button.when_pressed = capture

pause()
Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117
  • 1
    Much appreciated. And sorry upfront, as I'm not used to the way this site operates. Instead of taking a picture every 5 sec or so. I did plan on using the button 14 already in there. This is a GND GPIO on the Pi where the photoresistor is plugged in. So every time it sees light when it prints, it will trigger the camera. This setup still has it's flaws, as it will take a picture every time the resistor is exposed to light, so I will need to start/stop the script. But I do think this can be archived through OctoPrint via a plugin to enable/disable a script on print start/stop – Michael Christensen Mar 09 '22 at 12:43
  • No worries - I'll admit that was something I've hacked up here - I've not dug out my Pi to test it. Gotcha on the on the automation side of things, I did wonder why you'd been going for a button to press it when these prints can take some time ;) – Zhaph - Ben Duguid Mar 09 '22 at 15:26
  • @MichaelChristensen I've updated my code to reinstate those features. – Zhaph - Ben Duguid Mar 09 '22 at 15:42
  • 1
    You're a legend! Turns out I wasn't that far off I guess, I just got home, I'll test it right away. – Michael Christensen Mar 09 '22 at 15:58
  • As a head's up, there's a dedicated [RaspberryPi StackExchange](https://raspberrypi.stackexchange.com/) for all things truly Pi based ;) – Zhaph - Ben Duguid Mar 09 '22 at 19:52
  • 1
    Cheers, I did have time to try and run it through my Pi. Apart of me not using the correct button the script does seem work, well somewhat. Triggering the Photoresistor, did indeed call for a photo to be taken, but another instance is using my camera. I tried to kill the OctoPrint process and disabled any webcam requests from that tool without luck. The plan for today is to install legacy version of the Pi OS not using OctoPrint, and retry the script. Then I can work from there. – Michael Christensen Mar 10 '22 at 08:03