In [1]:
with open('./calories.txt') as f:
    content = list(f.readlines())

In [2]:
import scipy.misc
import os

target_dir = 'pfid'
result = {}
count = 0 
for i, c in enumerate(content):
    x = (c.strip().split("'"))
    fname = x[1]
    try:
        calorie = int(x[2].strip())
    except:
        calorie = -1
    # print(fname, calorie)
    if (os.path.splitext(fname)[1]) == '.jpg':
        current = scipy.misc.imread(fname)
        x, y, _ = current.shape
        if x > 1000:
            x_start = int(x / 3)
            x_end = int(x_start + (2 * x) / 3)
            y_start = int(y / 6)
            y_end = int(y_start + (2 * y) / 3)
            foto = current[x_start:x_end, y_start:y_end, :]
            new_file_name = os.path.join(target_dir, str(count) + '.jpg')
            resized = scipy.misc.imresize(foto, (256, 256))
            scipy.misc.imsave(new_file_name, resized)
            result[new_file_name] = calorie
            count += 1

In [5]:
with open('pfid/calories.txt', 'w') as f:
    for i, j in result.items():
        if os.path.exists(i):
            print(i, j, file=f)

In [1]:
import scipy.io
x = scipy.io.loadmat('./food101features/resnet_food101_100.mat')

In [2]:
import os
features = []
for name in os.listdir('./food101features/'):
    features.append(scipy.io.loadmat(os.path.join('./food101features/', name))['x'])

In [4]:
import numpy as np
allx = np.vstack(features)

In [15]:
allx.shape


Out[15]:
(100602, 2048)

In [11]:
import scipy.misc as sm
def make_y(content):
    
    result = []
    i = 1
    for a, y in content:
        path = a
        if os.path.splitext(a)[1] != '.jpg':
            print(a)
            continue
        imgcontent = sm.imread(path).astype(np.float32)
        if imgcontent.shape != (256, 256, 3):
            print(imgcontent.shape)
            continue
        yield y

In [24]:
with open('./food101/calories.txt') as f:
    all_y = list(map(str.split, f.readlines()))
y = list(make_y(all_y))


(256, 256)
(256, 256)
(256, 256)

In [25]:
newy = y[:100602]

newy = np.array(list(map(float, newy)))

In [26]:
scipy.io.savemat('resnet_features', {'x': allx, 'y':newy})

In [22]:
y.shape


Out[22]:
()