0

I'm trying to use a separate process to take pictures. This code was modified from being a Thread to being a multiprocess (by me, this is why it doesn't work). When i create an instance of this class and then run it with obj.start() a popup appears the program is already running, do you want to stop it? I don't understand what am I doing wrong? P.S ("GO" output is never shown on the screen)

# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import multiprocessing


class MultiProcess(multiprocessing.Process):
def __init__(self):

    print("INIT")
    multiprocessing.Process.__init__(self)

    # initialize the camera and stream
    self.camera             = PiCamera()
    self.camera.resolution  = (640, 480) 
    self.camera.framerate   = 32
    self.rawCapture         = PiRGBArray(self.camera, size=(320, 240))
    self.stream             = self.camera.capture_continuous(self.rawCapture, format="bgr", use_video_port=True)

    # initialize the frame and the variable used to indicate
    # if the thread should be stopped
    self.frame      = None
    self.stopped    = False

def start(self):
    # start the thread to read frames from the video stream
    p = multiprocessing.Process(target=self.update, args=())
    print("1")
    p.daemon = True
    print("2")
    p.start()
    print("3")
    p.join()
    print("GO")
    return self

def update(self):
    # keep looping infinitely until the thread is stopped
    for f in self.stream:
        print("NEVER REACH HERE")
        # grab the frame from the stream and clear the stream in
        # preparation for the next frame
        self.frame = f.array
        self.rawCapture.truncate(0)

        # if the thread indicator variable is set, stop the thread
        # and resource camera resources
        if self.stopped:
            self.stream.close()
            self.rawCapture.close()
            self.camera.close()
            return

def read(self):
    # return the frame most recently read
    return self.frame

def stop(self):
    # indicate that the thread should be stopped
    self.stopped = True
Cătălina Sîrbu
  • 1,253
  • 9
  • 30
  • What's your motivation for using a separate process rather than just a separate thread? Separate processes are good for CPU intensive tasks, which probably isn't the case here. – 101 May 03 '20 at 23:42
  • I am processing the image in the main thread and I was thinking that I could improve the speed of that processing by taking the pictures in a separate process rather than separat thread – Cătălina Sîrbu May 03 '20 at 23:44
  • the problem is that I cant start a process. Even this fails (after running the scrip, I can see that the sleeping processes number increases, while the running number remains the same): `def preview(): with PiCamera() as cam: cam.start_preview() p = Process(target=preview) p.start()` – Cătălina Sîrbu May 03 '20 at 23:49

0 Answers0