Improve a CMS’s photos library qualification with AI, Object Detection from images in Python with OpenCv and YOLO
Still digging, in Python, on more versatile usage in object detection from images to improve a house-made CMS multimedia searches. What a sentence! So, maybe, you have heard about the acronym YOLO one stands for “You Only Live Once”, a kind of modern version of the Latin “Carpe Diem”, the other YOLO stands for “You Only Look Once”.
So, let’s give a quick definition of the second YOLO thing and it’s all about image recognition.
What is this other YOLO thing?
YOLO is an extremely fast multi object detection algorithm which uses convolutional neural network (CNN) to detect and identify objects. Maybe less sexy than the first definition but pretty useful when like me you try to increase the images library relevance categorization…
At least, I also like the name of the library YOLO. I was figured out a funny dialog between developers mimicking pseudo rappers “What kinda python lib do you like, Bro!”, “YOLO, man! Totally. Need to check it out NOW!” 🙂
Anyway, let’s expose my objective.
To make better image categorization for a CMS
I have already exposed the issue, I am tackling with it already in a previous post dedicated to facial recognition.
- If you are interested, you can check the post “Improve a CMS’s photos library qualification with AI, facial recognition in python, to provide better images search results to users” at https://flaven.fr/2020/06/improve-a-cms-photos-library-qualification-with-ai-facial-recognition-in-python-to-provide-better-images-search-results-to-users/
If you ever read that post’s blog, I made few attempts on facial recognition with people faces mostly presidents. On a very European centric point of view, presidents can be range from known, less know to totally unknow!
Unfortunately, it is OK for human faces but not for any other element inside pictures to be recognized e.g. cars, buildings, dog, horse, human, tree…. whatever.
Well, as I am consistent or stubborn, my objective is still the same as a Backoffice’s PO: the multimedia library carries thousand of images that are uncorrectly tagged: no caption, no keywords… etc So, performing searches on that dataset is problematic. To be even more straightforward, I will answer “GIGO”, the famous “Garbage In, Garbage Out”. GIGO produces search results that suck! So how can the situation be improved, one possibility is to enhance image search with a fine-grained keywords attached to each picture but without making the job by humans. This is where you’d better call IA to the rescue.
Like always that is very practical issue, I won’t give you any explanations about the YOLO algorithm and what is “behind the curtain” with matrix and formula stuff both because it does not really interest me, I am user first and also because I am just no able to do so! No, I will focus on results !
Set-up your environnement
At the very beginning, I was looking for an example using TensorFlow Image Recognition but I decide to go for YOLO. I found it much easier to implement. Let’s see quickly what you need to play with YOLO.
Python librairies
First, I am using my mac and a remote disk to install all the required libraries with the help of anaconda, to save space disk. In addition to the principal Python libraries used in the scripts that are easy to install with pip or anaconda.
# YOLO object detection requirements import cv2 as cv import numpy as np import time # to parse the directory import os # to import in mysql import mysql.connector as mysql |
I just advise to use this command to install opencv.
# to install cv2 or opencv use this command pip install opencv-python # this command is not working pip install opencv |
You will have to download the YOLO files.
Got the look YOLO!
So easy to get YOLO, it requires 3 files to download:
- File_1 : In order to run the network, you will have to download the pre-trained YOLO weight file (237 MB). Download yolov3.weights
- File_2 : You have also download the the YOLO configuration file: yolov3.cfg – Download yolov3.cfg
- File_3 : Get the 80 COCO class names: coco.names – Download coco.names
You are done! You have the requirements to use YOLO.
Source: https://opencv-tutorial.readthedocs.io/en/latest/yolo/yolo.html
Few commands to use MySQL
I suppose that you have installed MySQL on your computer. I always forget the main commands when I am in the console so here are quick reminders.
For more explanations, you can check the previous post on facial recognition.
# to connect to the db mysql -u root -p |
# commands SELECT AND USE db SHOW DATABASES; USE yolo_images; # commands SELECT AND USE db SHOW TABLES; # DESCRIBE yolo_advanced_images DESCRIBE yolo_advanced_images; # SELECT a dump .sql SOURCE /Users/brunoflaven/Documents/01_work/blog_articles/tensorflow_image_recognition/mysql_yolo_advanced_images_1.sql # SELECT ALL records SELECT COUNT(*) FROM yolo_advanced_images; # SELECT ALL records SELECT * FROM yolo_advanced_images; # DELETE single image WITH the help OF the id DELETE FROM yolo_advanced_images WHERE id = '96'; DELETE FROM yolo_advanced_images WHERE id = '97'; # empty TABLE TRUNCATE TABLE yolo_advanced_images; |
The table structure `yolo_advanced_images` for databas yolo_images
DROP TABLE IF EXISTS `yolo_advanced_images`; CREATE TABLE `yolo_advanced_images` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `file` VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', `keywords` VARCHAR (255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; LOCK TABLES `yolo_advanced_images` WRITE; INSERT INTO `yolo_advanced_images` (`id`, `file`) VALUES (NULL,'image1.jpg', 'keyword_1_img_1'), (NULL,'image1.jpg', 'keyword_2_img_1'), (NULL,'image2.jpg', 'keyword_1_img_2'), (NULL,'image3.jpg', 'keyword_1_img_3' ); |
Conclusion: I have made also a translation in Spanish of the coco.names. You can just download the file coco_es.names from my github account. Meaning that I can tag in any language (English, Spanish, Russian, Portuguese…) images with the help of YOLO. So, for instance, I can provide a better image search in any language by providing a native language tagging system for the same dataset of images.
After, that, you can even improve the tagging system by coupling it with facial recognition. Using the principles detailed in my previous post on facial recognition, you’ll be able to precise who are the persons on pictures if it is a president, an NBA star… whatever the known faces images you drop in your sample aka the directory of know faces! So, that can inject proper noun like Macron, Trump, Bryan, Biden… in addition to the keyword created by YOLO for “person”. Then you are good! By injecting these new tags into the real multimedia database used in the CMS, you may considerably improve users’ images searches and never felt anymore so miserable!
Image copyright is www.vexels.com
Read more
- Convenient functions for YOLO v4 based on AlexeyAB Darknet Yolo.
https://darknet.gong.im/ - Darknet: Open Source Neural Networks in C
https://pjreddie.com/darknet/ - How to Perform YOLO Object Detection using OpenCV and PyTorch in Python
https://www.thepythoncode.com/article/yolo-object-detection-with-opencv-and-pytorch-in-python - yoloface
https://github.com/sthanhng/yoloface - Working With Files in Python
https://realpython.com/working-with-files-in-python/#listing-all-files-in-a-directory
- darknet
https://github.com/pjreddie/darknet - TensorFlow Image Recognition Python API Tutorial
https://towardsdatascience.com/tensorflow-image-recognition-python-api-e35f7d412a70 - Object Detection with ImageAI in Python
https://stackabuse.com/object-detection-with-imageai-in-python/ - YOLO Object Detection from image with OpenCV and Python
https://www.codespeedy.com/yolo-object-detection-from-image-with-opencv-and-python/ - Run YoloV3 detections on thousands of images and save outputs? #723
https://github.com/pjreddie/darknet/issues/723 - AlexeyAB/darknet
https://github.com/AlexeyAB/darknet - darkflow
https://github.com/thtrieu/darkflow - Review: YOLOv3 — You Only Look Once (Object Detection)
https://towardsdatascience.com/review-yolov3-you-only-look-once-object-detection-eab75d7a1ba6 - Tutorial: Build your custom real-time object classifier
https://towardsdatascience.com/tutorial-build-an-object-detection-system-using-yolo-9a930513643a - Image recognition with TensorFlow and Keras
https://developer.ibm.com/technologies/artificial-intelligence/articles/image-recognition-challenge-with-tensorflow-and-keras-pt1 - Deep Learning based Object Detection using YOLOv3 with OpenCV ( Python / C++ )
https://learnopencv.com/deep-learning-based-object-detection-using-yolov3-with-opencv-python-c/
- Deep Learning with OpenCV
https://www.pyimagesearch.com/2017/08/21/deep-learning-with-opencv/
- Top 8 Image-Processing Python Libraries Used in Machine Learning
https://neptune.ai/blog/image-processing-python-libraries-for-machine-learning - Image Recognition Tutorial in Python for Beginners
https://howtocreateapps.com/image-recognition-python/ - Image Recognition in Python with TensorFlow and Keras
https://stackabuse.com/image-recognition-in-python-with-tensorflow-and-keras/ - image-recognition on github
https://github.com/topics/image-recognition - Object Detection on Custom Dataset with YOLO (v5) using PyTorch and Python
https://curiousily.com/posts/object-detection-on-custom-dataset-with-yolo-v5-using-pytorch-and-python/ - Emergency-Vehicle-Classification-YOLO
https://github.com/SiddiAvinashM10/Emergency-Vehicle-Classification-YOLO - Create a simple image classifier using Tensorflow
https://medium.com/@linjunghsuan/create-a-simple-image-classifier-using-tensorflow-a7061635984a