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.
From this point onward, everything except preparing the necessary files will be performed within the Colaboratory.
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
For now, click on the haarcascade_frontalcatface.xml file to recognize the cat's face.
Right-click on Raw to save the file.
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.
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
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.
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.
Please refer to sites such as OpenCV-Python Tutorial.
Recommended Posts