User avatar
CopterRichie
Posts: 131
Joined: Tue Mar 26, 2013 3:14 am
Location: Los Angeles CA.

Re: Pure Python camera interface

Tue Dec 10, 2013 4:30 pm

Here is something I just located that MAYBE of some help to others:

A simple webcam example: http://pygstdocs.berlios.de/pygst-tutor ... iewer.html

User avatar
jbeale
Posts: 3680
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Pure Python camera interface

Tue Dec 10, 2013 5:39 pm

Konstantins wrote:Is it possible to get a 25-30fps 1080p or 720p or 480p video stream with "for loop" iterating through each pixel in python?
Let's think about that for a moment. If you want to do RGB processing that is 3 bytes per pixel. If you want to do 1920x1080 pixels at 30 fps, how many bytes per second would that be?

1920 x 1080 x 30 = 62.2 million pixels/sec, x 3 = 186.6 million bytes per second. I *think* the ARMv6 in the R-Pi is mostly one instruction per clock, eg 700 MHz = 700 MIPS. So you would have between three and four machine instructions per byte at most, even if you program in assembly with nothing else going on in the Pi. Also, there is internal memory bus bandwidth to consider. However, the ARM is a 32 bit processor so you could work on packed data, eg one 32-bit R-G-B-A word at a time.

Without going into more detail, the simplest operation (eg. one add or one subtract per pixel) may not be impossible at 1080p30 if you program bare-metal, but it seems unlikely with a high-level, less-efficient language like Python. However I seem to recall the bus bandwidth between the GPU and CPU does not support 1080p30 (? not sure). Also, that doesn't leave many MIPS left over to do something with the result (eg. display it or save it). The R-Pi can display and stream 1080p but that is because H.264 compression on the GPU is used to greatly reduce the bandwidth needed, before it is sent to the CPU.

waaromookniet
Posts: 2
Joined: Tue Dec 10, 2013 11:15 pm

Re: Pure Python camera interface

Tue Dec 10, 2013 11:20 pm

Hi,

I was wondering if this wonderful interface also records sound from the webcam's mic?
I'm trying to build a standalone video message recorder with some fullscreen GUI around it, would that be even possible with Python? (I have no prior knowledge of Python as you might notice) Or would you suggest to go for an other language or just stop trying at all? :?

Thanks in advance!

Paul

User avatar
waveform80
Posts: 359
Joined: Mon Sep 23, 2013 1:28 pm
Location: Manchester, UK
Contact: Website Twitter

Re: Pure Python camera interface

Wed Dec 11, 2013 1:42 pm

waaromookniet wrote:Hi,

I was wondering if this wonderful interface also records sound from the webcam's mic?
I'm trying to build a standalone video message recorder with some fullscreen GUI around it, would that be even possible with Python? (I have no prior knowledge of Python as you might notice) Or would you suggest to go for an other language or just stop trying at all? :?

Thanks in advance!

Paul
Well, the interface is actually for the Raspberry Pi's camera module - not a USB webcam; the camera module doesn't have a mic, but the Pi does have a line in so I suppose technically one could combine audio from that with video output from the camera (I imagine there'd be some interesting synchronization issues to deal with though). I did notice someone on github trying to do something like this with raspivid and ffmpeg (though it looks like they've run into a problem). However, I'm not sure I understand half their ffmpeg options!

For what you're trying to do, Python's a fine language (it should be able to handle the interface requirements, whether you're intending to do something in X, or at the framebuffer), but I'm not sure what library you'll need for recording video+sound from a USB webcam (pygame and opencv both have bits for interfacing to webcams but I've no idea what their recording capabilities are). Another possibility is just to fire up gstreamer as a subprocess - that should be capable of recording video+sound I think.

Hope that helps,

Dave.
Author of / contributor to a few pi related things (picamera, Sense HAT emulator, gpio-zero, piwheels, etc.), and currently a software engineer at Canonical responsible for Ubuntu Server and Core on the Raspberry Pi.

User avatar
jbeale
Posts: 3680
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Pure Python camera interface

Wed Dec 11, 2013 2:55 pm

waveform80 wrote:... but the Pi does have a line in...
Just to clarify- the Pi has a PWM audio output. It has no analog or audio inputs, unless you add a USB soundcard or connect an external I2S device, etc.
Last edited by jbeale on Wed Dec 11, 2013 2:58 pm, edited 1 time in total.

User avatar
waveform80
Posts: 359
Joined: Mon Sep 23, 2013 1:28 pm
Location: Manchester, UK
Contact: Website Twitter

