Potzi
Posts: 2
Joined: Mon Oct 07, 2019 9:26 am

photo booth / error not enough RAM

Thu Oct 17, 2019 6:37 pm

Hello,
I made a photo booth using a DSLR. the code is working fine so far, but when i take more than 10 pictures i get an error. the error says that I don't have enough space ( I assume that it is meant to be RAM, since I am from Austria and the Error is in German )
I'm very new to this stuff, but i think that when i download the images and show them on a monitor, the raspberry stores it in it's Ram (just a guess)
I searched a lot on the internet for a solution, but so far I didn't get lucky.
how can I fix my code so I don't get this error anymore? or is there a way to clean Ram when it reaches a certain amout? any ideas?
thanks in advance!

Here is my code:

Code: Select all

#!/usr/lib/python3.7

from time import sleep
from datetime import datetime
from sh import gphoto2 as gp
import signal, os, subprocess
import shutil
import glob
import random
#from pynput.keyboard import Key, Controller
from time import sleep

# gphoto Prozess killen, wenn die Kamera verbunden wird
# der kill Befehl um Bilder zu machen (Nummer ändert sich jedesmal)
def killgphoto2Prozess():
    p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
    out, err = p.communicate()

    # die Zeile suchen, die den Prozess hat (der gekillt werden soll)
    for line in out.splitlines():
        if b'gvfsd-gphoto2' in line:
            # den Prozess killen
            pid = int(line.split(None,1)[0])
            os.kill(pid, signal.SIGKILL)

speicherort = "/home/pi/Desktop/fotobox_test/zwischen"  # TODO externe Disk
programmOrt = "/home/pi/Desktop/fotobox_test"

def clearCameraSD():
    clearCommand = ["--folder", "/store_00020001/DCIM/100CANON", "-R", "--delete-all-files"]
    gp(clearCommand)

def createSaveFolder():
    #clearCameraSD()
    if not(os.path.exists(speicherort) and os.path.isdir(speicherort)):
        os.makedirs(speicherort)



def bildmachen():
    triggerCommand = ["--trigger-capture"]
    gp(triggerCommand)
    sleep(1)
    #os.chdir(TEMPDIR)
    downloadCommand = ["--get-all-files"]
    gp(downloadCommand)
    #clearCameraSD()
    for filename in os.listdir("."):
        #if filename.endswith(".JPG"):
        return speicherort+"/"+filename


#keyboard = Controller()

def foto():
    global speicherort
    killgphoto2Prozess()
    clearCameraSD()
    createSaveFolder()
    os.chdir(speicherort)
    photo = bildmachen()
    #print(photo)
    # TODO: zu speicherort verschieben und neuen pfad zurück geben
    #_bn = os.path.basename(photo)
    newPath = photo
    #os.rename(photo, newPath)
    #os.chdir(programmOrt)
    return newPath

def fotoMove():
    new_dir = "/home/pi/Desktop/fotobox_test/fotos"
    files = glob.iglob(os.path.join(speicherort, "*.JPG"))
    for file in files:
        if os.path.isfile(file):
            shutil.move(file, new_dir)
    #print("foto vecshoen")


if __name__ == "__main__":
    print(foto())



PS: this is the code where I take the picture and download it etc., I have another script, where i just check, if there is a button pushed so the camera takes a picture

User avatar
topguy
Posts: 5895
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: photo booth / error not enough RAM

Fri Oct 18, 2019 10:51 am

I assume that it is meant to be RAM,
You should verify this assumption with this command that shows you free space on your drives/partitions.

Code: Select all

df -h

Potzi
Posts: 2
Joined: Mon Oct 07, 2019 9:26 am

Re: photo booth / error not enough RAM

Thu Oct 24, 2019 8:25 am

yes it is RAM, df -h didn't give me the information i wanted.
but with the free and top command i could see, that with every taken picture, the Ram became less.
so is there a way to clean RAM within the python script, or is the only solution to restart my programm everytime?

User avatar
topguy
Posts: 5895
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: photo booth / error not enough RAM

Thu Oct 24, 2019 10:50 am

If you press "shift-m" while running "top" is should put the process with the highest memory usage on top. You should confirm that this is actually your python program that steals the memory and not 100ds of "gphoto" processes.
so is there a way to clean RAM within the python script,
Not really, you should find out where you use memory without freeing it again.
or is the only solution to restart my programm everytime?
You should confirm that this actually frees the RAM, let it run for a while and then look at "top" while you kill your script.

Return to “Graphics, sound and multimedia”