<figure><img src="image_1.png" /></figure>

HAL
open science

# Exploiting the Similarity of Top 100 Beauties for Hairstyle Recommendation via Perceptual Hash

Chentong Zhang, Jiajia Jiao

<figure><img src="image_2.png" /></figure>

► To cite this version:

Chentong Zhang, Jiajia Jiao. Exploiting the Similarity of Top 100 Beauties for Hairstyle Recommendation via Perceptual Hash. 2nd International Conference on Intelligence Science (ICIS), Nov 2018, Beijing, China. pp.49-59, 10.1007/978-3-030-01313-4_6. hal-02118820

HAL Id: hal-02118820

https://inria.hal.science/hal-02118820

Submitted on 3 May 2019

HAL is a multi-disciplinary open access
archive for the deposit and dissemination of sci-
entific research documents, whether they are pub-
lished or not. The documents may come from
teaching and research institutions in France or
abroad, or from public or private research centers.

L'archive ouverte pluridisciplinaire HAL, est
destinée au dépôt et à la diffusion de documents
scientifiques de niveau recherche, publiés ou non,
émanant des établissements d'enseignement et de
recherche français ou étrangers, des laboratoires
publics ou privés.

<figure><img src="image_3.png" /></figure>

Distributed under a Creative Commons Attribution 4.0 International License
Exploiting the similarity of top 100 beauties for hairstyle
recommendation via perceptual hash

Chentong Zhang, Jiajia Jiao

College of Information Engineering
Shanghai Maritime University
Shanghai, China

{zhangchentong@stu.shmtu.edu.cn, jiaojiajia@shmtu.edu.cn}

**Abstract.** In recent years, with the fast development of the fashion industry, the definition of beauty is constantly changing and the diversity of women's hairstyles always gives us a dazzling feeling. Most young female would like to pursue the varying fashion trend and change a hairstyle to meet the fashion requirements. How to choose an appropriate fashion hairstyle has become a critical issue for modern fashion women. In this paper, we design and implement a C++ based female hairstyle recommendation system. At first, the selected top 100 beauties per year in the world are collected for building the beauty standard. Then, considering the changing female status (e.g., age, weight), perceptual hash algorithm (pHash) is used to calculate the global similarity between the user and 100 beauties for selecting the appropriate hairstyle. Finally, the comprehensive rank results from more than 350 demonstrate the recommended hairstyle is preferred.

**Keywords:** Hairstyle recommendation; top 100 beauties; perceptual hash algorithm; Image similarity.

# 1 Introduction

Modern women pay more and more attention to fashion experience. Fashion expression is not only on clothing, but also on hairstyle. The common recommendation modules include Amazon's "guessing you like" block, and so on. It uses an object based collaborative filtering algorithm [1]. The key is that the system will record the user's previous search records to understand the user's preferences. At present, there is no recommendation system software for users to recommend hairstyles, most of them are similar to Meitu and other beauty software included in the wearing hairstyle model, users to speculate whether the hairstyle is suitable for itself. Obviously, there is a problem that the accuracy of hairdressing after face beauty treatment.

This paper discusses the techniques of recommendation based on machine learning, image data processing and image similarity calculation, which are recommended by the data set with the similarity of the user's face, which is more pertinent, and can
solve the current problem which is difficult to personalized recommendation for the
user's hair. The implementation of the hairstyle recommendation system can provide a
quick hairstyle choice for female users.

This paper is organized as follow: Section 1 introduces the brief information and
Section 2 presents the related work. Section 3 provides the detailed hairstyle recom-
mendation design. Section 4 describes the implementation for hairstyle recommenda-
tion and Section 5 is the results analysis, and Section 6 concludes the entire work.

## 2 Related Work

A perfect hairstyle could enhance anyone's self-confidence, especially women. How-
ever, in order to choose a good hairstyle, one was limited to rely on knowledge of a
beauty expert. Wisuwat Sunhem, et.al presented a hairstyle recommendation system
for women based on hairstyle experts' knowledge and a face shape classification
scheme [2]. The system classified the user's face shape into five categories which
were suitable for hairstyle recommendation using Support Vector Machine algorithms.
The hairstyle e rules were based on beauty experts' suggestions. The system is based
on the fine grained similarity of face shape. However, human face shape change over
time. For example, getting fat due to higher work pressure can change the shape of a
person's face. Therefore, our focus is to design and implement a coarse-grain similari-
ty of face shape for hairstyle recommendation.

Everyone has different understanding of the concept of beauty. It is difficult to de-
fine most of the things that most people think of beauty. However, at this stage, there
are many media widely selected beauty. The result of the selection makes people have
a certain grasp of the fashion trend. Because of the social effect, women will choose
the hair, makeup, dress and so on, which makes the hairstyle recommendation have
the standard of beauty. Therefore, how to define and verify the beauty is also our
important work in the hairstyle recommendation.