Re: Pure Python camera interface

Wed Dec 11, 2013 2:58 pm

jbeale wrote:
waveform80 wrote:
waaromookniet wrote: but the Pi does have a line in
Just to clarify- the Pi has a PWM audio output. It has no analog or audio inputs, unless you add a USB soundcard or connect an external I2S device, etc.
Sorry, you're quite right - that's a headphone jack, not a line-in. Probably a good thing I've never plugged anything into it!
Author of / contributor to a few pi related things (picamera, Sense HAT emulator, gpio-zero, piwheels, etc.), and currently a software engineer at Canonical responsible for Ubuntu Server and Core on the Raspberry Pi.

waaromookniet
Posts: 2
Joined: Tue Dec 10, 2013 11:15 pm

Re: Pure Python camera interface

Wed Dec 11, 2013 8:06 pm

waveform80 wrote:
waaromookniet wrote:Hi,

I was wondering if this wonderful interface also records sound from the webcam's mic?
I'm trying to build a standalone video message recorder with some fullscreen GUI around it, would that be even possible with Python? (I have no prior knowledge of Python as you might notice) Or would you suggest to go for an other language or just stop trying at all? :?

Thanks in advance!

Paul
Well, the interface is actually for the Raspberry Pi's camera module - not a USB webcam; the camera module doesn't have a mic, but the Pi does have a line in so I suppose technically one could combine audio from that with video output from the camera (I imagine there'd be some interesting synchronization issues to deal with though). I did notice someone on github trying to do something like this with raspivid and ffmpeg (though it looks like they've run into a problem). However, I'm not sure I understand half their ffmpeg options!

For what you're trying to do, Python's a fine language (it should be able to handle the interface requirements, whether you're intending to do something in X, or at the framebuffer), but I'm not sure what library you'll need for recording video+sound from a USB webcam (pygame and opencv both have bits for interfacing to webcams but I've no idea what their recording capabilities are). Another possibility is just to fire up gstreamer as a subprocess - that should be capable of recording video+sound I think.

Hope that helps,

Dave.
Thanks, it helps a lot!
I'll keep looking and trying other stuff

Alan Johnstone
Posts: 45
Joined: Tue Jan 08, 2013 4:35 pm

Re: Pure Python camera interface

Wed Dec 11, 2013 8:26 pm

Alan Johnstone wrote:Help, I have just upgraded to .8 and nothing works.

I get :
Traceback (most recent call last):
File "/testshow.py", line 39, in <module>
with picamera.PiCamera() as camera:
File "/root/.local/lib/python2.7/site-packages/picamera/camera.py", line 238, in __init__
self._init_camera()
File "/root/.local/lib/python2.7/site-packages/picamera/camera.py", line 269, in _init_camera
prefix="Failed to create camera component")
File "/root/.local/lib/python2.7/site-packages/picamera/exc.py", line 102, in mmal_check
}.get(status, "Unknown status error")))
PiCameraError: Failed to create camera component: Out of memory

As this is the begining of the program it seems unlikely that I have actually run out oof memory.

Any clues?

Alan
Well I resolved the matter. Shear stupidity. I removed the camera to connect the RPI to the internet to get the upgrade.
I then managed to put the cable back in back to front - hence no Camera!
Of course I blamed the upgrade and not myself. Sorry

Alan

User avatar
KLL
Posts: 1453
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: Pure Python camera interface

Thu Dec 12, 2013 8:52 am

i try to get that running also under the "darkbasic" image for RPI
( long way to install camera there too)
but that would only make sense if there is a big speed advantage.

so i do some benchmark on both systems and see that that miniwheezy
( because it seems not to react on my cpu tuning )
is not faster as a cpu tuned raspbian system.

also i am not sure that the selected tests are relevant for us.

details under http://kll.engineering-news.org/kllfusi ... icle_id=59

User avatar
Konstantins
Posts: 2
Joined: Tue Dec 10, 2013 2:44 am
Contact: Website

Re: Pure Python camera interface

Sun Dec 15, 2013 11:17 am

