import io import picamera import cv2 import numpy import pipan # center the camera p = pipan.PiPan() p.neutral_pan() p.neutral_tilt() # Pan -> (80, 220), middle position -> 150 # Tilt -> (50, 250) , middle position -> 150 #p.do_pan(100) #p.do_tilt(100) #Create a memory stream so photos doesn't need to be saved in a file stream = io.BytesIO() #Get the picture (low resolution, so it should be quite fast) #Here you can also specify other parameters (e.g.:rotate the image) with picamera.PiCamera() as camera: camera.resolution = (1280, 720) camera.hflip = True camera.vflip = True camera.capture(stream, format='jpeg') #Convert the picture into a numpy array buff = numpy.fromstring(stream.getvalue(), dtype=numpy.uint8) #Now creates an OpenCV image image = cv2.imdecode(buff, 1) #Load a cascade file for detecting faces face_cascade = cv2.CascadeClassifier('/home/pi/Desktop/SwarmCamera/face.xml') eye_cascade = cv2.CascadeClassifier('/home/pi/Desktop/SwarmCamera/haarcascade_eye.xml') #Convert to grayscale gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) #Look for faces in the image using the loaded cascade file faces = face_cascade.detectMultiScale(gray, 1.1, 5) print "Found "+str(len(faces))+" face(s)" #Draw a rectangle around every found face for (x,y,w,h) in faces: cv2.rectangle(image,(x,y),(x+w,y+h),(255,255,0),2) roi_gray = gray[y:y+h, x:x+w] roi_color = image[y:y+h, x:x+w] eyes = eye_cascade.detectMultiScale(roi_gray) for (ex,ey,ew,eh) in eyes: cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2) #Save the result image cv2.imwrite('result.jpg',image)