0

I am having trouble capturing images from a Raspberry Pi camera using Python. The following code works fine the first time, but fails to read data from the camera on subsequent attempts:

# shotFoto.py
import logging
from time import sleep
from datetime import datetime
import subprocess
import os
from picamera import PiCamera

logging.basicConfig(filename='app.log', filemode='a', format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO)

def analyze(photo_name):
    result1 = subprocess.run(["python", "img/colorDetection.py", photo_name], capture_output=True, text=True, timeout=60)
    return(result1.stdout)

def motor(steps):
    result2 = subprocess.run(["python", "motor.py", str(steps)], capture_output=True, text=True, timeout = 60)
    return(result2.stdout)

def comm(color_code):
    result3 = subprocess.run(["python", "comm.py", color_code], capture_output=True, text=True, timeout = 60)
    return(result3.stdout)

def photo1(filepath):
    camera = PiCamera()
    camera.resolution = (400, 300)
    camera.start_preview()
    sleep(0.5)
    camera.capture(filepath)
    camera.stop_preview()
    camera.close()

def photo2(filename):
    photo_command = "raspistill -o img/" + filename + " -t 500 -w 400 -h 300"
    os.system(photo_command)

while True:
    # camera = PiCamera()
    try: 
        filename = datetime.now().strftime('%Y-%m-%d_%H-%M-%S.jpg')
        filepath = "img/" + filename
        
        # photo_command = "raspistill -o img/" + filename + " -t 500 -w 400 -h 300"
        # os.system(photo_command)

        # camera.resolution = (400, 300)
        # camera.start_preview()
        # sleep(0.5)
        # camera.capture(filepath)
        # camera.stop_preview()
        # camera.close()

        # photo(filepath)
        photo2(filename)

        message = analyze(filename)
        logging.info('Analyze completed, color: %s', message)
        print(message)

        if (message.strip().lower() == "yellow"):
            # comm('X')
            logging.info('Comm(X)')
        elif (message.strip().lower() == "blue"):
            # comm('A')
            logging.info('Comm(A)')
        elif (message.strip().lower() == "green"):
            # comm('F')
            logging.info('Comm(F)')
        else:
            pass

        # motor(1800)
        motor(10)

        if (message.strip().lower() == "yellow"):
            # comm('Y')
            pass
        elif (message.strip().lower() == "blue"):
            # comm('B')
            pass
        elif (message.strip().lower() == "green"):
            # comm('G')
            pass
        else:
            pass

        sleep(1)
        logging.info('Program finished')
        pass
    finally:
        # camera.close()
        pass

After executing the code above, the camera appears to be 'temporarily' blocked, and the only way to resolve the issue is to reboot the Raspberry Pi.Here is a screenshot of the error message I received: error

However, what is interesting is that the code works fine without any issues when run subsequently:

# camera.py
from time import sleep
from picamera import PiCamera
from datetime import datetime

while True:

    filename = datetime.now().strftime('%Y-%m-%d_%H-%M-%S.jpg')
    filepath = "" + filename

    # photo_command = "raspistill -o img/" + filename + " -st -t 500 -w 400 -h 300"
    # os.system(photo_command)

    camera = PiCamera()
    camera.resolution = (400, 300)
    camera.start_preview()
    sleep(0.5)
    camera.capture(filepath)
    camera.stop_preview()
    camera.close()

Plenty of images

I've tried to look for processes blocking the camera, but I didn't find anything. I triple checked the connection, but I think it's good, because first (and any futher) execution of camera.py works great, only shotFoto.py is faulty and breaks it down.

Any ideas on why this might be happening?

I have: Raspberry Pi model 1B Raspberry Pi Cam Rev 1.3

Short note of other files:

colorDetection.py - reads picture and returns name of average color in middle (10x10px) square of it

motor.py - moves the stepper engine of given number of steps

comm.py - sends command to arduino (which handles position of servo)

It would be great if anyone could help me. It's the last not working feature in my conveyor belt integrated with a sorting system

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.