waveform80 wrote:Very much depends what you want to do with each pixel. You could probably iterate over each pixel at some vaguely reasonable resolution and framerate, but whether you could do anything meaningful with every pixel is another matter. What exactly are you looking to do with every pixel? Generally speaking, for loops simply aren't a great way to process image data (in Python or any language) - it's generally much better to treat such things as matrix operations.
Didn't notice I haven't subscribed to the topic, hence delay :) Thanks for reply!
I made a simple laser/camera "3D scanner" that I'll need as a component of another project, however at moment it works with footage via manual prerecording which is not good for the purpose.
I'm wondering if it'd be possible to run the scanning in real time.
A webcam probably is the solution, but I thought Raspberry camera might be capable of transferring image with less compression and post-processed, compared to a USB webcam. It'd be great to have RPi processing the stream and outputting the filtered scanline points (1per pixel column) via ethernet, hence reducing the main computer requirements.
jbeale wrote:Let's think about that for a moment. If you want to do RGB processing that is 3 bytes per pixel. If you want to do 1920x1080 pixels at 30 fps, how many bytes per second would that be?

1920 x 1080 x 30 = 62.2 million pixels/sec, x 3 = 186.6 million bytes per second. I *think* the ARMv6 in the R-Pi is mostly one instruction per clock, eg 700 MHz = 700 MIPS. So you would have between three and four machine instructions per byte at most, even if you program in assembly with nothing else going on in the Pi. Also, there is internal memory bus bandwidth to consider. However, the ARM is a 32 bit processor so you could work on packed data, eg one 32-bit R-G-B-A word at a time.

Without going into more detail, the simplest operation (eg. one add or one subtract per pixel) may not be impossible at 1080p30 if you program bare-metal, but it seems unlikely with a high-level, less-efficient language like Python. However I seem to recall the bus bandwidth between the GPU and CPU does not support 1080p30 (? not sure). Also, that doesn't leave many MIPS left over to do something with the result (eg. display it or save it). The R-Pi can display and stream 1080p but that is because H.264 compression on the GPU is used to greatly reduce the bandwidth needed, before it is sent to the CPU.
Hi,
thanks for insight. After a bit of testing, I think I have exaggerated the requirements - 720p is totally enough. The loop operation is not so simple though - it iterates to find the brightest pixel in each column or row, under a few conditions to filter out most of other (than laser) light sources. When it finds one, it iterates through closest few to get something like sub-pixel precision.
Sorry for ignorant questions - could the camera feed video with slower framerate (without increasing exposure)?
I guess still images won't be any good as I've seen people here complaining about significant delay. Is video more or less real time?

User avatar
jbeale
Posts: 3680
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Pure Python camera interface

Sun Dec 15, 2013 4:06 pm

The provided example "raspivid" code can do video from 30 fps down to 2 fps. That program saves video in compressed h264 format. Note to the camera and the GPU, "video" and "stills" are not so different, video is just one still after another. You can use the python interface to get stills using camera.capture_sequence() with "use_video_port=True" and get stills at near-video speeds, as I found out with this test: http://www.raspberrypi.org/phpBB3/viewt ... 25#p447540

for example 1296x972: Captured 60 images at 21.77 fps

but that is saving GPU-encoded JPEGs to ramdisk. You want to do image processing on the ARM side, so you'll get whatever performance your code allows. You don't need to slow down the camera acquisition side, you just grab a frame when you're ready for it. With the acquisition at 30 fps, some frames will go unused because you were busy with the processing.

User avatar
jbeale
Posts: 3680
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Pure Python camera interface

Sun Dec 15, 2013 4:21 pm

Question for waveform80 if you have time:

I posted some code here: http://www.raspberrypi.org/phpBB3/viewt ... 75#p461876 which is a simple motion detector, using your great Python library. You'll notice I am using the roundabout method of getting JPEGs from the camera and then decoding them in software to RGB. Really I'd like to get the plain RGB (or YUV) to avoid the un-needed processing overhead, but I was unable to make anything except JPEG work. Do you have any pointers on how to rewrite the code I have to avoid the JPEG-uncompression step? It takes time and loses quality due to JPEG compression.

User avatar
jbeale
Posts: 3680
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

example super-long exposure by averaging

Mon Dec 16, 2013 7:39 am

Simply python code to reduce noise by averaging together many frames, making one long exposure. It does two dumb things:

1) it gets every image as JPEG, then decompresses. It should work on uncompressed data, but I couldn't get that working
2) it accumulates the sum as a 2D floating-point array (actually 3D: has R,G,B separately). That takes too much memory, better to use short ints.

Averaging reduces the noise, but not as well as I expected. Comparing 1 frame with the average of 64 frames (images below), I think quite a bit of remaining noise with the 64-frame average is "fixed-pattern noise" which could be reduced with a dark frame subtraction and white frame balance step, which I haven't yet been bothered to do.

