[PYTHON] Try OpenCV with Google Colaboratory

You can do the following without installation

Input image (original image)

The output image (image after conversion) and the position of the cat's face are recognized, and a circle is added there.

You will need a Google account as you will be using Google Colaboratory.

1. Connect to Google Colaboratory

Search for "Google Drive" and sign in with your Google account.

Select "New", "Other", and "Add App".

Select Colaboratory.

Click Continue to install Colaboratory.

After installation, you can select "Google Colaboratory" from "New" and "Other".

If you select "Google Colaboratory", a new file will be created and Colaboratory will start.

From this point onward, everything except preparing the necessary files will be performed within the Colaboratory.

2. OpenCV, loading drawing library (matplotlib)

Type the following in the first cell of the Colaboratory and press Shift + Enter. It is OK if the OpenCV version (4.1.2 as of 2020.08.17) is displayed.

%matplotlib inline #Inline matplotlib output(Not a pop-up)To.
import cv2 #load opencv
import matplotlib.pyplot as plt #Load matplotlib and use pyplot with the name plt.
print(cv2.__version__) #Display the version of opencv and check the loading.
#output
# 4.1.2

3. Prepare the necessary files

Download the trained data from https://github.com/opencv/opencv/tree/master/data/haarcascades ..

For now, click on the haarcascade_frontalcatface.xml file to recognize the cat's face.

Right-click on Raw to save the file.

4. Upload the file to Colaboratory

Write the following code in the second cell and press Shift + Enter.

from google.colab import files #Use file operations from the colaboratory library.
f = files.upload() #Upload the file.

Press "Select File" to upload the file.

The file uploads the following two.

  1. Image file you want to convert
  2. haarcascade_frontalcatface.xml (Prepare the necessary files, the file downloaded in)

Write the following code in the next cell, press Shift + Enter and execute it, and check that the name of the uploaded file is output (in this example, the file name of the image you want to convert is "IMG_20200808_181512" .jpg ").

!ls #Execute the system command ls to check if the file is uploaded.
#output
# haarcascade_frontalcatface.xml	IMG_20200808_181512.jpg  sample_data

5. Load images with OpenCV

Write the following code in a new cell and press Shift + Enter to execute it.

#file name(IMG_20200808_181512.jpg)Please change to the file name of the image you uploaded.
img = cv2.imread("./IMG_20200808_181512.jpg ") #Loading images
show_img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) #Create an image with BGR format converted to RGB format for display.
plt.imshow(show_img) # matplot.Display the image read using lib.

It is OK if the uploaded image is displayed as shown below.

6. Face recognition and draw a circle and an arrow at the position of the face

Write the following code in a new cell and press Shift + Enter to run it. For details on cv2.circle and cv2.putText, see [OpenCV drawing function](http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_gui/py_drawing_functions/ Please refer to py_drawing_functions.html) etc.

#Generate a model from a trained file
face_cascade = cv2.CascadeClassifier('./haarcascade_frontalcatface.xml')
#Generate gray image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#Face recognition
faces = face_cascade.detectMultiScale(gray, 1.3, 5)

#Loop with recognition result x,y:Upper left of face rectangle, w,h:Width and height of face rectangle
for (x,y,w,h) in faces:
  #Draw a circle at the position of the face
  img = cv2.circle(img,(x+int(w/2),y+int(h/2)),int(max(w/2,h/2)),(0,0,255),12)
  #Draw an arrow with letters
  cv2.putText(img,"<---",(x+w,y+int(h/2)),cv2.FONT_HERSHEY_SIMPLEX,8,(0,0,255),32,cv2.LINE_AA)

#Convert to RGB for drawing
show_img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
#Drawing the result
plt.imshow(show_img)

If the cat's face is circled and has an arrow as shown below, it is successful. However, there are times when the face cannot be recognized. For example, in this image, the calico cat is facing you behind, but the face is not recognizable.

7. Other

Please refer to sites such as OpenCV-Python Tutorial.

Recommended Posts

Try OpenCV with Google Colaboratory
OpenCV feature detection with Google Colaboratory
Try edge detection with OpenCV
Try Google Mock with C
Try StyleGAN on Google Colaboratory
Study Python with Google Colaboratory
Google colaboratory
Try face recognition with python + OpenCV
Try to implement linear regression using Pytorch with Google Colaboratory
Try using the camera with Python's OpenCV
Try using Python with Google Cloud Functions
How to search Google Drive with Google Colaboratory
Make a cascade classifier with google colaboratory
Manage deals with Trello + Google Colaboratory (Part 1)
Using Java's Jupyter Kernel with Google Colaboratory
Use TPU and Keras with Google Colaboratory
Try running Google Chrome with Python and Selenium
Try converting videos in real time with OpenCV
I tried simple image processing with Google Colaboratory.
Cheat sheet when scraping with Google Colaboratory (Colab)
Try to detect fish with python + OpenCV2.4 (unfinished)
Authenticate Google with Django
Google Colaboratory setup summary
Try scraping with Python.
Detect stoop with OpenCV
Binarization with OpenCV / Python
Try SNN with BindsNET
Rotate sprites with OpenCV
Data Augmentation with openCV
Try regression with TensorFlow
Easy TopView with OpenCV
Stumble with homebrew opencv3
Try projective transformation of images using OpenCV with Python
How to load files in Google Drive with Google Colaboratory
End-to-End single channel sound source separation with Google Colaboratory
How to analyze with Google Colaboratory using Kaggle API
Easy learning of 100 language processing knock 2020 with "Google Colaboratory"
Building an environment to use CaboCha with google colaboratory
Try to factorial with recursion
Try function optimization with Optuna
Try converting webcam / camcorder videos in real time with OpenCV
Try deep learning with TensorFlow
Try using PythonTex with Texpad.
Test embedded software with Google Test
"Apple processing" with OpenCV3 + Python3
Code snippets often used when processing videos with Google Colaboratory
Try implementing RBM with chainer.
Use music21 on Google Colaboratory
Image editing with python OpenCV
Try using matplotlib with PyCharm
Try programming with a shell!
Camera capture with Python + OpenCV
Try GUI programming with Hy
Try an autoencoder with Pytorch
Try matrix operation with NumPy
Try using OpenCV on Windows
Try implementing XOR with PyTorch
Try running CNN with ChainerRL
About learning with google colab
Try Deep Learning with FPGA
Binarize photo data with OpenCV