Пример #1
0
def ReadImages(ListName, FolderName, Label):
    #global NumberList
    global responseData
    global trainData
    #global hog
    #global cv2
    #global imutils
    #global winSize
    global testData
    #global os

    global feature

    for image in ListName:
        face_cascade = cv2.CascadeClassifier(
            'haarcascade_frontalface_default.xml')
        img = cv2.imread(join(FolderName, image))
        imgray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        face = face_cascade.detectMultiScale(imgray)

        if len(face) > 0:
            feature = HOG(
                cv2.resize(
                    imgray[face[0][1]:face[0][1] + face[0][3],
                           face[0][0]:face[0][0] + face[0][2]], (100, 100)))
            trainData.append(feature.T)
            responseData.append(Label)
Пример #2
0
def ReadImages(ListName, FolderName):
    for image in ListName:
        #        face_cascade =cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
        #        img = cv2.imread(join(FolderName,image))
        #        imgray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
        #        face = face_cascade.detectMultiScale(imgray,minSize = (100, 100),scaleFactor = 1.25) #,scaleFactor = 1.15, minNeighbors = 5,
        #        print(face)
        #        if len(face)>0:
        #            x, y, w, h =  face[0]
        #            cv2.rectangle(imgray, (x, y), (x + w, y + h), (0, 255, 0), 2)
        #        else:
        #            pass
        #        cv2.imshow("Objects found", imgray)
        #        cv2.waitKey(100)
        #        sleep(0.3)
        #        cv2.destroyAllWindows()
        #        cv2.waitKey(1)
        #        cv2.waitKey(1)
        #        cv2.waitKey(1)
        #        cv2.waitKey(1) #bug in openCV linux
        try:
            image = cv2.imread(join(FolderName, image))
            imgray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
            face_cascade = cv2.CascadeClassifier(
                'haarcascade_frontalface_default.xml')
            face = face_cascade.detectMultiScale(imgray,
                                                 minSize=(50, 50),
                                                 scaleFactor=1.25)
            if len(face) > 0:
                feature, hog_image = HOG(cv2.resize(
                    imgray[face[0][1]:face[0][1] + face[0][3],
                           face[0][0]:face[0][0] + face[0][2]], (100, 100)),
                                         cells_per_block=(1, 1),
                                         visualise=True)
                cropped_image = imgray[face[0][1]:face[0][1] + face[0][3],
                                       face[0][0]:face[0][0] + face[0][2]]
                fig, (ax1, ax2) = plt.subplots(1, 2)

                ax1.axis('off')
                ax1.imshow(cropped_image, cmap=plt.cm.gray)
                ax1.set_title('Input image')
                ax1.set_adjustable('box-forced')

                # Rescale histogram for better display
                #hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 0.02))

                ax2.axis('off')
                ax2.imshow(hog_image, cmap=plt.cm.gray)
                ax2.set_title('Histogram of Oriented Gradients')
                ax1.set_adjustable('box-forced')
                plt.show()
            else:
                continue
        except Exception as e:
            print(e)
            pass
Пример #3
0
def ReadImages(ListName, FolderName, Label):
    global NumberList
    global responseData
    global testData
    global hog
    global cv2
    global imutils
    global winSize
    ListName = ListName[0:100]
    for image in ListName:
        img = cv2.imread(join(FolderName, image))
        NumberList.append(img)
        feature = HOG(cv2.cvtColor(img, cv2.COLOR_RGB2GRAY))
        testData.append(feature.T)
        realResponse.append(Label)
Пример #4
0
def ReadImages(ListName,FolderName,Label):
    global NumberList
    global responseData
    global trainData
    global hog
    global cv2
    global imutils
    global winSize
    for image in ListName:
        img = cv2.imread(join(FolderName,image))
        img = cv2.resize(img,(50,50))
        NumberList.append(img)    
        feature = HOG(cv2.cvtColor(img,cv2.COLOR_RGB2GRAY))
        trainData.append(feature.T)
        responseData.append(Label)
Пример #5
0
 def ReadImages(self, ListName, FolderName, Label):
     for image in ListName:
         img = cv2.imread(join(FolderName, image))
         try:
             imgray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
             face = self.face_cascade.detectMultiScale(imgray,
                                                       minSize=(50, 50),
                                                       scaleFactor=1.25)
             if len(face) > 0:
                 feature = HOG(
                     cv2.resize(
                         imgray[face[0][1]:face[0][1] + face[0][3],
                                face[0][0]:face[0][0] + face[0][2]],
                         (100, 100)))
                 self.trainData.append(feature.T)
                 self.responseData.append(Label)
         except Exception as e:
             print(e)
             pass