EDIT: yes, stepping through a sequence of these averaged images frame by frame, 100% of the remaining visible noise is fixed, unchanging from one set of averages to the next. So that is good in that it can, in principle, be removed.

Code: Select all

#!/usr/bin/python
# R-Pi camera simple frame averaging
# 15 Dec 2013 J.Beale

from __future__ import print_function
import io, os, time, datetime, picamera, cv2
import numpy as np

width = 2592/2  # use half of full 5Mpix resolution
height = 1944/2
fcount_limit = 64  # how many frames to average

np.set_printoptions(precision=2)
stream = io.BytesIO()
with picamera.PiCamera() as camera:
   camera.resolution = (width, height)
   camera.shutter_speed = 1000000          # set shutter speed in microseconds
   camera.exposure_mode = 'sports'         # enables ISO up to 1600
   camera.brightness = 55
   camera.start_preview()
   time.sleep(10)  # 10 seconds delay needed for exposure to reach ISO 1600 
   fcount = 0
   while True:
      camera.capture(stream, format='jpeg', use_video_port=True)
      stream.seek(0)
      data = np.fromstring(stream.getvalue(), dtype=np.uint8)
      image = cv2.imdecode(data, 1)
      image = image.astype(np.float32) # convert to single-precision floating point
      if (fcount > 0):
        sum = sum + image  # 2D floating-point matrix with accumulated frames
      else:
        sum = image 
      fcount = fcount + 1 
      daytime = datetime.datetime.now().strftime("%y%m%d-%H_%M_%S.%f")
      daytime = daytime[:-3]  # remove last three digits (xxx microseconds)
      if (fcount >= fcount_limit):
         imgName = daytime + ".jpg"
         average = sum / fcount  # average is total divided by number of elements
         image2 = average.astype(np.uint8)  # convert float back to single bytes
         cv2.imwrite(imgName, image2)  # save as an image
         fcount = 0
single frame, and 64-frame average. Taken at night through a window, and there is a faint reflection of the top edge of the room interior walls due to reflected light from my monitor. These are reduced-resolution versions; full size available via links below.
https://picasaweb.google.com/1099282360 ... 6624524994
https://picasaweb.google.com/1099282360 ... 2660402210
Image
Image
Last edited by jbeale on Mon Dec 16, 2013 10:56 pm, edited 2 times in total.

User avatar
jbeale
Posts: 3680
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Pure Python camera interface

Mon Dec 16, 2013 10:53 pm

Documentation correction: in the docs at http://picamera.readthedocs.org/en/rele ... or_effects the heading reads "color_effects" but the text mentions "color_effect" with no "s" at the end. There should be an "s" at the end.

Also, for reference here is the same view out the window as in my previous post but during daytime (and full frame 5 MPix). I am using the Pi-NoIR camera, but the colors are more or less normal (blue sky, somewhat green leaves, somewhat yellow oranges, etc.)

https://picasaweb.google.com/1099282360 ... 0394343698
Image

Alan Johnstone
Posts: 45
Joined: Tue Jan 08, 2013 4:35 pm

Re: Pure Python camera interface

Tue Dec 17, 2013 8:36 am

What exactly is "fixed pattern noise"? I thought the definition of noise was that it was random.

Alan

gordon77
Posts: 5040
Joined: Sun Aug 05, 2012 3:12 pm

Re: Pure Python camera interface

Tue Dec 17, 2013 9:20 am

Alan Johnstone wrote:What exactly is "fixed pattern noise"? I thought the definition of noise was that it was random.

Alan
I guess if its fixed then it could be classed as interference rather than noise, and comes from a source, but it will reduce the signal to noise, wanted to unwanted.

User avatar
jbeale
Posts: 3680
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Pure Python camera interface

Tue Dec 17, 2013 3:08 pm

Fixed Pattern Noise: http://en.wikipedia.org/wiki/Fixed-pattern_noise

And by the way, here is a crop from a full frame 1 sec exposure last night, I think it is a meteor trail.
EDIT: oops- looking at addition frames, it appears to be an airplane.
Dec16-Crop1.jpg
Pi NoIR cam meteor trail (?)
Dec16-Crop1.jpg (31.88 KiB) Viewed 10576 times
Last edited by jbeale on Tue Dec 17, 2013 8:04 pm, edited 2 times in total.

User avatar
waveform80
Posts: 359
Joined: Mon Sep 23, 2013 1:28 pm
Location: Manchester, UK
Contact: Website Twitter

Re: Pure Python camera interface

Tue Dec 17, 2013 3:10 pm

