In [6]:
import cv2
import numpy as np
import matplotlib.pyplot as plt
import os
from PIL import Image

In [2]:
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
smile_cascade = cv2.CascadeClassifier('haarcascade_smile.xml')

In [4]:
img = cv2.imread('arnie.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.1, 5)

for (x,y,w,h) in faces:
    cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
    roi_gray = gray[y:y+h, x:x+w]
    roi_color = img[y:y+h, x:x+w]
    eyes = eye_cascade.detectMultiScale(roi_gray)
    #smiles = smile_cascade.detectMultiScale(roi_color)
    
    for (ex,ey,ew,eh) in eyes:
         cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
    
    #for (sx,sy,sw,sh) in smiles:
    #     cv2.rectangle(roi_color,(sx,sy),(sx+sw,sy+sh),(255,0,255),2)

cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

In [8]:
img = cv2.imread('Image.png')
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

#image = cv2.imread('Image.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Detect faces in the image
faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=5,
    
)

print "Found {0} faces!".format(len(faces))

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
    #cv2.imshow("Adding faces to traning set...", img[y: y + h, x: x + w])

cv2.imshow("Faces found", img)
cv2.waitKey(0)
cv2.destroyAllWindows()


Found 4 faces!

In [9]:
path = './Images'
image_paths = [os.path.join(path, f) for f in os.listdir(path)]     #if f.endswith('.jpg')
#print image_path
# images will contains face images
images = []
# labels will contains the label that is assigned to the image
labels = []
for image_path in image_paths:
    # Read the image and convert to grayscale
    image_pil = Image.open(image_path).convert('L')
    # Convert the image format into numpy array
    image = np.array(image_pil, 'uint8')
    # Get the label of the image
    #nbr = int(os.path.split(image_path)[1].split(".")[0].replace("subject", ""))
    # Detect the face in the image
    faces = faceCascade.detectMultiScale(image)
    # If face is detected, append the face to images and the label to labels
    for (x, y, w, h) in faces:
        images.append(image[y: y + h, x: x + w])
        #labels.append(nbr)
        cv2.imshow("Adding faces to traning set...", image[y: y + h, x: x + w])
        cv2.waitKey(50)
cv2.destroyAllWindows()

In [ ]:


In [ ]:
x = images[1]
cv2.imshow("sample",x)
cv2.waitKey(0)
cv2.destroyAllWindows()

In [ ]:
x

In [ ]:
from PIL import Image, ImageFilter
#Read image
im = Image.open( 'ankitanarula.jpg' )
#Display image
im.show()

#Applying a filter to the image
im_sharp = im.filter( ImageFilter.SHARPEN )
#Saving the filtered image to a new file
im_sharp.save( 'image_sharpened.jpg', 'JPEG' )

#Splitting the image into its respective bands, i.e. Red, Green,
#and Blue for RGB
r,g,b = im_sharp.split()

#Viewing EXIF data embedded in image
exif_data = im._getexif()
exif_data

In [ ]:


In [ ]:
from PIL import Image
import glob, os

size = 128, 128

for infile in glob.glob("*.jpg"):
    file, ext = os.path.splitext(infile)
    im = Image.open(infile)
    im.thumbnail(size, Image.ANTIALIAS)
    im.save(file + ".thumbnail", "JPEG")

In [ ]:


In [ ]:

import sys, math, Image def Distance(p1,p2): dx = p2[0] - p1[0] dy = p2[1] - p1[1] return math.sqrt(dx*dx+dy*dy) def ScaleRotateTranslate(image, angle, center = None, new_center = None, scale = None, resample=Image.BICUBIC): if (scale is None) and (center is None): return image.rotate(angle=angle, resample=resample) nx,ny = x,y = center sx=sy=1.0 if new_center: (nx,ny) = new_center if scale: (sx,sy) = (scale, scale) cosine = math.cos(angle) sine = math.sin(angle) a = cosine/sx b = sine/sx c = x-nx*a-ny*b d = -sine/sy e = cosine/sy f = y-nx*d-ny*e return image.transform(image.size, Image.AFFINE, (a,b,c,d,e,f), resample=resample) def CropFace(image, eye_left=(0,0), eye_right=(0,0), offset_pct=(0.2,0.2), dest_sz = (70,70)): # calculate offsets in original image offset_h = math.floor(float(offset_pct[0])*dest_sz[0]) offset_v = math.floor(float(offset_pct[1])*dest_sz[1]) # get the direction eye_direction = (eye_right[0] - eye_left[0], eye_right[1] - eye_left[1]) # calc rotation angle in radians rotation = -math.atan2(float(eye_direction[1]),float(eye_direction[0])) # distance between them dist = Distance(eye_left, eye_right) # calculate the reference eye-width reference = dest_sz[0] - 2.0*offset_h # scale factor scale = float(dist)/float(reference) # rotate original around the left eye image = ScaleRotateTranslate(image, center=eye_left, angle=rotation) # crop the rotated image crop_xy = (eye_left[0] - scale*offset_h, eye_left[1] - scale*offset_v) crop_size = (dest_sz[0]*scale, dest_sz[1]*scale) image = image.crop((int(crop_xy[0]), int(crop_xy[1]), int(crop_xy[0]+crop_size[0]), int(crop_xy[1]+crop_size[1]))) # resize it image = image.resize(dest_sz, Image.ANTIALIAS) return image if __name__ == "__main__": image = Image.open("arnie.jpg") CropFace(image, eye_left=(252,364), eye_right=(420,366), offset_pct=(0.1,0.1), dest_sz=(200,200)).save("arnie_10_10_200_200.jpg") CropFace(image, eye_left=(252,364), eye_right=(420,366), offset_pct=(0.2,0.2), dest_sz=(200,200)).save("arnie_20_20_200_200.jpg") CropFace(image, eye_left=(252,364), eye_right=(420,366), offset_pct=(0.3,0.3), dest_sz=(200,200)).save("arnie_30_30_200_200.jpg") CropFace(image, eye_left=(252,364), eye_right=(420,366), offset_pct=(0.2,0.2)).save("arnie_20_20_70_70.jpg")

In [ ]: