[
  {
    "path": ".github/FUNDING.yml",
    "content": "# These are supported funding model platforms\n\ngithub: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]\npatreon: # Replace with a single Patreon username\nopen_collective: # Replace with a single Open Collective username\nko_fi: # Replace with a single Ko-fi username\ntidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel\ncommunity_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry\nliberapay: # Replace with a single Liberapay username\nissuehunt: # Replace with a single IssueHunt username\notechie: # Replace with a single Otechie username\ncustom: https://raw.githubusercontent.com/HansRen1024/HansRen1024.github.io/master/Get_paid_QR.jpg\n"
  },
  {
    "path": "10_EdgeBoxes+SVM+NMS_cam.py",
    "content": "#!/usr/bin/env python2\n# -*- coding: utf-8 -*-\n\"\"\"\nCreated on Tue Jul  4 15:10:57 2017\n\n@author: hans\n\"\"\"\n\nfrom imutils.object_detection import non_max_suppression\nfrom edge_boxes_python import edge_boxes_python\nimport numpy as np\nfrom skimage.feature import hog\nfrom sklearn.externals import joblib\nimport cv2\nimport os\nimport time\n\n\n\ndef rgb2gray(im):\n    gray = im[:, :, 0]*0.2989+im[:, :, 1]*0.5870+im[:, :, 2]*0.1140\n    return gray\n\ndef getFeat(data):\n    normalize = True\n    visualize = False\n    block_norm = 'L2-Hys'\n    cells_per_block = [2,2]\n    pixels_per_cell = [20,20]\n    orientations = 9\n    gray = rgb2gray(data)/255.0\n    fd = hog(gray, orientations, pixels_per_cell, cells_per_block, block_norm, visualize, normalize)\n    return fd\n        \nif __name__ == \"__main__\":\n    model_path = './models/svm_pso_less_hnm_50.model'\n    clf = joblib.load(model_path)\n    c = cv2.VideoCapture(0)\n    num = 0\n    while 1:\n        t0 = time.time()\n        num += 1\n        ret, image = c.read()\n        rects = []\n        eb = edge_boxes_python(os.path.expanduser('~') + '/HoG_SVM/cup/sf.dat')\n        bbs = eb.get_edge_boxes(image)\n        for (xmin, ymin, width, height, hb) in bbs[0:10]:\n            xmin = int(xmin)\n            ymin = int(ymin)\n            width = int(width)\n            height = int(height)\n            win = image[ymin:ymin + height, xmin:xmin + width]\n            window = cv2.resize(win,(200,200),interpolation=cv2.INTER_CUBIC)\n            win_fd = getFeat(window)\n            win_fd.shape = 1,-1\n            result = int(clf.predict(win_fd))\n            if result == 1:\n                rects.append([xmin, ymin, xmin + width, ymin + height])\n        if len(rects) != 0:\n            rects = np.array(rects)\n            pick = non_max_suppression(rects, probs=None, overlapThresh=0.1)\n            for (xA, yA, xB, yB) in pick:\n                cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2)\n        font=cv2.FONT_HERSHEY_SIMPLEX\n        t1 = time.time()\n        cv2.putText(image,'%.2f' %(1/(t1-t0)),(0,30),font,0.9,(255,255,255),2)\n        cv2.imshow(\"After NMS\", image)\n        cv2.waitKey(1)\n"
  },
  {
    "path": "1_HoG_extract_feature.py",
    "content": "#!/usr/bin/env python2  \n# -*- coding: utf-8 -*-  \n\"\"\" \nCreated on Tue Jun 13 10:24:50 2017 \n \n@author: hans \n\"\"\"  \n  \nfrom skimage.feature import hog  \nfrom sklearn.externals import joblib  \nimport xml.dom.minidom as xdm  \nimport numpy as np  \nimport Image  \nimport cv2  \nimport os  \nimport time  \n  \n# define parameter  \nnormalize = True  \nvisualize = False  \nblock_norm = 'L2-Hys'  \ncells_per_block = [2,2]  \npixels_per_cell = [20,20]  \norientations = 9  \n  \n# xml path  \ntrain_xml_filePath = r'./train/Annotation'  \n  \n  \ndef getBox(childDir):  \n    f_xml = os.path.join(train_xml_filePath, '%s.xml' %childDir.split('.')[0]) # organise path  \n    xml = xdm.parse(f_xml) # load xml file  \n    filename = xml.getElementsByTagName('filename')   \n    filename = filename[0].firstChild.data.encode(\"utf-8\") # read file name  \n    xmin = xml.getElementsByTagName('xmin') # coordinate of top left pixel\n    xmin = int(xmin[0].firstChild.data)  \n    ymin = xml.getElementsByTagName('ymin')  \n    ymin = int(ymin[0].firstChild.data)  \n    xmax = xml.getElementsByTagName('xmax') # coordinate of down right pixel \n    xmax = int(xmax[0].firstChild.data)  \n    ymax = xml.getElementsByTagName('ymax')  \n    ymax = int(ymax[0].firstChild.data)  \n    box = (xmin,ymin,xmax,ymax)   \n    return box  \n  \ndef getDataWithCrop(filePath,label):  \n    Data = []  \n    num = 0  \n    for childDir in os.listdir(filePath):  \n        f_im = os.path.join(filePath, childDir)  \n        image = Image.open(f_im) # open the image  \n        box = getBox(childDir)  \n        region = image.crop(box) # cut off image\n        data = np.asarray(region) # put the data of image into an N-dinimeter array  \n        data = cv2.resize(data,(200,200),interpolation=cv2.INTER_CUBIC) # resize image  \n        data = np.reshape(data, (200*200,3))   \n        data.shape = 1,3,-1  \n        fileName = np.array([[childDir]])  \n        datalebels = zip(data, label, fileName) # organise data  \n        Data.extend(datalebels) # pou the organised data into a list\n        num += 1  \n        print \"%d processing: %s\" %(num,childDir)  \n    return Data,num  \n  \ndef getData(filePath,label): # get the full image without cutting  \n    Data = []  \n    num = 0  \n    for childDir in os.listdir(filePath):  \n        f = os.path.join(filePath, childDir)  \n        data = cv2.imread(f)  \n        data = cv2.resize(data,(200,200),interpolation=cv2.INTER_CUBIC)  \n        data = np.reshape(data, (200 * 200,3))  \n        data.shape = 1,3,-1  \n        fileName = np.array([[childDir]])  \n        datalebels = zip(data, label, fileName)  \n        Data.extend(datalebels)  \n        num += 1  \n        print \"%d processing: %s\" %(num,childDir)  \n    return Data,num  \n  \ndef getFeat(Data,mode): # get and save feature valuve\n    num = 0  \n    for data in Data:  \n        image = np.reshape(data[0], (200, 200, 3)) \n        gray = rgb2gray(image)/255.0 # trans image to gray\n        fd = hog(gray, orientations, pixels_per_cell, cells_per_block, block_norm, visualize)\n#         fd = hog(gray, orientations, pixels_per_cell, cells_per_block, block_norm, visualize, normalize)  \n        fd = np.concatenate((fd, data[1])) # add label in the end of the array\n        filename = list(data[2])  \n        fd_name = filename[0].split('.')[0]+'.feat' # set file name  \n        if mode == 'train':  \n            fd_path = os.path.join('./features/train/', fd_name)  \n        else:  \n            fd_path = os.path.join('./features/test/', fd_name)  \n        joblib.dump(fd, fd_path,compress=3) # save data to local  \n        num += 1  \n        print \"%d saving: %s.\" %(num,fd_name)  \n  \ndef rgb2gray(im):  \n    gray = im[:, :, 0]*0.2989+im[:, :, 1]*0.5870+im[:, :, 2]*0.1140  \n    return gray  \n  \nif __name__ == '__main__':  \n    t0 = time.time()  \n      \n    # deal with Positive test dataset and trainset with cutting  \n    Ptrain_filePath = r'./train/positive'  \n    Ptest_filePath = r'./test/positive'  \n    PTrainData,P_train_num = getDataWithCrop(Ptrain_filePath,np.array([[1]]))  \n    getFeat(PTrainData,'train')  \n    PTestData,P_test_num = getData(Ptest_filePath,np.array([[1]]))  \n    getFeat(PTestData,'test')  \n      \n    # deal with positive trainset without cutting  \n    Pres_train_filePath = r'./train/positive_rest'  \n    PresTrainData,Pres_train_num = getData(Pres_train_filePath,np.array([[1]]))  \n    getFeat(PresTrainData,'train')  \n      \n    # deal with negative test dataset and train dataset without cutting  \n    Ntrain_filePath = r'./train/negative'  \n    Ntest_filePath = r'./test/negative'  \n    NTrainData,N_train_num = getData(Ntrain_filePath,np.array([[0]]))  \n    getFeat(NTrainData,'train')  \n    NTestData,N_test_num = getData(Ntest_filePath,np.array([[0]]))  \n    getFeat(NTestData,'test')  \n      \n    t1 = time.time()   \n    print \"------------------------------------------------\"  \n    print \"Train Positive: %d\" %(P_train_num + Pres_train_num)  \n    print \"Train Negative: %d\" %N_train_num  \n    print \"Train Total: %d\" %(P_train_num + Pres_train_num + N_train_num)  \n    print \"------------------------------------------------\"  \n    print \"Test Positive: %d\" %P_test_num  \n    print \"Test Negative: %d\" %N_test_num  \n    print \"Test Total: %d\" %(P_test_num+N_test_num)  \n    print \"------------------------------------------------\"  \nprint 'The cast of time is:%f'%(t1-t0)\n"
  },
  {
    "path": "2_Train_SVM.py",
    "content": "#!/usr/bin/env python2  \n# -*- coding: utf-8 -*-  \n\"\"\" \nCreated on Thu Jun 15 16:38:03 2017 \n \n@author: hans \n\"\"\"  \nimport sklearn.svm as ssv  \nfrom sklearn.externals import joblib  \nimport glob  \nimport os  \nimport time  \n  \nif __name__ == \"__main__\":  \n    model_path = './models/svm.model'  \n    train_feat_path = './features/train'  \n    fds = []  \n    labels = []  \n    num=0  \n    for feat_path in glob.glob(os.path.join(train_feat_path, '*.feat')):  \n        num += 1  \n        data = joblib.load(feat_path)  \n        fds.append(data[:-1])  \n        labels.append(data[-1])  \n        print \"%d Dealing with %s\" %(num,feat_path)  \n    t0 = time.time()  \n#------------------------SVM--------------------------------------------------  \n    clf = ssv.SVC(kernel='rbf') # for training initial model\n#     clf = ssv.SVC(kernel='rbf', C=17.255220940030252, gamma=1.2943653125547475e-06) # for training svm_pso.model(origin model)\n    print \"Training a SVM Classifier.\"  \n    clf.fit(fds, labels)  \n    joblib.dump(clf, model_path)\n#------------------------SVM--------------------------------------------------  \n    t1 = time.time()  \n    print \"Classifier saved to {}\".format(model_path)  \n    print 'The cast of time is :%f seconds' % (t1-t0)\n"
  },
  {
    "path": "3_Test_SVM.py",
    "content": "#!/usr/bin/env python2  \n# -*- coding: utf-8 -*-  \n\"\"\" \nCreated on Thu Jun 15 16:44:53 2017 \n \n@author: hans \n\"\"\"  \nfrom sklearn.externals import joblib  \nimport glob  \nimport os  \nimport time  \n  \nif __name__ == \"__main__\":  \n    model_path = './models/svm.model'  \n    test_feat_path = './features/test'  \n    total=0  \n    num=0  \n    t0 = time.time()  \n    clf = joblib.load(model_path)  \n    for feat_path in glob.glob(os.path.join(test_feat_path, '*.feat')):  \n        total += 1  \n        print \"%d processing: %s\" %(total, feat_path)  \n        data_test = joblib.load(feat_path)  \n        data_test_feat = data_test[:-1].reshape((1,-1))  \n        result = clf.predict(data_test_feat)  \n        if int(result) == int(data_test[-1]):  \n            num += 1  \n        rate = float(num)/total  \n        t1 = time.time()  \n    print 'The classification accuracy is %f' %rate  \n    print 'The cast of time is :%f seconds' % (t1-t0) \n"
  },
  {
    "path": "4_Train_PCA+SVM.py",
    "content": "#!/usr/bin/env python  \n#encoding:utf-8  \n  \n\"\"\" \nCreated on Thu Jun 15 17:29:22 2017 \n \n@author: hans \n\"\"\"  \n  \nimport numpy as np  \nimport sklearn.svm as ssv  \nfrom sklearn.externals import joblib  \nimport glob  \nimport os  \nimport time  \nm = '20pixel' # HoG choses pixel per cell will gain different number of feature values.  \ndef zeroMean(dataMat): # zero normalisation\n    meanVal=np.mean(dataMat,axis=0) # calculate mean value of every column.   \n    joblib.dump(meanVal,'./features/PCA/%s/meanVal_train_%s.mean' %(m,m)) # save mean value   \n    newData=dataMat-meanVal   \n    return newData,meanVal\ndef pca(dataMat,n):   \n    print \"Start to do PCA...\"   \n    t1 = time.time()   \n    newData,meanVal=zeroMean(dataMat)   \n    covMat=np.cov(newData,rowvar=0)   \n    eigVals,eigVects=np.linalg.eig(np.mat(covMat)) # calculate feature value and feature vector   \n    joblib.dump(eigVals,'./features/PCA/%s/eigVals_train_%s.eig' %(m,m),compress=3)    \n    joblib.dump(eigVects,'./features/PCA/%s/eigVects_train_%s.eig' %(m,m),compress=3)  \n    # eigVals = joblib.load('./features/PCA/%s/eigVals_train_%s.eig' %(m,m))  \n    # eigVects = joblib.load('./features/PCA/%s/eigVects_train_%s.eig' %(m,m))   \n    eigValIndice=np.argsort(eigVals) # sort feature value\n    n_eigValIndice=eigValIndice[-1:-(n+1):-1] # take n feature value   \n    n_eigVect=eigVects[:,n_eigValIndice] # take n feature vector \n    joblib.dump(n_eigVect,'./features/PCA/%s/n_eigVects_train_%s_%s.eig' %(m,m,n))    \n    lowDDataMat=newData*n_eigVect # calculate low dimention data\n    # reconMat=(lowDDataMat*n_eigVect.T)+meanVal   \n    t2 = time.time()   \n    print \"PCA takes %f seconds\" %(t2-t1)   \n    return lowDDataMat  \nif __name__ == \"__main__\":   \n    n = 100 # this is to define how dimentions u want   \n    model_path = './models/%s/svm_%s_pca_%s.model' %(m,m,n)   \n    train_feat_path = './features/train'   \n    fds = []   \n    labels = []   \n    num=0   \n    for feat_path in glob.glob(os.path.join(train_feat_path, '*.feat')):   \n        num += 1   \n        data = joblib.load(feat_path)   \n        fds.append(data[:-1])   \n        labels.append(data[-1])   \n        print \"%d Dealing with %s\" %(num,feat_path)  \n#------------------------PCA--------------------------------------------------   \n    fds = np.array(fds,dtype = int) #TODO, force to int format may damage the value to be all 0.\n    fds.shape = 2327,-1 # 2327 is the number of trainset  \n    fds= pca(fds,n)  \n#------------------------PCA--------------------------------------------------   \n    t0 = time.time()  \n#------------------------SVM--------------------------------------------------   \n    clf = ssv.SVC(kernel='rbf')   \n    print \"Training a SVM Classifier.\"   \n    clf.fit(fds, labels)   \n    joblib.dump(clf, model_path)  \n#------------------------SVM--------------------------------------------------   \n    t1 = time.time()   \n    print \"Classifier saved to {}\".format(model_path)   \n    print 'The cast of time is :%f seconds' % (t1-t0) \n"
  },
  {
    "path": "5_Test_PCA+SVM.py",
    "content": "#!/usr/bin/env python2  \n# -*- coding: utf-8 -*-  \n\"\"\" \nCreated on Thu Jun 15 17:29:22 2017 \n \n@author: hans \n\"\"\"  \n  \nfrom sklearn.externals import joblib  \nimport glob  \nimport os  \nimport time  \n  \nm = '20pixel'  \nn = 100  \n  \nif __name__ == \"__main__\":  \n    model_path = './models/%s/svm_%s_pca_%s.model' %(m,m,n)  \n    test_feat_path = './features/test'  \n    total=0  \n    num=0  \n    t0 = time.time()  \n    clf = joblib.load(model_path)  \n    for feat_path in glob.glob(os.path.join(test_feat_path, '*.feat')):  \n        total += 1  \n        print \"%d processing: %s\" %(total, feat_path)  \n        data_test = joblib.load(feat_path)  \n        data_test_feat = data_test[:-1].reshape((1,-1))  \n#------------------------PCA--------------------------------------------------  \n        meanVal = joblib.load('./features/PCA/%s/meanVal_train_%s.mean' %(m,m))  \n        data_test_feat = data_test_feat - meanVal \n        n_eigVects = joblib.load('./features/PCA/%s/n_eigVects_train_%s_%s.eig' %(m,m,n))  \n        data_test_feat = data_test_feat * n_eigVects \n#------------------------PCA--------------------------------------------------  \n        result = clf.predict(data_test_feat)  \n        if int(result) == int(data_test[-1]):  \n            num += 1  \n        rate = float(num)/total  \n        t1 = time.time()  \n    print 'The classification accuracy is %f' %rate  \n    print 'The cast of time is :%f seconds' % (t1-t0) \n"
  },
  {
    "path": "6_PSO+PCA.py",
    "content": "from sklearn.externals import joblib\nimport os\nfrom svmutil import svm_train\nimport numpy as np\nimport glob\nimport random\nimport copy\n\nn = 2000\ntrain_feat_path = './features/train'\n\nbirds = 20 # size of population\nmaxgen = 50\npos = [] # population of class\nspeed = []\nbestpos = []\ninitpos = []\ntempfit = []\nbirdsbestpos = []\nfds = []\ndict_fds = []\nlabels = []\nallbestpos = []\nw = 1 # best belongs to [0.8,1.2]\nc1 = 2\nc2 = 2\nr1 = random.uniform(0,1)\nr2 = random.uniform(0,1)\nm = 'pso'\n\ndef zeroMean(dataMat):\n    meanVal=np.mean(dataMat,axis=0)\n#    joblib.dump(meanVal,'./features/PCA/meanVal_train_%s.mean' %m)\n    newData=dataMat-meanVal\n    return newData,meanVal\n\ndef pca(dataMat,n):\n    print \"Start to do PCA...\"\n    newData,meanVal=zeroMean(dataMat)\n    \n#    covMat=np.cov(newData,rowvar=0)\n#    eigVals,eigVects=np.linalg.eig(np.mat(covMat))\n#    joblib.dump(eigVals,'./features/PCA/eigVals_train_%s.eig' %m,compress=3)\n#    joblib.dump(eigVects,'./features/PCA/eigVects_train_%s.eig' %m,compress=3)\n    \n    eigVals = joblib.load('./features/PCA/eigVals_train_%s.eig' %m)\n    eigVects = joblib.load('./features/PCA/eigVects_train_%s.eig' %m)\n\n    eigValIndice=np.argsort(eigVals)\n    n_eigValIndice=eigValIndice[-1:-(n+1):-1]\n    n_eigVect=eigVects[:,n_eigValIndice]\n#    joblib.dump(n_eigVect,'./features/PCA/n_eigVects_train_%s_%s.eig' %(m,n))\n    lowDDataMat=newData*n_eigVect\n    return lowDDataMat\n\nfor feat_path in glob.glob(os.path.join(train_feat_path, '*.feat')):\n    data = joblib.load(feat_path)\n    fds.append(data[:-1])\n    labels.append(data[-1])\nfds = np.array(fds,dtype = float)\nfds= pca(fds,n)\nfds = np.array(fds,dtype = float)\n\nfor i in range(len(fds[:,0])):\n    dict_data = dict(zip(range(len(data))[1:],fds[i,:]))\n    dict_fds.append(dict_data)\n\nfor i in range(birds):\n    pos.append([])\n    speed.append([])\n    bestpos.append([])\n    initpos.append([])\n    tempfit.append([])\n\ndef CalDis(list):\n    fitness=0.0\n    param = '-t 2 -v 3 -c %s -g %s' %(list[0],list[1])\n    fitness = svm_train(labels, dict_fds, param)\n    return fitness\n\nfor i in range(birds):          #initial all birds' pos,speed\n    pos[i].append(random.uniform(10,30))\n    pos[i].append(random.uniform(0.5e-06, 1e-06)) # 1/num_features\n    speed[i].append(float(0))\n    speed[i].append(float(0))\n#    speed[i].append(random.uniform(-10,10))\n#    speed[i].append(random.uniform(-0.00002,0.00002))\n    bestpos[i] = copy.deepcopy(pos[i])\n    initpos[i] = copy.deepcopy(pos[i])\n\ndef FindBirdsMostPos():\n    best=CalDis(bestpos[0])\n    index = 0\n    for i in range(birds):\n        print \"\\n>>>>>The %d'd time to find globel best pos. Total %d times.\\n\" %(i+1, birds)\n        tempfit[i] = CalDis(bestpos[i])\n        if tempfit[i] > best:\n            best = tempfit[i]\n            index = i\n            print '------- %d: %f' %(index, best)\n    return best, bestpos[index]\n\nprint \"\\n-------------------------Initial Globel Best Pos----------------------------------\\n\"\nbest_predict, birdsbestpos = FindBirdsMostPos()   #initial birdsbestpos\nprint \"\\n-------------------------Done Globel Best Pos----------------------------------\\n\"\n\ndef NumMulVec(num,list):         #result is in list\n    for i in range(len(list)):\n        list[i] *= num\n    return list\n\ndef VecSubVec(list1,list2):   #result is in list1\n    for i in range(len(list1)):\n        list1[i] -= list2[i]\n    return list1\n\ndef VecAddVec(list1,list2):      #result is in list1\n    for i in range(len(list1)):\n        list1[i] += list2[i]\n    return list1\n\ndef UpdateSpeed():\n    #global speed\n    for i in range(birds):\n        temp1 = NumMulVec(w,speed[i][:])\n        temp2 = VecSubVec(bestpos[i][:],pos[i])\n        temp2 = NumMulVec(c1*r1,temp2[:])\n        temp1 = VecAddVec(temp1[:],temp2)\n        temp2 = VecSubVec(birdsbestpos[:],pos[i])\n        temp2 = NumMulVec(c2*r2,temp2[:])\n        speed[i] = VecAddVec(temp1,temp2)\n\ndef UpdatePos():\n    print \"Update Pos.\"\n    global bestpos,birdsbestpos,tempfit\n    for i in range(birds):\n        if pos[i][0]+speed[i][0] > 0 and pos[i][1]+speed[i][1] > 0: \n            VecAddVec(pos[i],speed[i])\n            if CalDis(pos[i]) > tempfit[i]:\n                bestpos[i] = copy.deepcopy(pos[i])\n    best_predict, birdsbestpos = FindBirdsMostPos()\n    return best_predict, birdsbestpos\n\nfor asd in range(maxgen):\n    print \"\\n>>>>>>>>The %d'd time to update parameters. Total %d times\\n\" %(asd+1, maxgen)\n    UpdateSpeed()\n    best_predict, best_para = UpdatePos()\n    \n    allbestpos.append([best_para, best_predict])\n    f=open('result/PSO_%s-%s-%s.txt' %(birds,maxgen,n),'w')\n    f.write(str(allbestpos))\n    f.close()\n    \nprint \"After %d iterations\\nthe best C is: %f\\nthe best gamma is: %f\" %(maxgen,best_para[0],best_para[1])\n"
  },
  {
    "path": "7_Hard_Negative_Mining+SVM.py",
    "content": "#!/usr/bin/env python\n#encoding:utf-8\n\nimport numpy as np\nimport sklearn.svm as ssv\nfrom sklearn.externals import joblib\nfrom skimage.feature import hog\nimport random\nimport glob\nimport os\nimport cv2\n\ndef trainSvm(datas, labels):\n    clf = ssv.SVC(kernel='rbf', C=17.255220940030252, gamma=1.2943653125547475e-06) #20,50,500,0.842850\n    print \"Training a SVM Classifier.\"\n    clf.fit(datas, labels)\n    return clf\n\ndef loadData(path):\n    fds_all = []\n#    fds = []\n#    labels = []\n    num=0\n    for feat_path in glob.glob(os.path.join(path, '*.feat')):\n        num += 1\n        data = joblib.load(feat_path)\n        fds_all.append(data)\n#        fds.append(data[:-1])\n#        labels.append(data[-1])\n        print \"%d Dealing with %s\" %(num,feat_path)\n    return fds_all\n\ndef sliding_window(image, stepSize, windowSize):\n\tfor y in xrange(0, image.shape[0], stepSize):\n\t\tfor x in xrange(0, image.shape[1], stepSize):   \n\t\t\tyield (x, y, image[y:y + windowSize[1], x:x + windowSize[0]])\n\ndef rgb2gray(im):\n    gray = im[:, :, 0]*0.2989+im[:, :, 1]*0.5870+im[:, :, 2]*0.1140\n    return gray\n\ndef savefeat(childDir, num_win, fd):\n    fd_name = childDir.split('.')[0] + '_%d.feat' %num_win\n    fd_path = os.path.join('./features/train_hnm/', fd_name)\n    joblib.dump(fd, fd_path,compress=3)\n\nif __name__ == \"__main__\":\n    normalize = True\n    visualize = False\n    block_norm = 'L2-Hys'\n    cells_per_block = [2,2]\n    pixels_per_cell = [20,20]\n    orientations = 9\n    new_model_path = './models/svm_pso_hnm.model'\n    train_feat_path = './features/train_'\n    fds_all = loadData(train_feat_path) \n\n    model_path = './models/svm_pso.model'\n    clf = joblib.load(model_path)\n    \n#--------------------hard_negative_mining-------------------------------------\n    negative_img_path = './train/negative'\n    num = 0\n    for childDir in os.listdir(negative_img_path):\n        num += 1\n        num_win = 0\n        print \"num: %d hard negative mining: %s\" %(num,childDir)\n        f = os.path.join(negative_img_path, childDir)\n        data = cv2.imread(f)\n        scales = [(100, 100), (200,200), (300,300), (400,400), (500, 500), (600,600), (800, 800)]\n        for (winW,winH) in scales:\n            for (x, y, window) in sliding_window(data, stepSize=100, windowSize=(winW,winH)):\n                result = 0\n                if window.shape[0] != winH or window.shape[1] != winW:\n                    continue\n                if window.shape[0] != 200 or window.shape[1] != 200:\n                    window = cv2.resize(window,(200,200),interpolation=cv2.INTER_CUBIC)\n                gray = rgb2gray(window)/255.0\n                window_fd = hog(gray, orientations, pixels_per_cell, cells_per_block, block_norm, visualize, normalize)\n                win_fd = window_fd.reshape(1, -1)\n                result = int(clf.predict(win_fd))\n                if result == 1:\n                    num_win += 1\n                    fd = np.concatenate((window_fd, (float(0),)))\n                    fds_all.append(fd)\n                    \n                    savefeat(childDir, num_win, fd)\n\n#                    fds.append(window_fd)\n#                    labels.append(float(0))\n\n    random.shuffle(fds_all)\n    fds = np.numpy(fds_all)[:, :-1]\n    labels = np.numpy(fds_all)[:, -1]\n    new_clf = trainSvm(fds, labels)\n#-----------------------------------------------------------------------------\n    joblib.dump(new_clf, new_model_path)\n    print \"Classifier saved to {}\".format(new_model_path)\n"
  },
  {
    "path": "8_SlidingWindow+SVM+NMS_image.py",
    "content": "#!/usr/bin/env python2\n# -*- coding: utf-8 -*-\n\"\"\"\nCreated on Tue Jul  4 15:10:57 2017\n\n@author: hans\n\"\"\"\n\nfrom imutils.object_detection import non_max_suppression\nimport numpy as np\nfrom skimage.feature import hog\nfrom sklearn.externals import joblib\nimport time\nimport cv2\n\nnormalize = True\nvisualize = False\nblock_norm = 'L2-Hys'\ncells_per_block = [2,2]\npixels_per_cell = [20,20]\norientations = 9\n\ndef rgb2gray(im):\n    gray = im[:, :, 0]*0.2989+im[:, :, 1]*0.5870+im[:, :, 2]*0.1140\n    return gray\n\ndef getFeat(data):\n    gray = rgb2gray(data)/255.0\n    fd = hog(gray, orientations, pixels_per_cell, cells_per_block, block_norm, visualize, normalize)\n    return fd\n\ndef sliding_window(image, stepSize, windowSize):\n\t# slide a window across the image\n\tfor y in xrange(0, image.shape[0], stepSize):\n\t\tfor x in xrange(0, image.shape[1], stepSize):\n\t\t\t# yield the current window    \n\t\t\tyield (x, y, image[y:y + windowSize[1], x:x + windowSize[0]])\n        \nif __name__ == \"__main__\":\n#    image_path = str(raw_input(\"Please enter the path of an image: \"))\n    image_path = 'test/positive/n03147509_3599.JPEG'\n    t0 = time.time()\n    model_path = './models/svm_pso.model'\n    clf = joblib.load(model_path)\n    \n    image = cv2.imread(image_path)\n    image = cv2.resize(image,(500,500),interpolation=cv2.INTER_CUBIC)\n    orig = image.copy()\n    orig = cv2.resize(orig,(500,500),interpolation=cv2.INTER_CUBIC)\n    rects = []\n    scales = [(200,200), (300,300), (400, 400), (image.shape[1],image.shape[0])]\n    for (winW,winH) in scales:\n        for (x, y, window) in sliding_window(image, stepSize=90, windowSize=(winW,winH)):\n            result = 0\n            if window.shape[0] != winH or window.shape[1] != winW:\n                continue\n            cv2.imshow(\"asd\", window)\n            cv2.waitKey(0)\n            print window.shape\n            if window.shape[0] != 200 or window.shape[1] != 200:\n                window = cv2.resize(window,(200,200),interpolation=cv2.INTER_CUBIC)\n            win_fd = getFeat(window)\n            win_fd.shape = 1,-1\n            result = int(clf.predict(win_fd))\n            print 'smamll image result is %d' %result\n            if result == 1:\n                rects.append([x, y, x + winW, y + winH])\n                cv2.rectangle(orig, (x, y), (x + winW, y + winH), (0, 0, 255), 2)\n    rects = np.array(rects)\n    pick = non_max_suppression(rects, probs=None, overlapThresh=0.65)\n    for (xA, yA, xB, yB) in pick:\n        cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2)\n    t1 = time.time()\n    print 'The cast of time is :%f seconds' % (t1-t0)\n    cv2.imshow(\"Before NMS\", orig)\n    cv2.imshow(\"After NMS\", image)\n    cv2.waitKey(0)\n"
  },
  {
    "path": "9_SlidingWindow+SVM+NMS_cam.py",
    "content": "#!/usr/bin/env python2\n# -*- coding: utf-8 -*-\n\"\"\"\nCreated on Tue Jul  4 15:10:57 2017\n\n@author: hans\n\"\"\"\n\nfrom imutils.object_detection import non_max_suppression\nimport numpy as np\nfrom skimage.feature import hog\nfrom sklearn.externals import joblib\nimport cv2\n\ndef rgb2gray(im):\n    gray = im[:, :, 0]*0.2989+im[:, :, 1]*0.5870+im[:, :, 2]*0.1140\n    return gray\n\ndef getFeat(data):\n    normalize = True\n    visualize = False\n    block_norm = 'L2-Hys'\n    cells_per_block = [2,2]\n    pixels_per_cell = [20,20]\n    orientations = 9\n    gray = rgb2gray(data)/255.0\n    fd = hog(gray, orientations, pixels_per_cell, cells_per_block, block_norm, visualize, normalize)\n    return fd\n\ndef sliding_window(image, stepSize, windowSize):\n\tfor y in xrange(0, image.shape[0], stepSize):\n\t\tfor x in xrange(0, image.shape[1], stepSize):   \n\t\t\tyield (x, y, image[y:y + windowSize[1], x:x + windowSize[0]])\n        \nif __name__ == \"__main__\":\n    model_path = './models/svm_pso_85_hnm_50.model'\n    clf = joblib.load(model_path)\n    c = cv2.VideoCapture(0)\n    while 1:\n        ret, image = c.read()\n        rects = []\n#        image = cv2.resize(image,(500,500),interpolation=cv2.INTER_CUBIC)\n        scales = [(200,200), (300,300)]\n#        scales = [(200,200), (250, 250), (300,300)]\n        for (winW,winH) in scales:\n            for (x, y, window) in sliding_window(image, stepSize=100, windowSize=(winW,winH)):\n                result = 0\n                if window.shape[0] != winH or window.shape[1] != winW:\n                    continue\n                if window.shape[0] != 200 or window.shape[1] != 200:\n                    window = cv2.resize(window,(200,200),interpolation=cv2.INTER_CUBIC)\n                    win_fd = getFeat(window)\n                    win_fd.shape = 1,-1\n                    result = int(clf.predict(win_fd))\n                    if result == 1:\n                        rects.append([x, y, x + winW, y + winH])\n        rects = np.array(rects)\n        pick = non_max_suppression(rects, probs=None, overlapThresh=0.1)\n        minx = 10000\n        miny = 10000\n        maxx = 0\n        maxy = 0\n        for (xA, yA, xB, yB) in pick:\n            if xA < minx:\n                minx = xA\n            if yA < miny:\n                miny = yA\n            if xB > maxx:\n                maxx = xB\n            if yB > maxy:\n                maxy = yB\n        if (abs(maxx - minx) < image.shape[1]) and (abs(maxy - miny) < image.shape[0]):\n            cv2.rectangle(image, (minx, miny), (maxx, maxy), (0, 255, 0), 2)\n        cv2.imshow(\"After NMS\", image)\n        cv2.waitKey(1)\n"
  },
  {
    "path": "README.md",
    "content": "# SVM-classification-detection (Python2.7)\nHoG, PCA, PSO, Hard Negative Mining, Sliding Window, NMS\n\n\n![image](https://github.com/HansRen1024/SVM-classification-localization/blob/master/example.gif)\n\n\nBest way to do detection is:\n\nHoG(features) -> PCA(less features) + PSO(best C&gamma) -> origin SVM -> HNM(more features) -> better SVM -> SW -> NMS(bbox regression)\n\nSorry for my laziness.\n\nI think I should clarify the steps for the program.\n\n1. Extract HoG features (script 1)\n\n2. Train an initial model for pso (script 2)\n\n3. Do pca and pso for better parameters C and gamma (script 6)\n\n4. Use no-pca features and the best parameters to train the second model (script 2)\n\n5. In order to increase the accuracy, use the second model to do hnm and get the final model(script 7)\n\n6. Finally, choose an algorithm you like to do location(script 8 or 9 or 10)\n\n**PS:**\n\n1. The reason I use pca is to accelerate the speed of pso. To be honestly, pso is really slow.\n\n2. For step 4, you can also use features processed by pca, but I strongly advise you to hold as possible as more features. Because more features, higher accuracy.\n\n杯子数据集(Dataset)： https://pan.baidu.com/s/18ho4UI50x4YP6lkrjPm7Kw\n\n"
  }
]