Sorry for the long delay in replying!
jbeale wrote:Question for waveform80 if you have time:

I posted some code here: http://www.raspberrypi.org/phpBB3/viewt ... 75#p461876 which is a simple motion detector, using your great Python library. You'll notice I am using the roundabout method of getting JPEGs from the camera and then decoding them in software to RGB. Really I'd like to get the plain RGB (or YUV) to avoid the un-needed processing overhead, but I was unable to make anything except JPEG work. Do you have any pointers on how to rewrite the code I have to avoid the JPEG-uncompression step? It takes time and loses quality due to JPEG compression.
I've had a crack at re-writing it to use raw capture. I would've liked to go with RGB, but as mentioned in the latest release notes, video-port-based raw RGB capture sadly doesn't work in 0.8 (if I can reinstate it in future, I will though). So, I've gone with YUV capture, and just to keep things simple I haven't bothered decoding to RGB either - I figured the Y-channel alone would probably be good enough for motion detection (and it certainly seems to work for my limited test!). Here's the result - I've tried to change as little as possible, but I did have to tidy up the indentation to figure out the logic:

Code: Select all

#!/usr/bin/python

# simple motion-detection using pypi.python.org/pypi/picamera
# runs at up to 10 fps depending on resolution, etc.
# Nov. 30 2013 J.Beale

# Some Python 3 compatibility magic...
from __future__ import (
        unicode_literals,
        absolute_import,
        print_function,
        division,
        )
str = type('')

import io, os, time, datetime, picamera, cv2
import numpy as np

#width = 32
#height = 16 # minimum size that works ?
width = 144
height = 96
# Round width and height up to closest multiple of 32 and 16 respectively as
# these are what the camera rounds to internally, and we need these sizes for
# raw-mode captures
fwidth = (width + 31) // 32 * 32
fheight = (height + 15) // 16 * 16
frames = 0
first_frame = True
frac = 0.05       # fraction to update long-term average on each pass
a_thresh = 16.0   # amplitude change detection threshold for any pixel
pc_thresh = 20    # number of pixels required to exceed threshold
avgmax = 3        # long-term average of maximum-pixel-change-value
tfactor = 2       # threshold above max.average diff per frame for motion detect
picHoldoff = 1.0  # minimum interval (seconds) between saving images
fupdate = 100     # report debug data every this many frames
logfile = "cam-log.csv"

np.set_printoptions(precision=2)
with io.open(logfile, 'a') as f:
    f.write("# cam log v0.1 Nov.28 2013 JPB\n")
    f.write("# Start: " +  str(datetime.datetime.now()) + "\n")
    f.flush()

    daytime = datetime.datetime.now().strftime("%y%m%d-%H_%M_%S.%f")
    daytime = daytime[:-3]  # remove last three digits (xxx microseconds)
    print ("# Start at %s" % str(datetime.datetime.now()))

    stream = io.BytesIO()
    with picamera.PiCamera() as camera:
        camera.resolution = (width, height)
        camera.start_preview()
        time.sleep(2)
        start = time.time()
        # I'd like to use raw_format='RGB' here, but unfortunately that's not
        # currently possible with video-port based captures (the splitter being
        # used to permit simultaneous video and image capture from the
        # video-port doesn't work when the camera's video port is in RGB mode).
        #
        # So, we'll just have to stick to YUV. However, I'm betting that the Y
        # bits alone are probably good enough for motion detection, so instead
        # of bothering with RGB decoding, we'll just grab the first n bytes of
        # the stream (the Y-plane) and discard the rest.
        while True:
            camera.capture(stream, format='raw', use_video_port=True)
            stream.seek(0)
            image = np.fromstring(stream.getvalue(), count=fwidth * fheight, dtype=np.uint8)
            # Now reshape the array into a 2D image, and lop off the unused
            # portions (which resulted from the camera's rounding of the res).
            # There's no longer any need to "decode" the image with OpenCV as
            # an OpenCV image is just a numpy array anyway
            image = image.reshape((fheight, fwidth))[:height, :width].astype(np.float32)
            frames += 1
            if (frames % fupdate) == 0:
                print("%s,  %03d max = %5.3f, avg = %5.3f" % (str(datetime.datetime.now()), frames, max, avgmax))
            if first_frame:
                first_frame = False
                # No need to extract green channel anymore as we've only got a
                # grayscale image anyway. Just use the whole image
                avgcol = image
                avgdiff = avgcol / 20.0 # obviously a coarse estimate
            else:
                avgcol = (avgcol * (1.0 - frac)) + (image * frac)
                # Calculate the matrix of difference-from-average pixel values
                # (diff), and the long-term average difference (avgdiff)
                diff = image - avgcol
                diff = abs(diff)
                avgdiff = ((1 - frac) * avgdiff) + (frac * diff)
                # Calculate the adaptive amplitude-of-change threshold (athresh)
                a_thresh = tfactor * avgmax
                condition = diff > a_thresh
                changedPixels = np.extract(condition, diff)
                countPixels = changedPixels.size
                # Find the biggest single-pixel change
                max = np.amax(diff)
                avgmax = ((1 - frac) * avgmax) + (max * frac)
                # A notable change of enough pixels implies motion!
                if countPixels > pc_thresh:
                    now = time.time()
                    interval = now - start
                    start = now
                    daytime = datetime.datetime.now().strftime("%y%m%d-%H_%M_%S.%f")
                    daytime = daytime[:-3]  # remove last three digits (xxx microseconds)
                    daytime = daytime + "_" + str(countPixels)
                    tstr = ("%s,  %04.1f, %6.3f, %03d\n" % (daytime, max, interval, countPixels))
                    print (tstr, end='')
                    f.write(tstr)
                    f.flush()
                    # Don't write images more quickly than picHoldoff interval
                    if interval > picHoldoff:
                        imgName = daytime + ".jpg"
                        cv2.imwrite(imgName, image)  # save as an image
