Predictive Hacks

Get Started with OpenCV

opencv

In this post, we will provide some examples of what you can do with OpenCV.

Blending Images in OpenCV

We will give a walk-through example of how we can blend images using Python OpenCV. Below we represent the target and the filter images.

Target Image

Blending Images with OpenCV 2

Filter Image

Blending Images with OpenCV 3
import cv2
 
# Two images
img1 = cv2.imread('target.jpg')
img2 = cv2.imread('filter.png')
 
# OpenCV expects to get BGR images, so we will convert from BGR to RGB
 
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
 
# Resize the Images. In order to blend them, the two images
# must be of the same shape
 
img1 =cv2.resize(img1,(620,350))
img2 =cv2.resize(img2,(620,350))
 
 
# Now, we can blend them, we need to define the weight (alpha) of the target image
# as well as the weight of the filter image
# in our case we choose 80% target and 20% filter
blended = cv2.addWeighted(src1=img1,alpha=0.8,src2=img2,beta=0.2,gamma=0)
 
# finally we can save the image. Now we need to convert it from RGB to BGR
 
cv2.imwrite('Blending.png',cv2.cvtColor(blended, cv2.COLOR_RGB2BGR))

And voilà!

Blending Images with OpenCV 4

Processing Images in OpenCV

We will show how you can apply image processing with OpenCV in Python. We will work with this image obtained from Unsplash.

Image Processing in OpenCV Python 1

How to Blur the Image

import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
 
img = cv2.imread('panda.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
blurred_img = cv2.blur(img,ksize=(20,20))
cv2.imwrite("blurredpanda.jpg", blurred_img) 
Image Processing in OpenCV Python 2

How to apply Sobel Operator

You can have a look at Sobel Operator at Wikipedia and you can also start experimenting with some filters. Let’s apply the horizontal and vertical Sobel Operator.

img = cv2.imread('panda.jpeg',0)
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5)
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=5)
 
cv2.imwrite("sobelx_panda.jpg", sobelx) 
cv2.imwrite("sobely_panda.jpg", sobely) 
Image Processing in OpenCV Python 3
Image Processing in OpenCV Python 4

How to apply a Threshold to an Image

We can also binarize the images.

img = cv2.imread('panda.jpeg',0)
ret,th1 = cv2.threshold(img,100,255,cv2.THRESH_BINARY)
fig = plt.figure(figsize=(12,10))
plt.imshow(th1,cmap='gray')
Image Processing in OpenCV Python 5

Face Detection In OpenCV

We will discuss about how we can apply Face Detection using OpenCV. We go straightforward with a practical reproducible example.

The logic it the following: We get the image from the URL (or from the hard disk). We convert it to an numpy array and then to a gray scale. Then by applying the proper CascadeClassifier we get the bounding boxes of the faces. Finally, using PIllow (or even OpenCV) we can draw the boxes on the initial image.

import cv2 as cv
import numpy as np
import PIL
from PIL import Image
import requests
from io import BytesIO
from PIL import ImageDraw
 
 
# I have commented out the cat and eye cascade. Notice that the xml files are in the opencv folder that you have downloaded and installed
# so it is good a idea to write the whole path
face_cascade = cv.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_default.xml')
#cat_cascade = cv.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalcatface.xml')
#eye_cascade = cv.CascadeClassifier('C:\\opencv\\build\\etc\\haarcascades\\haarcascade_eye.xml')
 
 
URL = "https://images.unsplash.com/photo-1525267219888-bb077b8792cc?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80"
response = requests.get(URL)
img = Image.open(BytesIO(response.content))
img_initial = img.copy()
 
# convert it to np array
img = np.asarray(img)
 
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
 
faces = face_cascade.detectMultiScale(gray)
# And lets just print those faces out to the screen
#print(faces)
 
drawing=ImageDraw.Draw(img_initial)
 
# For each item in faces, lets surround it with a red box
for x,y,w,h in faces:
    # That might be new syntax for you! Recall that faces is a list of rectangles in (x,y,w,h)
    # format, that is, a list of lists. Instead of having to do an iteration and then manually
    # pull out each item, we can use tuple unpacking to pull out individual items in the sublist
    # directly to variables. A really nice python feature
    #
    # Now we just need to draw our box
    drawing.rectangle((x,y,x+w,y+h), outline="red")
display(img_initial)

The initial Image was this one:

Face Detection in OpenCV 1

And then after drawing the Bounding Boxes we got:

Face Detection in OpenCV 2

As we can see, we managed to get correctly the four faces BUT we discovered also a “ghost” behind the window…

Crop the faces to separate images

We can also crop the faces to separate images

for x,y,w,h in faces:
 
    img_initial.crop((x,y,x+w,y+h))
    display(img_initial.crop((x,y,x+w,y+h)))

For example, the first face that we get is:

Face Detection in OpenCV 3

Notice: In case you wanted to read the image from the hard disk you could simply type the following three lines:

# read image from the PC

initial_img=Image.open('my_image.jpg')
img = cv.imread('my_image.jpg')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

Face Detection Videos In OpenCV

This post is a practical example of how we can use OpenCV with Python for detecting faces in a video. In a previous post, we explained how to apply Object Detection in Tensorflow and Face Detection in OpenCV. Generally, the Computer Vision and the Object Detection is a hot topic in Artificial Intelligence. Think for instance the autonomous vehicles which must detect continuously many different objects around them (pedestrians, other vehicles, signs etc).

How to Record a Video of Face Detection

In the following example, we apply a Face Detection with our USB Camera and we write the video to an .mp4 file. As you can see, the OpenCV is able to detect the face, and when it is hiding behind the hands the OpenCV is losing it.

import cv2
# change your path to the one where the haarcascades/haarcascade_frontalface_default.xml is
face_cascade = cv2.CascadeClassifier('../DATA/haarcascades/haarcascade_frontalface_default.xml')
 
cap = cv2.VideoCapture(0) 
 
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
 
# MACOS AND LINUX: *'XVID' (MacOS users may want to try VIDX as well just in case)
# WINDOWS *'VIDX'
writer = cv2.VideoWriter('myface.mp4', cv2.VideoWriter_fourcc(*'XVID'),25, (width, height))
 
 
while True: 
     
    ret, frame = cap.read(0) 
      
    frame = detect_face(frame)
    writer.write(frame)
  
    cv2.imshow('Video Face Detection', frame) 
    # escape button to close it
    c = cv2.waitKey(1) 
    if c == 27: 
        break
         
cap.release() 
writer.release()
cv2.destroyAllWindows()

The Output of the Computer Vision Code

Few lines of code are needed to record this video with dynamic face detection. If you run the above block of code you will get a similar video (Of course with a different face 

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.