def get_faces(image_path, api_mode=False, rescale_face_crop=0): """ Return a list of cropped faces given an image path :param image_path: Path to image :type image_path: str :param api_mode: If api_mode is True get_faces returns a list of found HaarFeatures :type api_mode: bool :returns: list of images """ original_image = Image(image_path) faces = original_image.findHaarFeatures(segment_face) if api_mode: return faces else: if rescale_face_crop: return [ original_image.crop( scale_bounding_box(face.boundingBox(), rescale_face_crop)) for face in faces ] else: return [face.crop() for face in faces]
def findFace(imgPath): img = Image(imgPath) faces = img.findHaarFeatures("face.xml") if faces is not None: #print("find faces!") return True else: #print("Don't find andy faces!") return False
def face_recognize(filename): from SimpleCV import Image, Display, DrawingLayer image = Image(filename) faces = image.findHaarFeatures('face.xml') if faces: for face in faces: face_layer = DrawingLayer((image.width, image.height)) face_box = face_layer.centeredRectangle(face.coordinates(), (face.width(), face.height())) image.addDrawingLayer(face_layer) image.applyLayers() image.save(filename) print('偵測到 {} 張人臉'.format(len(faces))) else: print('沒有偵測到人臉')
def face_recognize(filename): from SimpleCV import Image, Display, DrawingLayer image = Image(filename) faces = image.findHaarFeatures('face.xml') if faces: for face in faces: face_layer = DrawingLayer((image.width, image.height)) face_box = face_layer.centeredRectangle( face.coordinates(), (face.width(), face.height())) image.addDrawingLayer(face_layer) image.applyLayers() image.save(filename) print('偵測到 {} 張人臉'.format(len(faces))) else: print('沒有偵測到人臉')
def get_faces(image_path, api_mode=False, rescale_face_crop=0): """ Return a list of cropped faces given an image path :param image_path: Path to image :type image_path: str :param api_mode: If api_mode is True get_faces returns a list of found HaarFeatures :type api_mode: bool :returns: list of images """ original_image = Image(image_path) faces = original_image.findHaarFeatures(segment_face) if api_mode: return faces else: if rescale_face_crop: return [original_image.crop(scale_bounding_box(face.boundingBox(), rescale_face_crop)) for face in faces] else: return [face.crop() for face in faces]
# wiringpi.digitalWrite(65, 0) # imposta GPA0 a 0 (0V, off) # wiringpi.pinMode(72, 1) # imposta GPA7 come output # wiringpi.digitalWrite(72, 0) # imposta GPA7 a 0 (0V, off) with picamera.PiCamera() as camera: print "\n请输入脸型密码\n" os.system('omxplayer --no-osd -no-keys password.wav') while success_flag: camera.start_preview() time.sleep(5) camera.capture('pifacepw.jpg') image=Image("pifacepw.jpg") camera.stop_preview() faces = image.findHaarFeatures("face.xml") # recognize your face through the file HaarCFeatures "face" if faces: if not password: faces.draw() face = faces[-1] password = face.crop().save("password.jpg") print "已更新脸型密码库" break else: faces.draw() face = faces[-1] template = face.crop() template.save("passwordmatch.jpg") keypoints = password.findKeypointMatch(template,quality,minDist,minMatch) if keypoints: print "\n识别正确,欢迎回来\n"
GPIO.setup(RedLED, GPIO.OUT) GPIO.setup(GreenLED, GPIO.OUT) GPIO.setup(YellowLED, GPIO.OUT) GPIO.output(GreenLED, 0) GPIO.output(RedLED, 0) GPIO.output(YellowLED, 0) with picamera.PiCamera() as camera: while True: camera.start_preview() time.sleep(5) camera.capture('pifacepw.jpg') image = Image("pifacepw.jpg") camera.stop_preview() faces = image.findHaarFeatures("face.xml") if faces: if not password: faces.draw() face = faces[-1] password = face.crop().save("password.jpg") print "First Run Application" print "Exit Program" break else: faces.draw() face = faces[-1] template = face.crop() template.save("passwordmatch.jpg") template = Image("passwordmatch.jpg") diff = template.resize(password.width,
def fancify(): if request.method == 'POST': print request.data cur_request = json.loads(request.data) else: #cur_request = """{"url": "", "debug":true}""" #cur_request = """{"url": "", "debug":true}""" #cur_request = """{"url": "", "debug":true}""" cur_request = """{"url": "http://localhost/images/scrubs.jpg", "debug":true}""" #cur_request = """{"url": "http://www.newrichstrategies.com/wp-content/uploads/2012/03/How-to-Find-Good-People-in-Your-Life.jpg", "debug":false}""" #cur_request = """{"url": "http://greenobles.com/data_images/frank-lampard/frank-lampard-02.jpg", "debug":true}""" #cur_request = """{"url": "http://www.billslater.com/barack__obama.jpg"}""" #cur_request = """{"url": "http://celebrityroast.com/wp-content/uploads/2011/01/arnold-schwarzenegger-body-building.jpg", "debug":false}""" #cur_request = """{"url": "http://face2face.si.edu/.a/6a00e550199efb8833010536a5483e970c-800wi", "debug":true}""" #cur_request = """{"url": "http://collider.com/uploads/imageGallery/Scrubs/scrubs_cast_image__medium_.jpg", "debug":false}""" #cur_request = """{"url": "http://localhost/images/Kevin_Bacon_at_the_2010_SAG_Awards.jpg", "debug":false}""" #cur_request = """{"url": "http://cdn02.cdn.justjared.com/wp-content/uploads/headlines/2012/02/anna-faris-oscars-red-carpet-2012.jpg", "debug":true}""" #cur_request = """{"url": "http://www.viewzone.com/attractive.female.jpg", "debug":true}""" cur_request = json.loads(cur_request) print cur_request["url"] img = Image(str(cur_request["url"])) img = img.scale(2.0) debug = True #if "debug" in cur_request: # debug = cur_request["debug"] chosen_faces = [] faces = img.findHaarFeatures(face_cascade) if faces is not None: for face in faces: face_features = [] invalid_face = False face_rect = Rect(face.x - (face.width() / 2), face.y - (face.height() / 2), face.width(), face.height()) for chosen_face in chosen_faces: if face_rect.colliderect(chosen_face): invalid_face = True break if invalid_face: break nose = None mouth = None left_eye = None right_eye = None cur_face = img.crop(face.x, face.y, face.width(), face.height(), centered=True) #cur_face = face.crop() noses = cur_face.findHaarFeatures(nose_cascade) mouths = cur_face.findHaarFeatures(mouth_cascade) eyes = cur_face.findHaarFeatures(eye_cascade) face_left_edge = face.x - (face.width() / 2) face_top_edge = face.y - (face.height() / 2) if noses is not None: nose = noses[0] nose_dist = (abs(nose.x - (face.width() / 2)) + abs(nose.y - (face.height() * 5 / 9)) + abs(nose.width() - (face.width() / 4))) for cur_nose in noses: cur_dist = (abs(cur_nose.x - (face.width() / 2)) + abs(cur_nose.y - (face.height() * 5 / 9)) + abs(cur_nose.width() - (face.width() / 4))) if cur_dist < nose_dist: nose = cur_nose nost_dist = cur_dist if nose and (nose.y < (face.height() / 3)): nose = None if nose and mouths is not None: mouth = mouths[0] mouth_dist = abs(mouth.x - nose.x) + (abs(mouth.y - (face.height() * 4 / 5)) * 2) for cur_mouth in mouths: cur_dist = abs(cur_mouth.x - nose.x) + (abs(cur_mouth.y - (face.height() * 4/ 5)) * 2) if (cur_dist < mouth_dist) and (cur_mouth.y > nose.y): mouth = cur_mouth mouth_dist = cur_dist if nose and eyes: right_eye = eyes[0] right_eye_dist = (abs(right_eye.x - (3 * face.width() / 4)) * 2 + abs(right_eye.y - (nose.y - (nose.height() / 2)) / 2) + abs(right_eye.width() - (face.width() / 3))) for cur_eye in eyes: cur_right_dist = (abs(cur_eye.x - (3 * face.width() / 4)) + abs(cur_eye.y - (nose.y - (nose.height() / 2)) / 2) + abs(cur_eye.width() - (face.width() / 3))) if (cur_right_dist <= right_eye_dist): # and (cur_eye.y < nose.y): right_eye = cur_eye right_eye_dist = cur_right_dist if nose and right_eye and (((right_eye.y - (right_eye.height() / 2)) > nose.y) or (right_eye.x < nose.x)): print "Culling right_eye" right_eye = None if nose and mouth: chosen_faces.append(face_rect) x_face = face.x - (face.width() / 2) y_face = face.y - (face.height() / 2) x_nose = nose.x - (nose.width() / 2) y_nose = nose.y - (nose.height() / 2) # Setup TopHat Image scale_factor = face.width() / 175.0 cur_hat = hat.copy() cur_hat = cur_hat.scale(scale_factor) cur_hat_mask = hat_mask.copy() cur_hat_mask = cur_hat_mask.scale(scale_factor) cur_hat_mask = cur_hat_mask.createAlphaMask(hue_lb=0, hue_ub=100) # Calculate the hat position if (face.y - face.height() / 2) > cur_hat.height: x_hat = face.x - (cur_hat.width / 2) y_hat = face.y - (face.height() * 7 / 10) - (cur_hat.height / 2) img = img.blit(cur_hat, pos=(x_hat, y_hat), alphaMask=cur_hat_mask) if mouth: x_mouth = mouth.x - (mouth.width() / 2) y_mouth = mouth.y - (mouth.height() / 2) # Setup Mustache Image cur_stache = stache.copy() scale_factor = ((nose.width() / 300.0) + (face.width() / 600.0)) / 2.0 cur_stache = cur_stache.scale(scale_factor) stache_mask = cur_stache.createAlphaMask(hue_lb=0, hue_ub=10).invert() # Calculate the mustache position bottom_of_nose = y_nose + (nose.height() * 4 / 5) top_of_mouth = y_mouth # if top_of_mouth > bottom_of_nose: # top_of_mouth = bottom_of_nose y_must = y_face + ((bottom_of_nose + top_of_mouth) / 2) - (cur_stache.height / 2) middle_of_nose = nose.x middle_of_mouth = mouth.x x_must = x_face + ((middle_of_nose + middle_of_mouth) / 2) - (cur_stache.width / 2) if right_eye: x_right_eye = right_eye.x - (right_eye.width() / 2) y_right_eye = right_eye.y - (right_eye.height() / 2) # Setup Monocle Image cur_mono = monocle.copy() scale_factor = ((right_eye.width() / 65.0) + (face.width() / 200.0)) / 2.0 cur_mono = cur_mono.scale(scale_factor) mono_mask = cur_mono.createAlphaMask(hue_lb=0, hue_ub=100).invert() # Calculate Monocle Position x_mono = x_face + x_right_eye y_mono = y_face + y_right_eye img = img.blit(cur_mono, pos=(x_mono, y_mono), alphaMask=mono_mask) img = img.blit(cur_stache, pos=(x_must, y_must), alphaMask=stache_mask) if debug: noselayer = DrawingLayer((img.width, img.height)) nosebox_dimensions = (nose.width(), nose.height()) center_point = (face.x - (face.width() / 2) + nose.x, face.y - (face.height() / 2) + nose.y) nosebox = noselayer.centeredRectangle(center_point, nosebox_dimensions, width=3) img.addDrawingLayer(noselayer) img = img.applyLayers() else: print "Face culled:" if not nose: print " No Nose" if not mouth: print " No mouth" if not right_eye: print " No right eye" print if debug: face_left_edge = face.x - (face.width() / 2) face_top_edge = face.y - (face.height() / 2) facelayer = DrawingLayer((img.width, img.height)) facebox_dimensions = (face.width(), face.height()) center_point = (face.x, face.y) facebox = facelayer.centeredRectangle(center_point, facebox_dimensions, Color.BLUE) img.addDrawingLayer(facelayer) if noses: for nose in noses: noselayer = DrawingLayer((img.width, img.height)) nosebox_dimensions = (nose.width(), nose.height()) center_point = (face.x - (face.width() / 2) + nose.x, face.y - (face.height() / 2) + nose.y) nosebox = noselayer.centeredRectangle(center_point, nosebox_dimensions) img.addDrawingLayer(noselayer) if mouths: for mouth in mouths: mouthlayer = DrawingLayer((img.width, img.height)) mouthbox_dimensions = (mouth.width(), mouth.height()) center_point = (face.x - (face.width() / 2) + mouth.x, face.y - (face.height() / 2) + mouth.y) mouthbox = mouthlayer.centeredRectangle(center_point, mouthbox_dimensions, Color.GREEN) img.addDrawingLayer(mouthlayer) if eyes: for right_eye in eyes: right_eyelayer = DrawingLayer((img.width, img.height)) right_eyebox_dimensions = (right_eye.width(), right_eye.height()) right_eye_center_point = (face_left_edge + right_eye.x, face_top_edge + right_eye.y) right_eyebox = right_eyelayer.centeredRectangle(right_eye_center_point, right_eyebox_dimensions) img.addDrawingLayer(right_eyelayer) img = img.applyLayers() img = img.scale(0.5) w_ratio = img.width / 800.0 h_ratio = img.height / 600.0 if h_ratio > 1.0 or w_ratio > 1.0: if h_ratio > w_ratio: img = img.resize(h=600) else: img = img.resize(w=800) output = StringIO.StringIO() img.getPIL().save(output, format="JPEG") #, quality=85, optimize=True) img_contents = output.getvalue() mimetype = "image/jpeg" return app.response_class(img_contents, mimetype=mimetype, direct_passthrough=False)
from SimpleCV import Image from os import getcwd, walk f = [] for (dirpath, dirnames, filenames) in walk(getcwd()): f.extend(filenames) break #print(f) for imgFile in f: img = Image(imgFile) feat = img.findHaarFeatures('face.xml', min_neighbors=5) count = 0 for face in feat: i = face.crop() i.save('face' + str(count) + '_' + imgFile)
with picamera.PiCamera() as camera: while True: #acquire image print "Please align your face with the camera" time.sleep(1) camera.start_preview() #add overlay o = camera.add_overlay(np.getbuffer(a), layer = 3, alpha = 64) time.sleep(10) camera.capture('pipw.jpg') camera.remove_overlay(o) #process image image = Image("pipw.jpg") camera.stop_preview() faces = image.findHaarFeatures("face.xml") faces.draw() face = faces[-1] #link image to your name and personal greeting print "Please Enter Your Name" name = raw_input() print("Please enter a personalized greeting message:") greeting = raw_input() print("Please enter the exact name of your favorite subreddit: ") myCity = raw_input() print("Please enter the zip code of your current location:") myzip = raw_input() #save image as password_yourName.jpg face.crop().save("Saved_Passwords/password_" + str(name) + ".jpg")
while disp.isNotDone(): x = None y = None width = None height = None coordinates = (x, y) tempApple = None exist = True if disp.mouseLeft: exist = not exist if disp.mouseRight: cycle() # Look for a face tempImage = image faces = image.findHaarFeatures('face') if faces is not None: # Get the largest face faces = faces.sortArea() bigFace = faces[-1] # Draw a green box around the face bigFace.draw() width = bigFace.width() height = bigFace.height() (x, y) = bigFace.coordinates() coordinates = (x - width / 2, y - height / 2) if exist == True: tempApple = apple.scale(width, height) mask = tempApple.invert().invert() tempImage = tempImage.blit(img=tempApple, pos=coordinates, mask=mask) tempImage.save(disp)
face_method.append(new) if add_new: faces.append(new) ind = 0 for paint in db: print str(ind) i = ImageCV('./barroco/' + str(paint ['id']) + '.jpg') paint['resolution'] = {'width':i.size()[0], 'height':i.size()[1]} paint['faces'] = [] paint['face_methods'] = {} for xml in xmls: faces = i.findHaarFeatures( '/usr/local/share/OpenCV/haarcascades/' + xml['file']) face_method = [] paint['face_methods'][xml['code']]=face_method if faces: for f in faces: fuzzy_add({ 'x':f.coordinates()[0], 'y':f.coordinates()[1], 'width':f.width(), 'height':f.height() }, paint['faces'], face_method) ind+=1 if ind >= 100: break;
from SimpleCV import Camera, Display, Image, HaarCascade display = Display() #lenna = Image("c:\\lenna.png") #face = lenna.crop(200,200,200,200) lenna = Image("C:\\Users\\Vijay\\Pictures\\aidan july 2010 pictures 008.jpg") segment = HaarCascade("C:\\haarcascade_fullbody.xml") autoface = lenna.findHaarFeatures(segment) if ( autoface is not None ): face = autoface[-1].crop() while display.isNotDone(): face.show()
from SimpleCV import Image image = Image("./TestFiles/faces.jpg") faces = image.findHaarFeatures('face.xml', min_neighbors=5) faces.draw(width=4) image.show()
# open display display = Display() print "Display opening..." while display.isNotDone(): pir_output = GPIO.input(37) num_imgs = 0 if pir_output: num_imgs = num_imgs + 1 print "capturing image..." os.system('raspistill -n -w 500 -h 500 -o cam.jpg') img = Image("cam.jpg") print 'searching for features...' profile_features = img.findHaarFeatures('profile.xml') lower_features = img.findHaarFeatures('lower_body.xml') upper_features = img.findHaarFeatures('upper_body.xml') face_features = img.findHaarFeatures('face.xml') if (face_features is not None and len(face_features) != 0): img.show() print 'Face features found at:' for f in face_features: coord = f.coordinates print coord string1 = b'Face detected! \n' string1_encode = string1.encode() ser.write(string1) #string1_encode) elif (profile_features is not None and len(profile_features) != 0): img.show() print 'Profile features found at:'
import os filename = 'frame' # Save each loop's image under the same name so it's overwritten try: print 'Press Ctrl+C to exit.' while True: # Take an image using raspistill. The small resolution helps speed things up print 'Capturing...' os.system('raspistill -n -w 500 -h 500 -o %s.jpg' % filename) img = Image('%s.jpg' % filename) # Pass the captured image to SimpleCV print 'Searching...' # This is the bit that does all the work: # Try putting 'upper_body.xml' or 'eye.xml' in place of 'face.xml' faces = img.findHaarFeatures('face.xml') if faces != []: # If any faces are found print 'Face(s) found at:' for face in faces: # Print the coordinates for each face found coOrd = face.coordinates() print '%s' % coOrd else: print 'No faces found.' print '--------------------' # This just breaks things up for easy viewing except KeyboardInterrupt: print 'Exit'
def doface(aa, f1, cc, f2, ee): camera = PiCamera() #imgg = Image('img1.jpg') #disp = Display(imgg.size()) dsize = (640, 480) disp = Display(dsize) #drawing = Image('mustache.png') #maskk = drawing.createAlphaMask() #camera.start_preview() #sleep(2) #['right_eye.xml', 'lefteye.xml', 'face3.xml', 'glasses.xml', # 'right_ear.xml', 'fullbody.xml', 'profile.xml', 'upper_body2.xml', # 'face.xml', 'face4.xml', 'two_eyes_big.xml', 'right_eye2.xml', # 'left_ear.xml', 'nose.xml', 'upper_body.xml', 'left_eye2.xml', # 'two_eyes_small.xml', 'face2.xml', 'eye.xml', 'face_cv2.xml', # 'mouth.xml', 'lower_body.xml'] while disp.isNotDone(): camera.capture('img2.png') img = Image('img2.png') img = img.resize(640, 480) #whatt = img.listHaarFeatures() faces = img.findHaarFeatures('face.xml') print 'faces:', faces if faces: #is not None: face = faces.sortArea()[-1] #print 'size:',face.size if aa == 'none': break elif aa == 'block': face.draw() else: f0draw = aa + '.png' draw0 = Image('use/' + f0draw) face = face.blit(draw0, pos=(100, 200)) #bigFace = face[-1] myface = face.crop() if f1 and cc is not None: feature01 = f1 + '.xml' f1draw = cc + '.png' draw1 = Image('/home/pi/cv/use/' + f1draw) feature1s = myface.findHaarFeatures(feature01) if feature1s is not None: feature1 = feature1s.sortArea()[-1] xpos1 = face.points[0][0] + feature1.x - (draw1.width / 2) ypos1 = face.points[0][ 1] + feature1.y #+ (2*draw1.height/3) #pos = (xmust,ymust) img = img.blit(draw1, pos=(xpos1, ypos1)) #mask=maskk) if f2 and ee is not None: feature02 = f2 + '.xml' f2draw = ee + '.png' draw2 = Image('/home/pi/cv/use/' + f2draw) feature2s = myface.findHaarFeatures(feature02) if feature2s is not None: feature2 = feature2s.sortArea()[-1] xpos2 = face.points[0][0] + feature2.x - (draw2.width / 2) ypos2 = face.points[0][ 1] + feature2.y #+ (2*draw2.height/3) #pos = (xmust,ymust) img = img.blit(draw2, pos=(xpos2, ypos2)) #mask=maskk) img.save(disp) else: print 'no face~~'
while (1): try: # camera.start_preview() time.sleep(3) print "Capturing image" camera.capture('_foto.jpg') # camera.stop_preview() print "processing" # camera.stop_preview() foto=Image("_foto.jpg") # foto=Image("test3.jpeg") print(foto.listHaarFeatures()) features=foto.findHaarFeatures('face.xml', 1.3, 3) if features: features.sortArea() #largest face first for face in features: print "Face found at coordinate : " + str(face.coordinates()) + " Area: " + str(face.area()) facecount = facecount+1 # face.draw() face.crop().save('face-f_%d.jpg' % facecount) # foto.save('foto_%d.jpg' % facecount) # else: # print "None found" # features=foto.findHaarFeatures('profile.xml', 1.3, 3) # if features: # features.sortArea() #largest face first
#! /usr/bin/python2.7 # Program to Test Haar Features import picamera from SimpleCV import Image import time with picamera.PiCamera() as camera: camera.resolution = (640, 480) camera.start_preview() time.sleep(2) camera.capture('foto.jpg') foto = Image("foto.jpg") print(foto.listHaarFeatures()) trovati = foto.findHaarFeatures('face.xml') if trovati: for trovato in trovati: print "Face Found at Coordinates : " + str(trovato.coordinates()) trovato.draw() else: print "Not Found" camera.stop_preview() foto.save('foto1.jpg') foto.show() time.sleep(10)
from SimpleCV import Display, Image import os filename = 'frame' # Save each loop's image under the same name so it's overwritten try: print 'Press Ctrl+C to exit.' while True: # Take an image using raspistill. The small resolution helps speed things up print 'Capturing...' os.system('raspistill -n -w 500 -h 500 -o %s.jpg' % filename) img = Image('%s.jpg' % filename) # Pass the captured image to SimpleCV print 'Searching...' # This is the bit that does all the work: # Try putting 'upper_body.xml' or 'eye.xml' in place of 'face.xml' faces = img.findHaarFeatures('face.xml') if faces != []: # If any faces are found print 'Face(s) found at:' for face in faces: # Print the coordinates for each face found coOrd = face.coordinates() print '%s' % coOrd else: print 'No faces found.' print '--------------------' # This just breaks things up for easy viewing except KeyboardInterrupt: print 'Exit'
#!/usr/bin/python2.7 # Programma test Haar Features import picamera from SimpleCV import Image import time with picamera.PiCamera() as camera: camera.resolution = (640, 480) camera.start_preview() time.sleep(10) camera.capture('foto.jpg') foto = Image("foto.jpg") print(foto.listHaarFeatures()) trovati = foto.findHaarFeatures('face.xml') if trovati: for trovato in trovati: print "Found all coordinate : " + str(trovato.coordinates()) trovato.draw() else: print "No found" camera.stop_preview() foto.save('foto1.jpg') foto.show() time.sleep(10)
#!/usr/bin/python2.7 #A proof-of-concept image processing code from picamera import PiCamera from SimpleCV import Image import time camera = PiCamera() camera.resolution = (640, 480) camera.start_preview() time.sleep(5) camera.capture('image.jpg') img = Image("image.jpg") eyes = img.findHaarFeatures('two_eyes_big.xml') if (eyes is not None): eyes = eyes[-1].crop() eyes.show() else: print "Not found" camera.stop_preview() img.show() zoomEyes = eyes.scale(3) zoomEyes.save('eyes.jpg') zoomEyes.show() time.sleep(5)