## 3 Proposed Hairstyle Recommendation System

### 3.1 Overall framework

The three parts of the design of the hairstyle recommendation system based on ma-
chine learning are functional requirement analysis, module analysis and algorithm
design analysis. Functional requirement analysis mainly explains the modules needed
by the system and the functions that each module needs to achieve. Module analysis is
also the structural division of the system. At the present stage, data acquisition, data
updating, face detection, recommendation processing and recommendation results
display are divided into three parts. Algorithm analysis is mainly about the content of
image similarity.

The overall design idea of the system is shown in Figure.1.User function modules
include registration, login, password modification, uploading and saving pictures, face
detection, hairstyle data set update, user view of recommended hairstyle results and
data collection material display.
<figure><img src="image_4.png" /><figcaption>**Figure. 1.** Overall framework map</figcaption></figure>

**3.2 Data collection and pre-handling**

Although people can feel beauty and recognize beauty, there are many different answers to the question of "what is beauty". Beauty is a subjective feeling. If something is in line with people's aesthetic sense, then it is said that this thing is beautiful. Similarly, the beauty of the world elected annually is most commonly recognized as "beauty". Beauty is the effect produced by all things. A person is beautiful, and we can think that the hairstyle that she owns is beautiful. Therefore, the 2013-2017 years of the TC Candle website [7] recommended by the hairstyle recommender system is selected as a dataset. The acquisition of data sets adopts the way of intercepting and saving video frames in a fixed time interval. Female users recommend different data sets according to their favorite trends. For example, some people like retro wind and some like new trends. The advantage of the system data set design is to provide users with more varied choices of hairstyles.

The data sets are used for face detection, and the face part is detected and preserved. This step completes the image preprocessing operation, and the final recommendation results are based on the similarity ratio between the user's face and the beautiful face of the data set. Figure 2 shows the process of image processing.

<figure><img src="image_5.png" /><figcaption>**Figure. 2.** pHash algorithm flow chart</figcaption></figure>

**3.3 Perceptual hash based recommendation**

When most pictures are converted to frequency domain, their coefficients are basically 0, which is determined by the redundancy and relevance of pictures. The key technique for the search of similar pictures in Google or Baidu's map function is called "perceptual Hash algorithm"[3]. The function is to generate a "fingerprint" string for
each picture, and to compare the similarity of the picture by comparing the finger-
prints produced by different pictures. The common perceptual hashing algorithm is
divided into low frequency perceptual hashing algorithm, dHash algorithm and pHash
algorithm. The pHash algorithm is used in this system.

(1)pHash algorithm.

Although the mean hash algorithm based on low frequency is simple, there are many
factors that can affect the mean value, and the reliability is reduced. In each step of
perceptual hash algorithm, histogram equalization is needed, which will affect the
mean size and affect the hash value. Then, the enhanced version of pHash algorithm
can solve this problem well, and get low-frequency components with DCT transform.
The working process of the pHash algorithm is shown in Figure.3.

* Reduce the size: use small pictures, but at least more than 8*8, where 32*32 is the best size. Reducing the size of the picture simplifies the calculation of DCT.

* Simplify the color: transform the image into grayscale image, discard the color factors of the image, and further simplify the computation.

* Calculate DCT: DCT transform and get the DCT coefficient matrix of 32*32.

* Reduce DCT: just keep the 8*8 matrix in the upper left corner, because this part presents the lowest frequency in the picture.

* Calculate the mean value: the mean of DCT is calculated.

* Calculate the hash value: if the DCT value of the pixel is greater than or equal to the DCT mean, then it is set to "1", and the less than DCT mean is set to "0". In this way, a 64 bit binary number, the hash value of a picture, is formed by comparison.

<figure><img src="image_6.png" /><figcaption>Figure. 3. pHash algorithm flow chart</figcaption></figure>
When most pictures are converted to frequency domain, their coefficients are basically 0, which is determined by the redundancy and relevance of pictures. The more similar the pictures' hash value is, the more similar the picture is. The system uses the perceptual Hash algorithm to find the largest image of the person's face in a number of data sets and recommends it. In the perceptual Hash algorithm, the pHash algorithm is improved on the original low frequency perception Hash algorithm. In summary, the system uses the pHash algorithm to convert the data to a hash value.

## (2) Similarity algorithm

The core of recommendation algorithm is to measure the similarity of faces between users and data sets. Here we use the pHash algorithm in perceptual hashing algorithm to get hash value. Next, we need to compare the similarity of hash values produced by different images. The following is the image Hash similarity measure method. Let S₁, S₂ be a hash sequence of length N, where S₁ (i) and S₂ (i) are the i elements in S₁ and S₂ sequences, respectively, D (S₁, S₂) is a similar measure of hash S₁ and S₂.

