Ask Your Question
0

passing parameters to cv2.kmeans in python

asked 2016-11-03 04:39:42 -0500

irum gravatar image

updated 2016-11-03 05:47:08 -0500

i have a dataset of images and i want to do clustering on it. I have read the openCV documentations of kmeans but i just do not get it properly. Below is my code and i have no idea how can i pass images to the kmeans() and how to send all the clusters into different folders.

import cv2, sys, numpy, os
import matplotlib.pyplot as plt
from matplotlib import style
style.use("ggplot")
from sklearn.cluster import KMeans


fn_dir2 = 'unknown'
path2='/home/irum/Desktop/Face-Recognition/thakarrecog/unknown/UNKNOWNS'

# Create a list of images and a list of corresponding names
(images, lables, names, id) = ([], [], {}, 0)

#reading images from dataset
for (subdirs, dirs, files) in os.walk(fn_dir2):
    for subdir in dirs:
        names[id] = subdir 
        subjectpath = os.path.join(fn_dir2, subdir) 
        for filename in os.listdir(subjectpath):
            path = subjectpath + '/' + filename
            lable = id
            images.append(cv2.imread(path, 0))
            lables.append(int(lable))

        id += 1
#converting images and lables to numpy arrays 
(images, lables) = [numpy.array(lis) for lis in [images, lables]]
print "length images", len(images)
print type(images)
print "length lables", len(lables)

criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)

images = np.asarray(images, np.float32)
N = len(images)
images = images.reshape(N,-1)
ret,label,center=cv2.kmeans(images,k,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)

now i am having this error Traceback (most recent call last): File "Kmeans2.py", line 39, in <module> ret,label,center=cv2.kmeans(images,2,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS) TypeError: an integer is required

edit retag flag offensive close merge delete

Comments

what is the purpose of the clustering ? (in the context of face-recognition ?)

berak gravatar imageberak ( 2016-11-03 04:46:12 -0500 )edit

i will use this clustered data for face recognition .... basically i am doing clustering for face-recognition

irum gravatar imageirum ( 2016-11-03 04:54:33 -0500 )edit

again, why ? you're trying to solve some problem there, - which is ?

berak gravatar imageberak ( 2016-11-03 04:59:45 -0500 )edit
1

i am doing detection and recognition , any face which is detected and not recognized is saved in a dataset. after a day my algorithm start doing clustering on that dataset in order to put them in a database of known people and each cluster will have a specific name after that. So when i tried doing clustering using open cv KMeans i am having different errors. Because i think i am passing wrong samples MAY BE. Problem is that i am asking how can i pass the images numpy array to this cv2.KMeans function ?

irum gravatar imageirum ( 2016-11-03 05:15:31 -0500 )edit

(images, lables) = [numpy.array(lis) for lis in [images, lables]] these are two numpy arrays, one has faces and other has lables. i need to cluster image data, i want to differentiate one face from another so i can get same faces in one cluster.

irum gravatar imageirum ( 2016-11-03 05:22:13 -0500 )edit

yes determining k is also another problem so i thought for now i can use k=10 because i have not that much different people in dataset. secondly if there is any other technique that can be more useful i will be very happy to try.

irum gravatar imageirum ( 2016-11-03 05:49:22 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-11-03 05:29:35 -0500

berak gravatar image

updated 2016-11-03 05:35:30 -0500

to apply cv2.kmeans, you need a single, 2d matrix, where each row is a flattened 1d image, and convert to float:

images = np.asarray(images, np.float32)
N = len(images)
images = images.reshape(N,-1)

also: https://github.com/opencv/opencv/blob...

edit flag offensive delete link more

Comments

i am having this error now. you can see the code above i have edited the code. Traceback (most recent call last): File "Kmeans2.py", line 39, in <module> ret,label,center=cv2.kmeans(images,2,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS) TypeError: an integer is required

irum gravatar imageirum ( 2016-11-03 05:46:35 -0500 )edit

i'm getting same error with opencv2.4/python2.7 but not with opencv3.1/python3.5 for 2.4 it seems to be:

>>> help(cv2.kmeans)
Help on built-in function kmeans in module cv2:

kmeans(...)
    kmeans(data, K, criteria, attempts, flags[, bestLabels[, centers]]) -> retval, bestLabels, centers

so, try to remove the None param

berak gravatar imageberak ( 2016-11-03 05:52:33 -0500 )edit

i solved the problem by using sklearn.cluster.KMeans it is making clusters now. I used the piece of code in you answer and then just passed the images to KMeans. `#Convert Images to Float32 images = np.asarray(images, np.float32) N = len(images) images = images.reshape(N,-1)

using kmeans clustring having 5 clusters

kmeans = KMeans(n_clusters=5)

passing images to kmeans

kmeans.fit(images)

centroids = kmeans.cluster_centers_ labels = kmeans.labels_ ` its working... Thank you for the help.

irum gravatar imageirum ( 2016-11-04 05:33:46 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-11-03 04:39:42 -0500

Seen: 780 times

Last updated: Nov 03 '16