Hope this helps!


Cheers,

Dave.
Author of / contributor to a few pi related things (picamera, Sense HAT emulator, gpio-zero, piwheels, etc.), and currently a software engineer at Canonical responsible for Ubuntu Server and Core on the Raspberry Pi.

Alan Johnstone
Posts: 45
Joined: Tue Jan 08, 2013 4:35 pm

Re: Pure Python camera interface

Tue Dec 17, 2013 3:44 pm

If I understand what you are doing correctly you are producing an average of a number of pictures then looking at the difference of that average compared to a new image. You then look for differences. If there was no movement you expect a black screen but what you get shows the sky as lighter. You think this is due to Fixed Pattern Noise. If it was because of fixed pattern noise ie some pixels are regularly lighter than their surrounding pixels then I think you would loose that on subtrtaction.

Alan

User avatar
jbeale
Posts: 3680
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Pure Python camera interface

Tue Dec 17, 2013 4:57 pm

Alan Johnstone wrote:If I understand what you are doing correctly you are producing an average of a number of pictures then looking at the difference of that average compared to a new image. You then look for differences. If there was no movement you expect a black screen but what you get shows the sky as lighter. You think this is due to Fixed Pattern Noise. If it was because of fixed pattern noise ie some pixels are regularly lighter than their surrounding pixels then I think you would loose that on subtrtaction.
Sorry, this thread is getting confused because I have posted several different and unrelated projects in it. The motion-detection project involving averaging and difference-from-average, is separate from the long-exposure project. In the latter, I am simply summing up many 1-second long exposures and looking at the average. There is no difference involved. Had I done a subtraction, I would remove any truly fixed pattern just as you say, and I should probably try that next and see if it works as expected.

@waveform80: Thanks very much for your help! Looks like your code is working and it is quite a bit faster than my version. With your version I see up to 30 fps at 144x96 where I had only 10 fps before, and at 288x192 I now see up to 15 fps. Nice work!

If we just had a way to simultaneously capture a full-res frame at the moment motion is detected, that would be perfect- I know the video splitter/resizer isn't working for now, but I'm holding out hope that could still eventually work.

User avatar
butyi
Posts: 2
Joined: Thu Jan 02, 2014 8:34 pm
Location: Hungary, Budapest
Contact: Website

Re: Pure Python camera interface

Thu Jan 02, 2014 9:04 pm

Hi!
1 month ago this example shot python code worked.

Code: Select all

import picamera
import time
with picamera.PiCamera() as camera:
    try:
        camera.start_preview()
        camera.resolution = (640, 480)
        time.sleep(2)
        camera.capture('image.jpg')
        camera.stop_preview()
    finally:
        camera.close()
