PostgreSQL Application in Image Search and Video and Image Deduplication

Image for post
Image for post

Background

Multiple image processing business scenarios, such as image search, video deduplication, face recognition, image beautification, and image deduplication, encounter the common challenge of managing the humongous volume of duplicate images and videos stored on the server.

For instance, users may upload many videos where specific video has different versions with different resolutions, audio tracks, or compression ratios. This leads to a large number of duplicate videos saved on the server. Another example is the identification of porn videos or images. It is crucial to identify such content but the challenge is how to identify porn videos and images.

On the other hand, image search is the second most common search engine after text search. Image search engines, such as Alibaba Cloud Image Search, allow you to upload a particular picture to the interface and search out a bunch of similar pictures.

This article describes how to screen out duplicate videos and how to implement an efficient image search using PostgreSQL. With the omnipotent API of PostgreSQL, it is easy to extend its image search function.

PostgreSQL Image Search Plug-in: Background Technology

The PostgreSQL image search plug-in uses the mainstream Haar wavelet technology to store images after conversion. Take a look at its Wikipedia page to learn more about this technology.

You can also refer to this document to know more about how to search similar images in PostgreSQL.

Following are some examples to illustrate the Haar wavelet technology application.

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Introduction to the PostgreSQL Image Search Plug-in

Follow the steps below to install the PostgreSQL Image Search Plug-in:

Step 1. Add dependency on gd.h.

Step 2. Download and install imgsmlr.

Step 3. Install the plug-in.

The following are two newly added data types in imgsmlr.

Image for post
Image for post

The KNN operator and gist index method that supports the pattern and signature types can be used for similar image search.

Image for post
Image for post

The following table shows several newly added functions. Now, you can convert binary images to the pattern type and convert the data stored in the pattern into the signature type.

Image for post
Image for post

PostgreSQL Image Search Plug-in: Testing

Follow the steps listed below to test the PostgreSQL Image Search Plug-in:

Step 1. Import some images as shown below (the more the better).

Image for post
Image for post

Step 2. Now, create the image table.

Step 3. Import the images to the database.

Step 4. Next, convert the images to the pattern and signature types.

Step 5. Create indexes as shown below.

Step 6. Now you can run an approximation query, such as querying images that are similar to id = : id images and retrieve the top 10 items on the similarity ranking list.

Here, you can use KNN indexing to quickly get output based on the similarity rankings.

Let’s take a look at the following examples.

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Video Deduplication

For video deduplication, extract keyframes in a video to generate the Cartesian product through self-correlation. Calculate the similarity between any two images of different videos, and when the similarity reaches a certain threshold value, the two videos are deemed the same.

Refer to the following example for better understanding.

Conclusion

In short, PostgreSQL is a very powerful database with highly customizable functions and does not require you to make any changes to the PostgreSQL kernel. Therefore, it is safe and reliable. The image search technology is an example of PostgreSQL feature extension and offers extremely high speed.

Build your own PostgreSQL solution on Alibaba Cloud with ApsaraDB for RDS PostgreSQL.

Original Source:

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store