### 1) Hamming distance.

Hamming distance, also called Hing distance, requires hash sequence as a 0/1 form. The method of measuring the difference is by comparing the number of different values of the corresponding position 0/1 of the two sequences. The size of the Hamming distance is proportional to the size of the corresponding image difference, that is, the larger the value is, the larger the image difference is, the more novel the value is, the more novel a map is. The smaller the difference in content is [12]. The formula of Hamming distance described in equation (1):

$$D(s_1, s_2) = \sum |s_1(i) - s_2(i)| \quad (1)$$

### 2) Correlation coefficient

Correlation coefficient is used to distinguish the linear degree between the hash sequences obtained by the image. The calculation formula of correlation coefficient is equation (2). The larger value represents the higher similarity.

$$D(s_1, s_2) = \frac{C(s_1, s_2)}{\sqrt{V(s_1)} \sqrt{V(s_2)}} \quad (2)$$

### 3) Euclidean distance

Euclidean distance, also known as Euclidean distance, measures the absolute distance between two hash sequences. The Euclidean distance of the two images of hash S1 and S2 is as follows in equation(3):

$$D(s_1, s_2) = \sqrt{\sum_{i=1}^{N} [s_1(i) - s_2(i)]^2} \quad (3)$$
Generally, the smaller the value, the more similar between the two images; on the contrary, the greater the value, the greater the difference in the image, but the Euclidean distance measurement has a very obvious disadvantage, which is influenced by the scale of different units of the index.

### 4) Norm

The norm is also the Minkowski distance. The norm between the two image hash sequences is defined as follows in equation (4), where P is a parameter.

$$D(s_1, s_2) = \left( \sum_{i=1}^{N} [s_1(i) - s_2(i)]^p \right)^{\frac{1}{p}} \quad (4)$$

### 5) Angle cosine

The angle cosine method compares the degree of difference in the two vector directions, takes the Hash sequence as a vector, and uses the angle cosine method to make the qualitative measure. The definition of the angle cosine is shown in equation (5):

$$D(s_1, s_2) = \cos \theta = \sqrt{\frac{\left(\mathbf{s}_1^T \mathbf{s}_2\right)^2}{\mathbf{s}_1^T \mathbf{s}_1 \mathbf{s}_2^T \mathbf{s}_2}} \quad (5)$$

The angle of the angle is from 0 to 90 degrees. The bigger the angle cosine value is, the greater the difference is.

In the process of similarity measurement, pHash chooses Hamming distance[4]. The advantage is that the algorithm runs fast and ignores image scaling. Therefore, Hairstyle recommendation system adopts Hamming distance to measure similarity due to its simple idea and high efficiency.

## 3.4 Haar feature classified

The program uses the haar feature classifier[5] in OpenCV to realize the face detection function. OpenCV provides a set of object detection function. After training, it can detect any object you need. The library has many test parameters that can be directly used for many scenes, such as face, eyes, mouth, body, upper body, lower body and smiling face. The detection engine consists of a very simple detector cascade. These detectors are called Haar feature detectors, each of which has different scales and weights. In the training phase, the decision tree is optimized by known correct and wrong pictures. After initializing the face detector with the required parameters, it can be used for face detection. In the detection process, the trained classifier traverses each pixel of the input image with different scales to detect faces of different sizes.

# 4 Implementation

## 4.1 Programming language

The system uses the C++ language[6] to develop, the hair recommendation system mainly uses the image processing technology. Because of the large amount of data,
the time consumption of image processing is more than that of the general algorithm. Then, it is very important to improve the processing efficiency for improving the time consumption. The C++ language has the following advantages: 1)high flexibility. For example, C++ language uses pointer and pointer to realize dynamic memory allocation. 2) the rate of memory utilization is improved. There is a limit to the memory of the computer. Large quantities of images take up a lot of memory resources. However, the C++ language can achieve direct allocation and release of memory, thus improving the memory utilization. 3)it has rapid execution efficiency. The compiled language generated by the C++ language is the executable code. It can run directly on the processor.

## 4.2 Critical persuade code for hairstyle recommendation

As Figure4 shows, the hairstyle recommendation module recommends the corresponding hairstyle pictures based on the name of the input user picture, and the fifth line pseudo code calls the CalHashValue function to transform the picture into a string by using the pHash algorithm. In line 3 to line7, pseudo code realizes the Hamming distance value of the string corresponding to the user picture and the string corresponding to each picture in the dataset. The operation of pseudo code in line 8 to line 13 is to find out the smallest picture with Hamming distance. The corresponding picture is finally displayed.

<figure><img src="image_7.png" /><figcaption>**Figure. 4.** Critical persuade code for hairstyle recommendation</figcaption></figure>
# 5 Results and Analysis

