clustering_images.py
661 Bytes
from PIL import Image, ImageDraw
import hcluster
import numpy as np
import os
from scipy.cluster.vq import *
import matplotlib.pyplot as plt
path = 'mel_spectogram_cut/barking'
#path = '/home/compu/ksw/dog_project/mel_spectogram_cut/barking'
imlist = [os.path.join(path, f) for f in os.listdir(path) if f.endswith('.png')]
features = np.zeros([len(imlist), 512])
for i, f in enumerate(imlist):
im = np.array(Image.open(f))
h, edge = np.histogramdd(im.reshape(-1,3), 8, normed=True,
range=[(0,255),(0,255),(0,255)])
features[i] = h.flatten()
if i%100 == 0:
print(i)
tree = hcluster.hcluster(features)
tree1 = hcluster.draw_dendrogram(tree, imlist)