CROP AND SWAP 2 IMAGES

* Install cv2 module
command: pip install OpenCV-python

A. CROP THE IMAGES

* cv2.imread() :: loads an image from the specified file.SYNTAX: cv2.imread (path)
* cv2.CascadeClassifier() : used to load the models* I used frontal face pretrained model ::
https://github.com/nazmiasri95/Face-Recognition/blob/master/haarcascade_frontalface_default.xml
* detectMultiScale():: detect the faces. 
* This function will return a rectangle with coordinates(x,y,w,h) around the detected face.
-> Here faces indicate the above array
x1 = faces[0][0]
y1 = faces[0][1]
x2 = x1 + faces[0][2]
y2 = y1 + faces[0][3]
For img1 , For img2 ,
x1 = 226 x1 = 254
y1 = 65 y1 = 74
x2 = 226 + 138 = 364 x2 = 254 + 107 = 361
y2 = 65 + 138 = 203 y2 = 74 + 107 = 181

# Add an image in the window : display  
SYNTAX :: cv2.imshow (window_name, image)
# wait for a specific time in milliseconds
SYNTAX :: cv2.waitKey()
# Destroys the window showing image
SYNTAX :: cv2.destroyAllWindows()

B. SWAP THE CROPPED IMAGES

BASIC RULE FOR SWAPPING:
TEMP = a
b = TEMP
* For slicing , get the values from face1 
swap1 = img1[ y1 : y2 , x1 : x2 ]
img2[ y1 : y2 , x1 : x2 ] = swap1
* For slicing , get the values from face1 
swap2 = img2[ y1 : y2 , x1 : x2 ]
img1[ y1 : y2 , x1 : x2 ] = swap2

FINAL CODE :

DONE !!!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store