The system is composed of six main parts. The system login interface, as shown in Figure.5(a), is successfully entered into the main interface of the system through the login system after the login account. In the main interface shown in Figure.5(b), each function module and the interface to add and save the picture are included. The face detection interface realizes the recognition and preservation of the face parts in the picture, and lists the number of the detected faces at the bottom of the interface and the detection time, as shown in Figure.5(c).Figure.5(d) shows the hairstyle recommendation interface. The user enters the picture name, and the system will show the recommended hairstyle pictures. The content of the dataset is displayed in the software material display interface, which is similar to the video player interface, as shown in Figure.5(e). The software update interface, as shown in Figure.5(f), implements the updated hairstyle data set, which includes 2013-2017 years of data.

<figure><img src="image_8.png" /><figcaption>**Figure. 5.** System page (a) Login interface;(b) Main interface;(c) Face detection interface;(d) Hairstyle recommendation interface;(e) Software material display interface;(f) Software update interface</figcaption></figure>

After the completion of the system design and the test work, in order to understand the satisfaction of the user to the results of the recommendation, an online questionnaire is set up. The content of the questionnaire is to give a user picture as a topic, and let the investigators anonymous to the user's hairstyle recommended by the hairstyle recommendation system (in this case, the recommended hairstyle 1) and the other two hairstyle pictures (recommended hairstyle 2 and recommended hairstyle 3) are sorted. After the online questionnaire was released, a total of 352 questionnaires were collected. The wide range of respondents has enhanced the credibility of the survey results. After recommending the system to test the user's picture, the recommended hairstyle is recommended hairstyle 1.The average comprehensive score of the options is calculated automatically according to the sorting of all the candidates. It reflects the comprehensive ranking of the options, and the higher the score is, the better the comprehensive ranking is. The calculation method is as follows in eq (6):
$$ \text{Averagescore} = \frac{\sum (\text{frequency} * \text{weight})}{\text{Number}} \qquad (6) $$

The variable Number refers to the number of people filling in the questionnaire and the weight is determined by the position of the option being arranged. For example, there are 3 options for sorting, the weight in the first place is 3, the second position is 2, and the third position is 1. The results of the statistical analysis of the questionnaire are shown in Figure.6(a) and the ratio of the recommended hairstyle 1 to the first is shown in Figure.6(b). From the graph, we can see that the user satisfaction of hairstyle recommendation system recommended is high, which proves that the hairstyle recommendation system has great application prospects. Considering the disagreement between people in different regions, the survey of online questionnaires is widely distributed, accounting for 88.6% of China's region, as shown in Figure.6(c).

<figure><img src="image_9.png" /><figcaption>**Figure. 6.** Survey results of user satisfaction. (a)Comprehensive ranking results;(b) Recommended hairstyle for the first place;(c) Geographic distribution map of the respondents.</figcaption></figure>
# 6 Conclusion

The hairstyle recommendation system recommends is based on the face coarse-grain global similarity, the benefit lies in ignoring the external influence factors of the face change, and can provide the hair recommendation reference for the fashionable female users quickly and effectively. Based on a lot of survey work and analysis, this paper has stipulated the standard of hairstyle beauty. The dataset is updatable and scalable, so that users can choose different data sets according to their preferences. The user uploads the picture to be recommended. Then, face detection module keeps only the valid face information of the user's picture. Finally, the system compares the face part of the user picture with the data set, and draws the most similar face, and recommends its corresponding hairstyle. In addition, user satisfaction surveys were conducted on the results of the system hairstyle recommendation, and the user satisfaction rate of the recommended hairstyle was 23.4% higher than the average satisfaction.

# Acknowledgement

The work is granted by National Natural Science Foundation of China, numbered 61502298 and Shanghai Maritime University Innovative Program Grant.

# References

1. Xiaoying Wang,Chengliang Wang,Recommendation system of e-commerce based on improved collaborative filtering algorithm,IEEE,2017,332-335

2. Wisuwat Sunhem,Kitsuchart Pasupa,Piyakorn Jansiripitikul,Hairstyle recommendation system for women,IEEE,2016,166-169

3. Xin Liu,Qian Zhang,RuPeng Luan,Feng Yu,Applications of perceptual hash algorithm in agriculture images,IEEE,2013,698-702

4. Haifeng Hu,Liang Zhang,Jianshen Wu,Hamming distance based approximate similarity text search algorithm,IEEE,2015,1-6

5. Ali Sharifara,Mohd Shafry Mohd Rahim,Yasaman Anisi,A general review of human face detection including a study of neural networks and Haar feature-based cascade classifier in face detection,IEEE,2014,73-78

6. Pamela Bhattacharya,Iulian Neamtiu,Assessing programming language impact on development and maintenance:a study on C and C++,IEEE,2011,171-18

7. http://independentcritics.com/