In a time when AI is almost universal to family members, everywhere you go you will hear about AI. So is AI really popular or extremely sublime?
In my personal opinion, it’s both: it’s really difficult to say it’s sublime because it’s hard to understand deeply, understand it, and create new things, but it’s universal because nowadays the resources and libraries to use Available is indeed numerous and very accessible.
The sublime part probably doesn’t have to say more, but the common and simple part probably has many people disagreeing. To prove to you, that I am not joking, today I will show you how to recognize human faces on photos with only 3 lines of code. Let’s make it so easy!!!!
1. Environment setting
The first part is to set up the environment, for the simplest and most user-friendly I will use Google Colab. This is a free “cloud” service specializing in the field of AI, you can easily write and run Python code on notebooks right in your browser.
You just need to click on the link above to select File as New notebook
2. And we begin.
We will use 2 libraries for this face detection: cv2 – for reading, writing, image processing and face_detection – for face detection.
Because cv2 is already included by Colab, we just need to install face_detection, then import these 2 libraries to use.
! pip install face-detection
Next is to upload the image we want to try on Colab. Click the folder icon on the left then select the Upload button to upload photos.
Sau khi tải ảnh lên thành công các ảnh sẽ nằm dưới thư mục sample_data
Next are 3 lines of “normal” code:
detector = face_detection.build_detector(“DSFDDetector”, confidence_threshold=.5, nms_iou_threshold=.3) # initialize “model”
img = cv2.imread(“baeyoohuyn.jpg”) # read image with name filled in
detections = detector.detect(img) # detect faces in images
The detections variable will return a list of received faces in the image, each face will have the information about the area containing that face (coordinates of the lower left corner, coordinates of the upper right corner of the area) and the confidence level when receiving that face. face.
With the above example, we see that the model captures an area containing a human face with the lower left corner coordinates (218,98672, 144.85922) and the upper right corner coordinates (311,37233, 273.77774) with confidence 99.99% accuracy :>
That’s it, the identification part, 3 lines of code.
Now we will use cv2 to redraw the frame to see if the other 99.99% is correct.
# Blue color in BGR color = (255, 0, 0) # Line thickness of 2 px thickness = 3 # Draw detect box on image for face in range(len(detections)): pts1 = (detections[face],detections[face]) pts2 = (detections[face],detections[face]) image = cv2.rectangle(img, pts1, pts2, color, thickness) # rewrite detection image cv2.imwrite("detected.jpg", image)
And here is the finished product
I also tried it with a picture without a human face as well as a face of many people to see what the result was.
With a picture of only one monkey, the result is nothing
Photos with multiple faces:
2. Original code
Finally, here is the source code of the original library above, anyone who is interested can learn more.
If you have any questions about this interesting field, do not hesitate to comment.