Пример #6
0
 def inputTestData(self, name):
     name = self.trainFolder + str(name) + '/'
     images = [f for f in listdir(name) if isfile(join(name, f))]
     for image in images:
         img = cv2.imread(join(name, image))
         try:
             imgray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
             face = self.face_cascade.detectMultiScale(imgray,
                                                       minSize=(50, 50),
                                                       scaleFactor=1.25)
             if len(face) > 0:
                 feature = HOG(
                     cv2.resize(
                         imgray[face[0][1]:face[0][1] + face[0][3],
                                face[0][0]:face[0][0] + face[0][2]],
                         (100, 100)))
                 self.testData.append(feature.T)
                 self.testDict[image] = feature.T
         except Exception as e:
             print(e)
             pass
Пример #7
0
ReadImages(GalImages, Gal, 6)
ReadImages(SandbergImages, Sandberg, 7)
ReadImages(SainaImages, Saina, 8)

svm = NuSVC()
nu_options = np.arange(0.1, 1)
kernel_options = ['linear', 'rbf']
param_grid = dict(kernel=kernel_options, nu=nu_options)
gridSVM = GridSearchCV(svm, param_grid, scoring='accuracy')
X = np.float32(trainData)
y = np.float32(responseData)
gridSVM.fit(X, y)
print(gridSVM.best_score_)
print(gridSVM.best_estimator_)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

TestImages = [f for f in listdir(Test) if isfile(join(Test, f))]
for image in TestImages:
    img = cv2.imread(join(Test, image))
    imgray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    face = face_cascade.detectMultiScale(imgray)
    if len(face) > 0:
        feature = HOG(
            cv2.resize(
                imgray[face[0][1]:face[0][1] + face[0][3],
                       face[0][0]:face[0][0] + face[0][2]], (100, 100)))
    pred = gridSVM.predict(feature)
    plt.figure()
    plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    plt.title(num2name(pred))
Пример #8
0
    pred = np.argmax(pred, axis=1)
    plt.figure()
    plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    plt.title(predDict[int(pred)])
    try:
        image = cv2.imread(join(Test, image))
        imgray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
        face_cascade = cv2.CascadeClassifier(
            'haarcascade_frontalface_default.xml')
        face = face_cascade.detectMultiScale(imgray,
                                             minSize=(50, 50),
                                             scaleFactor=1.25)
        if len(face) > 0:
            feature, hog_image = HOG(cv2.resize(
                imgray[face[0][1]:face[0][1] + face[0][3],
                       face[0][0]:face[0][0] + face[0][2]], (100, 100)),
                                     cells_per_block=(1, 1),
                                     visualise=True)
            cropped_image = imgray[face[0][1]:face[0][1] + face[0][3],
                                   face[0][0]:face[0][0] + face[0][2]]
            fig, (ax1, ax2) = plt.subplots(1, 2)

            ax1.axis('off')
            ax1.imshow(cropped_image, cmap=plt.cm.gray)
            ax1.set_title('Input image')
            ax1.set_adjustable('box-forced')

            # Rescale histogram for better display
            #hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 0.02))

            ax2.axis('off')
Пример #9
0
        responseData.append(Label)


ReadImages(ZeroImages, Zero, 0)
ReadImages(OneImages, One, 1)
ReadImages(TwoImages, Two, 2)
ReadImages(ThreeImages, Three, 3)
ReadImages(FourImages, Four, 4)
ReadImages(FiveImages, Five, 5)
ReadImages(SixImages, Six, 6)
ReadImages(SevenImages, Seven, 7)
ReadImages(EightImages, Eight, 8)
ReadImages(NineImages, Nine, 9)

trainNp = np.float32(trainData)
responseNp = np.float32(responseData)
X = np.float32(trainNp)
y = np.float32(responseNp)

svm = NuSVC(kernel='linear', nu=0.2)
svm.fit(X, y)

for i in range(len(TestImages)):
    img = cv2.imread(join(testFolder, TestImages[i]))
    img2 = cv2.resize(img, (28, 28))
    featuretest = HOG(cv2.cvtColor(img2, cv2.COLOR_RGB2GRAY))
    ytest = svm.predict(featuretest)
    plt.figure()
    plt.imshow(img)
    plt.title(str(ytest))