def clearn(datas): # arrat of (tipe, warna, image) dtipe = {} dcolor = {} for image in datas: img = image[2] img = futil.image_cleanup(img, 20) shape = fshape.feature_shape_extract(img) color = fcolor.feature_color_extract(img) texture = ftexture.feature_texture_extract(img) if not image[0] in dtipe: dtipe[image[0]] = [] if not image[1] in dcolor: dcolor[image[1]] = [] data = shape[0] data.extend(shape[1:]) data.extend(texture) dtipe[image[0]].append(data) dcolor[image[1]].append(color) with open('data_type.json', 'w') as ft, open('data_color.json', 'w') as fc: json.dump(dtipe, ft) json.dump(dcolor, fc) return 'data_type.json', 'data_color.json'
traintype = {} traincolor = {} with open('data_type.json') as ft, open('data_color.json') as fc: traintype = json.load(ft) traincolor = json.load(fc) for item in items: img = Image.open(item) img = futil.image_cleanup(img, 20) container = {} imgs = futil.image_clip(img) for obj in imgs: shape = fshape.feature_shape_extract(obj) color = fcolor.feature_color_extract(obj) texture = ftexture.feature_texture_extract(obj) # Get Color colorval = {} colorsum = [] for key, val in traincolor.iteritems(): colorval[key] = [] for feature in val: mean = (feature[1][0][0] - color[1][0][0]) ** 2 + (feature[1][0][1] - color[1][0][1]) ** 2 + (feature[1][0][2] - color[1][0][2]) ** 2 std = (feature[1][1][0] - color[1][1][0]) ** 2 + (feature[1][1][1] - color[1][1][1]) ** 2 + (feature[1][1][2] - color[1][1][2]) ** 2 skew = (feature[1][2][0] - color[1][2][0]) ** 2 + (feature[1][2][1] - color[1][2][1]) ** 2 + (feature[1][2][2] - color[1][2][2]) ** 2 colorval[key].append(mean + std + skew) colorval[key].sort() dist = sum(colorval[key][:3])
traintype = {} traincolor = {} with open('data_type.json') as ft, open('data_color.json') as fc: traintype = json.load(ft) traincolor = json.load(fc) for item in items: img = Image.open(item) img = futil.image_cleanup(img, 20) container = {} shape = fshape.feature_shape_extract(img) color = fcolor.feature_color_extract(img) texture = ftexture.feature_texture_extract(img) # Get Color colorval = {} colorsum = [] for key, val in traincolor.iteritems(): colorval[key] = [] for feature in val: mean = (feature[1][0][0] - color[1][0][0]) ** 2 + (feature[1][0][1] - color[1][0][1]) ** 2 + (feature[1][0][2] - color[1][0][2]) ** 2 std = (feature[1][1][0] - color[1][1][0]) ** 2 + (feature[1][1][1] - color[1][1][1]) ** 2 + (feature[1][1][2] - color[1][1][2]) ** 2 skew = (feature[1][2][0] - color[1][2][0]) ** 2 + (feature[1][2][1] - color[1][2][1]) ** 2 + (feature[1][2][2] - color[1][2][2]) ** 2 colorval[key].append(mean + std + skew) colorval[key].sort() dist = sum(colorval[key][:3])
def ctest(dtype, dcolor, datas): traintype = dtype traincolor = dcolor for item in datas: img = futil.image_cleanup(item[0], 20) shape = fshape.feature_shape_extract(img) color = fcolor.feature_color_extract(img) texture = ftexture.feature_texture_extract(img) # Get Color colorval = {} colorsum = [] for key, val in traincolor.iteritems(): colorval[key] = [] for feature in val: mean = (feature[1][0][0] - color[1][0][0]) ** 2 + (feature[1][0][1] - color[1][0][1]) ** 2 + (feature[1][0][2] - color[1][0][2]) ** 2 std = (feature[1][1][0] - color[1][1][0]) ** 2 + (feature[1][1][1] - color[1][1][1]) ** 2 + (feature[1][1][2] - color[1][1][2]) ** 2 skew = (feature[1][2][0] - color[1][2][0]) ** 2 + (feature[1][2][1] - color[1][2][1]) ** 2 + (feature[1][2][2] - color[1][2][2]) ** 2 colorval[key].append(mean + std + skew) colorval[key].sort() dist = sum(colorval[key][:2]) # print key, colorval[key], dist if dist > thresholdc: continue colorsum.append((dist, key)) colorname = "uc" if colorsum: colorsum.sort() colorname = colorsum[0][1] # Get Type data = shape[0] data.extend(shape[1:]) data.extend(texture) idx = range(len(weight_table)) typeval = {} typesum = [] for key, val in traintype.iteritems(): typeval[key] = [] fx = [] for i in idx: fx.append([]) # Apply weight and sum the squared difference of each feature for feature in val: for i in idx: fx[i].append(feature[i]) typeval[key].append(sum([((data[i] - feature[i]) * weight_table[i]) ** 2 for i in idx])) # Calculate shape distance with the average. If a shape have large distance, it might be an unknown shape. avg = [weight_table[i] * np.mean(fx[i]) for i in idx] std = [weight_table[i] * np.std(fx[i]) for i in idx] dist = np.sqrt(sum([(data[i] * weight_table[i] - avg[i]) ** 2 / std[i] if abs(std[i]) > 1e-10 else 0. for i in idx])) if dist > thresholdt: # If it is unknown, skip it continue # Take 3 best match typeval[key].sort() typesum.append((sum(typeval[key][:3]), key)) typename = 'ut' if typesum: typesum.sort() typename = typesum[0][1] item[1].set(kamus[colorname]+' '+kamus[typename])