Search code examples
opencvimage-processingsvmscikit-learnlibsvm

Using SVM after feature extraction for Nudity Detection Algorithm


Nudity Detection Algorithm

  • normalization
  • Zoning
  • Feature Extraction
  • Classification using SVM

Following methods are use

1.Normalization: First image is converted into .jpg format and size of 256X256.Then it is converted into YCbCr color space,for this i use OpenCV python.Here is the code.

2.Zoning: Normalized Images are then divided into three zones.It is because assumption is "Nudity of images are found mostly in Central zone".

3.Feature Extraction: In this module Image is in YCbCr,Skin pixels are filtered by thresholding in range (0,133,77),(255,173,127) and divided into three zones.and then for each zones features are calculated 2 color features(number of connected skin pixels and proportion of skin pixel to total pixel) and 2 texture features(Homogeneity and correlation).texture features are calculated using glcm (skimage.features module).here is the code

import os
import numpy as np
import cv2
from cv2 import cv
import skimage.feature as sf
total_pixels=256.0*256.0

class normalize:
    def __init__(self,src,dst):
        self.src=src
        self.dst=dst+"_1.jpg"
    def resize(self):
        x,y=256,256
        src=cv2.imread(self.src,1)
        src=cv2.resize(src,(x,y))
        cv2.imwrite(self.dst,src)
        dst=cv2.imread(self.dst,1)
        return dst
"""Segmentation module is used to segment out skin pixels in YCrCb color space"""

def segmentation(src):
    img=src.copy()
    img=cv2.cvtColor(src,cv.CV_BGR2YCrCb)
    dst=cv2.inRange(img,(0,133,77),(255,173,127))
    return dst

"""Image Zoning and feature extraction module"""

class features:
    def __init__(self,src):
        self.zone1=src
        self.zone2=src[30:226,30:226]
        self.zone3=src[60:196,60:196]

    def createglcm(self,zone):
        return sf.greycomatrix(zone,[1],[0,np.pi/4,np.pi/2,-np.pi/2,-np.pi/4,np.pi*25/12],normed=True)

    def getCorrelation(self,glcm):
        return sf.greycoprops(glcm,'correlation')

    def getHomogeneity(self,glcm):
        return sf.greycoprops(glcm,'homogeneity')

    def getcolorfeatures(self,zone):
        contours, hierarchy = cv2.findContours(zone,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

        skin_pixel_connected=0 

        for i in range(len(contours)):
            skin_pixel_connected=skin_pixel_connected+cv2.contourArea(contours[i])

        return [skin_pixel_connected,skin_pixel_connected/total_pixels]

Now i have retrieved a list of various features as given in code. How to make feature vector for svm from python lists.How to use SVM for training by using nude and non-nude image(i have 5000 images) and then for detection.? Can any body suggest me.


Solution

  • After cross-validation strategy,C=100.00 and gamma=0.07

    this is what my code looks:

    from sklearn.svm import SVC
    classifier=SVC(kernel='rbf',C=100.0,gamma=0.07,cache_size=800)
    classifier.fit(np.array(featurespace),np.array(classes))
    classifier.predict(X_test)