2017-05-21 120 views
0

我有下面的代碼來做到這一點看Kaggle面部識別數據集:在Python 3.5

from __future__ import print_function, division 
from builtins import range, input 
# Note: you may need to update your version of future 
# sudo pip install -U future 

import numpy as np 
import matplotlib.pyplot as plt 

import os 
os.chdir('C:\\Users\\User\\Google Drive\\Udemy\\LP_CNN in Python\\facial-expression-recognition') 
os.getcwd() 

def getData(balance_ones=True): 
    # images are 48x48 = 2304 size vectors 
    # N = 35887 
    Y = [] 
    X = [] 
    first = True 
    for line in open('fer2013.csv'): 
     if first: 
      first = False 
     else: 
      row = line.split(',') 
      Y.append(int(row[0])) 
      X.append([int((p)) for p in row[1].split()]) 

    X, Y = np.array(X)/255.0, np.array(Y) 

    if balance_ones: 
     # balance the 1 class 
     X0, Y0 = X[Y!=1, :], Y[Y!=1] 
     X1 = X[Y==1, :] 
     X1 = np.repeat(X1, 9, axis=0) 
     X = np.vstack([X0, X1]) 
     Y = np.concatenate((Y0, [1]*len(X1))) 

    return X, Y 

label_map = ['Anger', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral'] 

def main(): 
    X, Y = getData(balance_ones=False) 

    while True: 
     for i in range(7): 
      x, y = X[Y==i], Y[Y==i] 
      N = len(y) 
      j = np.random.choice(N) 
      plt.imshow(x[j].reshape(48, 48), cmap='gray') 
      plt.title(label_map[y[j]]) 
      plt.show() 
     prompt = input('Quit? Enter Y:\n') 
     if prompt == 'Y': 
      break 


if __name__ == '__main__': 
    main() 

此問題是,我接到了一個視頻教程,與Python 2.7版作品的代碼。當我的Python 3.5運行它,我得到以下錯誤:

runfile('C:/Users/User/Google Drive/Udemy/LP_CNN in Python/facial-expression-recognition/show_images.py', wdir='C:/Users/User/Google Drive/Udemy/LP_CNN in Python/facial-expression-recognition') 
Traceback (most recent call last): 

    File "<ipython-input-15-efb5f5556496>", line 1, in <module> 
    runfile('C:/Users/User/Google Drive/Udemy/LP_CNN in Python/facial-expression-recognition/show_images.py', wdir='C:/Users/User/Google Drive/Udemy/LP_CNN in Python/facial-expression-recognition') 

    File "C:\Users\User\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile 
    execfile(filename, namespace) 

    File "C:\Users\User\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile 
    exec(compile(f.read(), filename, 'exec'), namespace) 

    File "C:/Users/User/Google Drive/Udemy/LP_CNN in Python/facial-expression-recognition/show_images.py", line 58, in <module> 
    main() 

    File "C:/Users/User/Google Drive/Udemy/LP_CNN in Python/facial-expression-recognition/show_images.py", line 42, in main 
    X, Y = getData(balance_ones=False) 

    File "C:/Users/User/Google Drive/Udemy/LP_CNN in Python/facial-expression-recognition/show_images.py", line 24, in getData 
    Y.append(int(row[0])) 

ValueError: invalid literal for int() with base 10: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0 

有很多線程網上有關解決這個問題,int和我已經試過幾件事情如int(浮點(行[0]))等。但他們只是導致進一步的錯誤:(

我想指出的是,數據集可以在https://www.kaggle.com/c/challenges-in-representation-learning-facial-expression-recognition-challenge/data

我似乎已經正確下載它可以下載從Kaggle的tar.gz文件,因爲它顯示在良好Excel(作爲一個.csv)和記事本。

有人知道如何解決這個問題嗎?或者有替代的Python 3.5代碼來顯示像素的.csv文件中的圖像?

非常感謝!

回答

-1

這是正確的代碼:

from __future__ import print_function, division 
from builtins import range 
# Note: you may need to update your version of future 
# sudo pip install -U future 

import numpy as np 
import pandas as pd 

def convert_tar2csv(): 
    df = pd.read_csv('fer2013.tar', compression='gzip', 
        header=0, sep=' ', quotechar='"', error_bad_lines=False) 
    df.to_csv('fer2013.csv') 

def getData(balance_ones=True, thresh=35889): 
    # images are 48x48 = 2304 size vectors 
    Y = [] 
    X = [] 
    cv = [] 
    first = True 
    i = 0 
    for line in open('fer2013.csv'): 
     i += 1 
     if first: 
      first = False 
     elif i < thresh: 
      row = line.replace('"', '')[:-1].split(',') 
      Y.append(int(row[0])) 
      X.append([float(p) for p in row[1:-1]]) 
      cv.append(row[-1]) 

    X, Y, cv = np.array(X)/255.0, np.array(Y), np.array(cv) 

    if balance_ones: 
     # balance the 1 class 
     X0, Y0, cv0 = X[Y != 1, :], Y[Y != 1], cv[Y != 1] 
     X1 = X[Y==1, :] 
     X1 = np.repeat(X1, 9, axis=0) 
     X = np.vstack([X0, X1]) 
     cv1 = cv[Y == 1, :] 
     cv1 = np.repeat(cv1, 9, axis=0) 
     cv = np.vstack([cv0, cv1]) 
     Y = np.concatenate((Y0, [1]*len(X1))) 

    return X, Y, cv 


def getBinaryData(thresh=35889): 
    Y = [] 
    X = [] 
    cv = [] 
    i = 0 
    first = True 
    for line in open('fer2013.csv'): 
     i += 1 
     if first: 
      first = False 
     elif i < thresh: 
      row = line.replace('"', '')[:-1].split(',') 
      y = int(row[0]) 
      if y == 0 or y == 1: 
       Y.append(y) 
       X.append([float(p) for p in row[1:-1]]) 
       cv.append(row[-1]) 
    return np.array(X)/255.0, np.array(Y), np.array(cv) 


def getImageData(): 
    X, Y, cv = getData() 
    N, D = X.shape 
    d = int(np.sqrt(D)) 
    X = X.reshape(N, 1, d, d) 
    return X, Y, cv 

祝你好運:)