Michael Christensen
Michael Christensen

Reputation: 11

Making a timelapse for my resin printer using a mix of already known-to-work functions - but custom tailored

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.

Upvotes: 1

Views: 154

Answers (1)

Zhaph - Ben Duguid
Zhaph - Ben Duguid

Reputation: 26966

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()

Upvotes: 0

Related Questions