I did an apt-get update/upgrade, and now it does not work. :-(
I get the following response:
Traceback (most recent call last):
File "shot.py", line 9, in <module>
camera.capture('image.jpg')
File "/usr/local/lib/python2.7/dist-packages/picamera/camera.py", line 821, in capture
encoder.start(output)
File "/usr/local/lib/python2.7/dist-packages/picamera/encoders.py", line 554, in start
super(PiCookedOneImageEncoder, self).start(output)
File "/usr/local/lib/python2.7/dist-packages/picamera/encoders.py", line 238, in start
self._open_output(output)
File "/usr/local/lib/python2.7/dist-packages/picamera/encoders.py", line 213, in _open_output
self.output = io.open(output, 'wb', buffering=1048576)
AttributeError: 'module' object has no attribute 'open'
I restarted everything from the scratch (original raspian image, install picamera) but error message is same.
The start-stop preview example works. I am not a python expert to debug it.
I afraid I am doing something mistake, and don't the system has changed.
Can somebody help?

User avatar
waveform80
Posts: 359
Joined: Mon Sep 23, 2013 1:28 pm
Location: Manchester, UK
Contact: Website Twitter

Re: Pure Python camera interface

Fri Jan 03, 2014 11:55 am

butyi wrote:Hi!
1 month ago this example shot python code worked.

Code: Select all

import picamera
import time
with picamera.PiCamera() as camera:
    try:
        camera.start_preview()
        camera.resolution = (640, 480)
        time.sleep(2)
        camera.capture('image.jpg')
        camera.stop_preview()
    finally:
        camera.close()
I did an apt-get update/upgrade, and now it does not work. :-(
I get the following response:
Traceback (most recent call last):
File "shot.py", line 9, in <module>
camera.capture('image.jpg')
File "/usr/local/lib/python2.7/dist-packages/picamera/camera.py", line 821, in capture
encoder.start(output)
File "/usr/local/lib/python2.7/dist-packages/picamera/encoders.py", line 554, in start
super(PiCookedOneImageEncoder, self).start(output)
File "/usr/local/lib/python2.7/dist-packages/picamera/encoders.py", line 238, in start
self._open_output(output)
File "/usr/local/lib/python2.7/dist-packages/picamera/encoders.py", line 213, in _open_output
self.output = io.open(output, 'wb', buffering=1048576)
AttributeError: 'module' object has no attribute 'open'
I restarted everything from the scratch (original raspian image, install picamera) but error message is same.
The start-stop preview example works. I am not a python expert to debug it.
I afraid I am doing something mistake, and don't the system has changed.
Can somebody help?
Wow - that's quite strange. The io module is one of the core Python modules, so it looks like something's gone horribly wrong with your Python installation (how, I'm not sure). Could you try the following, just to confirm: fire up "python" on the command line, import the io module and see what the "open" function is? Here's what I get:

Code: Select all

dave@kermit ~ $ python
Python 2.7.3 (default, Jan 13 2013, 11:20:46) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import io
>>> io.open
<built-in function open>
>>> 
If you get something other than <built-in function open> then your python installation is indeed broken - I might suspect a corrupted/broken SD card, given you've reflashed the raspbian image on it (though it's an awfully specific corruption...).

One other thing to note (nothing to do with the error, though) is that you don't need the try..finally..camera.close - if you're using the camera within a "with" block that gets done automatically at the end of the "with" block.


Cheers,

Dave.
Author of / contributor to a few pi related things (picamera, Sense HAT emulator, gpio-zero, piwheels, etc.), and currently a software engineer at Canonical responsible for Ubuntu Server and Core on the Raspberry Pi.

User avatar
jbeale
Posts: 3680
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Pure Python camera interface

Fri Jan 03, 2014 4:12 pm

I just tried this code, and it did not work, although I get a different message. First I tried update/upgrade, and then rpi-update. This was after a reboot and I confirmed that I did not have any other process using the camera. I confirmed that the 'raspistill' program still works normally, so I know my camera module is functioning properly.

Code: Select all

after sudo apt-get update && sudo apt-get upgrade:
----------------------------
pi@rp5 ~/pics/test3 $ python camtest.py 
Traceback (most recent call last):
  File "camtest.py", line 6, in <module>
    camera.resolution = (640, 480)
  File "/usr/local/lib/python2.7/dist-packages/picamera-0.6-py2.7.egg/picamera/camera.py", line 1060, in _set_resolution
    self._check_preview_stopped()
  File "/usr/local/lib/python2.7/dist-packages/picamera-0.6-py2.7.egg/picamera/camera.py", line 325, in _check_preview_stopped
    raise PiCameraRuntimeError("Preview is currently running")
picamera.exc.PiCameraRuntimeError: Preview is currently running


pi@rp5 ~/pics/test3 $ uname -a
Linux rp5 3.10.24+ #614 PREEMPT Thu Dec 19 20:38:42 GMT 2013 armv6l GNU/Linux

after sudo rpi-update:
-------------------------
pi@rp5 ~/pics/test3 $ python camtest.py 
Traceback (most recent call last):
  File "camtest.py", line 6, in <module>
    camera.resolution = (640, 480)
  File "/usr/local/lib/python2.7/dist-packages/picamera-0.6-py2.7.egg/picamera/camera.py", line 1060, in _set_resolution
    self._check_preview_stopped()
  File "/usr/local/lib/python2.7/dist-packages/picamera-0.6-py2.7.egg/picamera/camera.py", line 325, in _check_preview_stopped
    raise PiCameraRuntimeError("Preview is currently running")
picamera.exc.PiCameraRuntimeError: Preview is currently running

pi@rp5 ~/pics/test3 $ uname -a
Linux rp5 3.10.25+ #618 PREEMPT Thu Jan 2 20:00:10 GMT 2014 armv6l GNU/Linux
Note, the library in general is still OK because I found that the simple "motion detector" example here does work (after doing sudo apt-get install python-opencv )

User avatar
waveform80
Posts: 359
Joined: Mon Sep 23, 2013 1:28 pm
Location: Manchester, UK
Contact: Website Twitter

Re: Pure Python camera interface

Fri Jan 03, 2014 4:31 pm

jbeale wrote:I just tried this code, and it did not work, although I get a different message. First I tried update/upgrade, and then rpi-update. This was after a reboot and I confirmed that I did not have any other process using the camera. I confirmed that the 'raspistill' program still works normally, so I know my camera module is functioning properly.

Code: Select all

after sudo apt-get update && sudo apt-get upgrade:
----------------------------
pi@rp5 ~/pics/test3 $ python camtest.py 
Traceback (most recent call last):
  File "camtest.py", line 6, in <module>
    camera.resolution = (640, 480)
  File "/usr/local/lib/python2.7/dist-packages/picamera-0.6-py2.7.egg/picamera/camera.py", line 1060, in _set_resolution
    self._check_preview_stopped()
  File "/usr/local/lib/python2.7/dist-packages/picamera-0.6-py2.7.egg/picamera/camera.py", line 325, in _check_preview_stopped
    raise PiCameraRuntimeError("Preview is currently running")
picamera.exc.PiCameraRuntimeError: Preview is currently running


pi@rp5 ~/pics/test3 $ uname -a
Linux rp5 3.10.24+ #614 PREEMPT Thu Dec 19 20:38:42 GMT 2013 armv6l GNU/Linux

after sudo rpi-update:
-------------------------
pi@rp5 ~/pics/test3 $ python camtest.py 
Traceback (most recent call last):
  File "camtest.py", line 6, in <module>
    camera.resolution = (640, 480)
  File "/usr/local/lib/python2.7/dist-packages/picamera-0.6-py2.7.egg/picamera/camera.py", line 1060, in _set_resolution
    self._check_preview_stopped()
  File "/usr/local/lib/python2.7/dist-packages/picamera-0.6-py2.7.egg/picamera/camera.py", line 325, in _check_preview_stopped
    raise PiCameraRuntimeError("Preview is currently running")
picamera.exc.PiCameraRuntimeError: Preview is currently running

pi@rp5 ~/pics/test3 $ uname -a
Linux rp5 3.10.25+ #618 PREEMPT Thu Jan 2 20:00:10 GMT 2014 armv6l GNU/Linux
Ah, you've got a rather old version of picamera there - the newer ones can change resolution while the preview is running (I think it was 0.7 that introduced that change). If you run "pip install -U picamera" it should upgrade it 0.8 (I'm afraid apt-get upgrade only deals with .deb packages - not stuff installed via pip/easy_install - this should be corrected once picamera 1.0 rolls out as I'll be building .deb packages for that as well as the usual pip/easy_install stuff).

Cheers,

Dave.
Author of / contributor to a few pi related things (picamera, Sense HAT emulator, gpio-zero, piwheels, etc.), and currently a software engineer at Canonical responsible for Ubuntu Server and Core on the Raspberry Pi.

User avatar
jbeale
Posts: 3680
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Pure Python camera interface

Fri Jan 03, 2014 4:50 pm

Sure enough. After updating, the test code does work without an error, and saves 'image.jpg' as expected.

Code: Select all

sudo apt-get install python-pip
sudo pip install -U picamera
python camtest.py 
ll image.jpg 
-rw-r--r-- 1 pi pi 120432 Jan  3 08:47 image.jpg

Return to “Camera board”