To achieve maximum performance delivering single JPG frames as a simple webservice I constantly fetch into a circular buffer with the picamera API. Unfortunately the documentation about it is not very helpful. I want to deliver the latest frame which was written to the buffer. In the API I can not find a way to know about the latest frame written. I can iterate over all frames in the buffer, but this is it. Also I don't know what happens to the buffer (PiCameraCircularIO) while reading it. Below is the code attached. There I simply read the last frame (whatever this is) and give it back to the HTTP client.
import threading import os import io import base64 import time import socket import picamera import daemon from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer PORT_NUMBER = 8080 #This class will handles any incoming request from #the browser class myHandler(BaseHTTPRequestHandler): #Handler for the GET requests def do_GET(self): self.send_response(200) self.send_header('Content-type','image/jpeg') self.end_headers() # Send the html message #myStream = io.BytesIO() start_time = time.time() #camera.capture(myStream, 'jpeg','true') #camera.capture(myStream, 'bgr','true') #self.wfile.write(myStream.getvalue()) lastframe = None counter = 0 while lastframe is None: for frame in stream.frames: counter += 1 if frame.complete: lastframe = frame; # if frame.complete: # break stream.seek(lastframe.position) self.wfile.write(stream.read(lastframe.frame_size)) # myStream.close() # print (time.time()-start_time, " :: ", counter, " :: ") return try: #Create a web server and define the handler to manage the #incoming request camera = picamera.PiCamera() camera.resolution = (1640, 1232) camera.exposure_mode = 'sports' camera.framerate = 42 #camera.shutter_speed=100 camera.start_preview() stream = picamera.PiCameraCircularIO(camera, seconds=2) camera.start_recording(stream, format='mjpeg') server = HTTPServer(('', PORT_NUMBER), myHandler) #print 'Started httpserver on port ' , PORT_NUMBER #Wait forever for incoming htto requests server.serve_forever() except KeyboardInterrupt: print '^C received, shutting down the web server' server.socket.close() camera.close()