[PYTHON] I tried to sort out the objects from the image of the steak set meal-④ Clustering

Introduction

Continuing from I tried to sort out objects from steak set meal images-similar image detection, this time I tried clustering the images compared with the histogram. It was.

Source code

grouping_image.py


image_dir = "{Directory path}"
    target_files = os.listdir(image_dir)
    files = os.listdir(image_dir)

    group_images = {}

    for target_file in target_files:
        if target_file == '.DS_Store':
            continue

        #Load the image file.
        target_image_path = image_dir + target_file
        target_image = cv2.imread(target_image_path)

        #Generate histogram data.
        target_hist = cv2.calcHist([target_image], [0], None, [256], [0, 256])

        similarity_images = {}

        for file in files:
            if file == '.DS_Store' or file == target_file:
                continue

            #Load the image file.
            comparing_image_path = image_dir + file
            comparing_image = cv2.imread(comparing_image_path)

            #Generate histogram data.
            comparing_hist = cv2.calcHist([comparing_image], [0], None, [256], [0, 256])

            #Histogram comparison is performed to calculate the similarity of images.
            ret = cv2.compareHist(target_hist, comparing_hist, 0)

            #Converts the result to probability notation.
            probability = ret * 100

            #Outputs the similarity.
            # print("target file: " + target_file, "file: " + file, "similarity: " + str(probability) + "%")

            #Similarity is 90%Group only the above.
            if probability > 90:
                if len(target_file) not in group_images:
                    group_images[target_file] = {}

                similarity_images[len(similarity_images)] = file
                group_images[target_file].update(similarity_images)

    #Group similar images.
    similarity_group = {}

    for target_images in group_images:
        similarity_image = {}
        count = 0

        #Check for images that are already grouped.
        if is_distinct_image(similarity_group, target_images):
            for similarity_images in group_images:
                if target_images != similarity_images:
                    distinct_images = {v:k for k, v in group_images[similarity_images].items()}

                    if target_images in distinct_images:
                        similarity_image[count] = similarity_images
                        count = count + 1

                        if target_images not in similarity_group:
                            similarity_group[target_images] = {}

                        similarity_group[target_images].update(similarity_image)

    #Outputs the grouping result.
    for file in similarity_group:
        print file, similarity_group[file]

#If it is grouped, False will be returned.
def is_distinct_image(similarity_group, target_images):
    for images in similarity_group:
        distinct_images = {v:k for k, v in similarity_group[images].items()}

        if target_images in distinct_images:
            return False

    return True

result

634.jpg {0: '632.jpg', 1: '657.jpg'}
575.jpg {0: '603.jpg', 1: '581.jpg', 2: '659.jpg', 3: '758.jpg', 4: '754.jpg', 5: '629.jpg'}
932.jpg {0: '799.jpg', 1: '659.jpg', 2: '815.jpg', 3: '492.jpg', 4: '921.jpg', 5: '658.jpg', 6: '920.jpg', 7: '974.jpg', 8: '629.jpg', 9: '1018.jpg', 10: '806.jpg', 11: '972.jpg'}
547.jpg {0: '559.jpg', 1: '463.jpg'}
480.jpg {0: '432.jpg'}
389.jpg {0: '432.jpg', 1: '250.jpg', 2: '369.jpg'}

Details

It was classified into 6 types. The following two groups were divided into those that were successful and wanted to take.

Sauce group

634.jpg 634.jpg

632.jpg 632.jpg

Rice group

575.jpg 575.jpg

603.jpg 603.jpg

Impressions

In addition, pickles, miso soup, and condiments have been made difficult. Perhaps the reason is that there is an image of the entire yakiniku set meal being transferred, and I think that it is being pulled by it.

All page links

-I tried object detection using Python and OpenCV -I tried to sort out objects from the image of steak set meal-① Object detection -I tried to sort out the objects from the image of the steak set meal-② Overlap number sorting -I tried to sort out the objects from the image of the steak set meal-③ Similar image heat map detection -I tried to sort out the objects from the image of the steak set meal-④ Clustering -I tried to sort out objects from the image of steak set meal-⑤ Similar image feature point detection edition

Recommended Posts

I tried to sort out the objects from the image of the steak set meal-④ Clustering
I tried to sort out the objects from the image of the steak set meal-① Object detection
I tried to sort out the objects from the image of the steak set meal-② Overlap number sorting
I tried to sort out the objects from the image of the steak set meal-⑤ Similar image feature point detection
I tried to cut out a still image from the video
I tried to correct the keystone of the image
I tried to detect the iris from the camera image
I tried to find the entropy of the image with python
I tried to transform the face image using sparse_image_warp of TensorFlow Addons
I tried to touch the API of ebay
I tried using the image filter of OpenCV
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!
I tried to automate the face hiding work of the coordination image for wear
I tried to predict the number of people infected with coronavirus in consideration of the effect of refraining from going out
I tried to summarize the basic form of GPLVM
I tried to visualize the spacha information of VTuber
I tried to erase the negative part of Meros
I tried to classify the voices of voice actors
I tried to compress the image using machine learning
I tried to summarize the string operations of Python
I tried to put out the frequent word ranking of LINE talk with Python
I tried to extract the text in the image file using Tesseract of the OCR engine
I tried to find out the outline about Big Gorilla
I tried "gamma correction" of the image with Python + OpenCV
I tried to get the location information of Odakyu Bus
I tried to find the average of the sequence with TensorFlow
[Python] I tried to visualize the follow relationship of Twitter
[Machine learning] I tried to summarize the theory of Adaboost
I tried to fight the Local Minimum of Goldstein-Price Function
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
I tried to get various information from the codeforces API
I tried to predict the genre of music from the song title on the Recurrent Neural Network
Implementation of recommendation system ~ I tried to find the similarity from the outline of the movie using TF-IDF ~
I tried using PI Fu to generate a 3D model of a person from one image
I tried to find the trend of the number of ships in Tokyo Bay from satellite images.
[Linux] I tried to summarize the command of resource confirmation system
I tried to get the index of the list using the enumerate function
I tried to automate the watering of the planter with Raspberry Pi
[Python] Try to graph from the image of Ring Fit [OCR]
I tried to process the image in "sketch style" with OpenCV
I tried to implement hierarchical clustering
I tried to process the image in "pencil style" with OpenCV
I tried to expand the size of the logical volume with LVM
I tried to summarize the frequently used implementation method of pytest-mock
I tried to improve the efficiency of daily work with Python
I tried to visualize the common condition of VTuber channel viewers
I tried to move the ball
I tried to estimate the interval.
[Python] I tried to reproduce the emergency escape program to return from the world to return from the modified world of "The disappearance of Haruhi Suzumiya"
I tried to make a thumbnail image of the best avoidance flag-chan! With RGB values ​​[Histogram] [Visualization]
I tried to deliver mail from Node.js and Python using the mail delivery service (SendGrid) of IBM Cloud!
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.
I tried to execute SQL from the local environment using Looker SDK
I tried moving the image to the specified folder by right-clicking and left-clicking
I tried to visualize the age group and rate distribution of Atcoder
I tried transcribing the news of the example business integration to Amazon Transcribe
I tried calling the prediction API of the machine learning model from WordPress
zoom I tried to quantify the degree of excitement of the story at the meeting
I tried to estimate the similarity of the question intent using gensim's Doc2Vec
I tried how to improve the accuracy of my own Neural Network