Compare commits

..

No commits in common. "2f98d26842aa283ded2ed0f4656c97fc976a3fd4" and "d5a4b46c7a71213a7576f7ca843e91f4686b2dc1" have entirely different histories.

2 changed files with 3 additions and 45 deletions

15
TODO.md
View file

@ -1,15 +0,0 @@
# TODOO
[x] fix get_avg_color
[ ] make rasterizer function return a grayscale image rather than rgb
[x] implement score function: (image, mask: grayscale) -> score number
[ ] resize font to be size of character bounding box
[ ] implement a final function check: (image) -> which font it probably is
[ ] test(run it in a loop for the result)
## In case results are bad
[ ] modify score system to use squared error rather than normal error
[ ] noramlize the mask before use
[ ] if still bad, maybe contact professor for improvement idea?

View file

@ -4,10 +4,6 @@ import h5py as h5
db = None db = None
def init_train():
''' Default init based on the train set `train.h5` '''
init('train.h5')
def init(path): def init(path):
''' initializes the database, must be called before any use ''' ''' initializes the database, must be called before any use '''
global db global db
@ -55,41 +51,18 @@ def get_avg_color(img, mask):
gets avg color from an image that is underneath a mask, gets avg color from an image that is underneath a mask,
img and mask needs to be of same size(in x,y) but img can img and mask needs to be of same size(in x,y) but img can
have any third dimension size it want(usually 3 for rgb or 1 for grayscale) have any third dimension size it want(usually 3 for rgb or 1 for grayscale)
mask needs to be of shape(img.width, img.height, 1)
''' '''
sx, sy, sw = img.shape sx, sy, sw = img.shape
mx, my = mask.shape mx, my = mask.shape
if sx != mx or sy != my: if sx != mx or sy != my:
print('Image and mask size doesnt match!') print('Image and mask shape doesnt match!')
return None return None
avg = np.zeros(sw, dtype=np.float32) avg = np.array(sw, dtype=np.float32)
count = 0.0 count = 0.0
for x in range(sx): for x in range(sx):
for y in range(sy): for y in range(sy):
m = mask[x, y] m = mas[x, y]
avg += img[x, y] * m avg += img[x, y] * m
count += m count += m
avg /= count avg /= count
return avg return avg
def calc_score(img, mask, avg_color):
'''
Calculates the score for each mask with each color
'''
sx, sy, sw = img.shape
mx, my = mask.shape
if sx != mx or sy != my:
print('Image and mask size doesnt match!')
return 0.0
if sw != avg_color.shape[0]:
print('Image width doesnt match color width!')
return 0.0
score = 0.0
for x in range(sx):
for y in range(sy):
m = 0.5 - mask[x, y]
diff = img[x, y] - avg_color
mag = np.sqrt(diff.dot(diff)) # calculate magnitude
score += mag * m
return score