waveform80 / picamera

A pure Python interface to the Raspberry Pi camera module
https://picamera.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.57k stars 355 forks source link

PiMotionAnalysis on Compute Module with stereo_mode #601

Open cbasavaraj opened 4 years ago

cbasavaraj commented 4 years ago

Hi, I am working on a Compute Module with two cameras attached. In general, it's working fine. Today, I am trying some of the Advanced Recipes. In particular, I need motion detection and downstream processing when there's motion.

THRESH = 60

class MotionDetector(picamera.array.PiMotionAnalysis):
    def analyse(self, data):
        rho = np.sqrt(
                np.square(data['x'].astype(np.float)) +
                np.square(data['y'].astype(np.float))
                ).clip(0, 255).astype(np.uint8)
        n = (rho > THRESH).sum()
        if n > 10:
            print('Motion: {:}!'.format(n))

with picamera.PiCamera() as camera:
    camera.resolution = (1536, 864)
    camera.framerate = 30
    print('Starting recording...')
    camera.start_recording(
        '/dev/null', format='h264',
        motion_output=MotionDetector(camera)
        )
    camera.wait_recording(30)
    camera.stop_recording()
    print('Done!')

The code above from 4.11. Custom Outputs works fine, but when I try it with with picamera.PiCamera(stereo_mode='side-by-side') as camera:

it goes haywire:

Starting recording...
Motion: 1003!
Motion: 734!
Motion: 2260!
Motion: 612!
Motion: 802!
Motion: 612!
Motion: 802!
Motion: 612!
Motion: 802!
Motion: 612!
Motion: 802!
Motion: 612!
Motion: 802!
Motion: 612!
Motion: 802!
Motion: 612!
Motion: 802!

I think the motion data is the difference of data from the two cameras, which makes the MotionDetector.analyse() think there's motion all the time. Can anyone please help? Thanks