Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_image_hash(self,file):
if(not os.path.isfile(file)):
print file+"is not a file"
# sys.exit(0)
try:
img = Image.open(file)
h = str(imagehash.dhash(img))
return h
except Exception, e:
raise
else:
pass
finally:
pass
all_possible_values = histogram.keys()
col_stats = {
'data_type': data_type,
'data_subtype': curr_data_subtype,
"histogram": {
"x": list(histogram.keys()),
"y": list(histogram.values())
}
#"percentage_buckets": list(histogram.keys())
}
elif curr_data_subtype == DATA_SUBTYPES.IMAGE:
image_hashes = []
for img_path in col_data:
img_hash = imagehash.phash(Image.open(img_path))
seq_hash = []
for hash_row in img_hash.hash:
seq_hash.extend(hash_row)
image_hashes.append(np.array(seq_hash))
kmeans = MiniBatchKMeans(n_clusters=20, batch_size=round(len(image_hashes)/4))
kmeans.fit(image_hashes)
if hmd is not None:
hmd['bucketing_algorithms'][col_name] = kmeans
x = []
y = [0] * len(kmeans.cluster_centers_)
path = '/home/sol315/pictures/'
left_str = c_str.split('=>')[0].split('++')[0]
right_str = c_str.split('=>')[1].split('++')[0]
try:
im_l = Image.open('{}{}.png'.format(path, left_str))
im_r = Image.open('{}{}.png'.format(path, right_str))
except:
return 2
imgwidth, imgheight = im_l.size
width = imgwidth / 2
height = imgheight
box = (0, 0, width, height)
a_1 = im_l.crop(box)
h_l = imagehash.average_hash(im_l)
a_2 = im_r.crop(box)
h_r = imagehash.average_hash(im_r)
if h_l == h_r:
return 1
else:
return 0
return 0 for text change, 1 for emoji change
"""
path = '/home/sol315/pictures/'
left_str = c_str.split('=>')[0].split('++')[0]
right_str = c_str.split('=>')[1].split('++')[0]
try:
im_l = Image.open('{}{}.png'.format(path, left_str))
im_r = Image.open('{}{}.png'.format(path, right_str))
except:
return 2
imgwidth, imgheight = im_l.size
width = imgwidth / 2
height = imgheight
box = (0, 0, width, height)
a_1 = im_l.crop(box)
h_l = imagehash.average_hash(im_l)
a_2 = im_r.crop(box)
h_r = imagehash.average_hash(im_r)
if h_l == h_r:
return 1
else:
return 0
if file.endswith(('.png', '.bmp', '.jpg')):
movie_name = file[0:file.rfind('.bk2') + 4]
# skip everything which is not from a movie file
if movie_name == 'new' or not root.endswith('.bk2'):
continue
file_ending = file.split('.')[-1]
index = int(file[file.rfind('_') + 1:file.rfind('.{}'.format(file_ending))])
image_hash = self.calculate_img_hash(os.path.join(root, file))
self.all_hashes.append(image_hash)
self.hashes[str(image_hash)].append(self.pressed_keys[movie_name][index])
self.hash_to_file[image_hash].append(file)
self.printer.log('Time for all hashes: {}'.format(time.time() - start_time))
# if pickled we need to store all image hashes
if len(self.all_hashes) == 0:
self.all_hashes = [imagehash.hex_to_hash(i) for i in self.hashes.keys()]
# pickle the hashes
if not pickled or overwrite:
with open(filename_hashes, 'wb') as f:
pickle.dump(self.hashes, f, protocol=pickle.HIGHEST_PROTOCOL)
with open(filename_hash_to_file, 'wb') as f:
pickle.dump(self.hash_to_file, f, protocol=pickle.HIGHEST_PROTOCOL)
"Elapsed time": str(DT.datetime.now() - start_datetime).rsplit('.', maxsplit=1)[0],
})
if progress.wasCanceled():
break
print(i, '/', number_image_by_hashes, other_file_name)
if other_file_name == file_name:
continue
other_hash_value = hashes[hash_algo]
# TODO: Monkey patch. https://github.com/JohannesBuchner/imagehash/issues/112
if hash_algo == 'colorhash':
from PIL import Image
other_hash_value = imagehash.colorhash(Image.open(other_file_name))
score = hash_value - other_hash_value
print(f'Score: {score:2}. Similar images: {file_name!r} and {other_file_name!r}. '
f'{hash_value} vs {other_hash_value}')
# TODO: выяснить максимальные значения для каждого из алгоритмом
# думаю, можно ориентироваться на длину хеша
# можно в sql посмотреть или в self.image_by_hashes[file_name]
if score > max_score:
continue
# print(f'Score: {score:2}. Similar images: {file_name!r} and {other_file_name!r}. '
# f'{hash_value} vs {other_hash_value}')
results.append(other_file_name)
fetch_data.get_valid_filename(card_info['name']))
card_img = cv2.imread(img_name)
# If the image doesn't exist, download it from the URL
if card_img is None:
fetch_data.fetch_card_image(card_info,
out_dir='%s/card_img/png/%s' % (Config.data_dir, card_info['set']))
card_img = cv2.imread(img_name)
if card_img is None:
print('WARNING: card %s is not found!' % img_name)
# Compute value of the card's perceptual hash, then store it to the database
#img_art = Image.fromarray(card_img[121:580, 63:685]) # For 745*1040 size card image
img_card = Image.fromarray(card_img)
for hs in hash_size:
card_hash = ih.phash(img_card, hash_size=hs)
card_info['card_hash_%d' % hs] = card_hash
#art_hash = ih.phash(img_art, hash_size=hs)
#card_info['art_hash_%d' % hs] = art_hash
new_pool.loc[0 if new_pool.empty else new_pool.index.max() + 1] = card_info
if save_to is not None:
new_pool.to_pickle(save_to)
return new_pool
def predict_finishline(self, image):
"""
Predicts if the finish line was passed based on the appearance of the ghost
:param image: PIL Image
:return: bool, True if finish line was passed, False if not
"""
img_cropped = image.crop((0, 0, 250, 100))
img_array = np.array(img_cropped).reshape(1, -1)
finish_line = bool(self.classifier['lap'].predict(img_array) == [1])
if finish_line:
hash_value = str(imagehash.phash(img_cropped, hash_size=5))
self.printer.log(hash_value)
if hash_value in self.true_positives and hash_value in self.false_positives:
raise ValueError("hash {} was found in both false and true positives".format(hash_value))
direc = os.path.join(os.getcwd(), "classifiers", "round_passed_real_cases")
if hash_value in self.false_positives:
self.printer.log('false positive')
return False
elif hash_value in self.true_positives:
self.printer.log('true positive')
with open("{}/{}.finished".format(direc, hash_value), 'w') as f:
f.write('true positive')
return True
else:
image.save("{}/{}.png".format(direc, hash_value))
def identify_obj(examplar_path, candidate_path, targets):
# get the closest obj
examplar = Image.open(examplar_path)
candidate = Image.open(candidate_path)
scores = []
p_examplar = phash(examplar)
for i, target in enumerate(targets):
target = tuple(int(x) for x in target)
img_cropped = candidate.crop(target)
img_cropped.resize(examplar.size)
p_img_cropped = phash(img_cropped)
scores.append(p_img_cropped - p_examplar)
scores.sort()
return targets[np.argsort(scores)[0]]
@param mask: An image matching the dimensions of the source, but 1 channel grayscale
@return: The similarity between the hashes of the image as a number 0 to 1.
"""
# Since imagehash doesn't have any masking itself, bitwise_and will allow us
# to apply the mask to the source and capture before calculating the pHash for
# each of the images. As a result of this, this function is not going to be very
# helpful for large masks as the images when shrinked down to 8x8 will mostly be
# the same
source = cv2.bitwise_and(source, source, mask=mask)
capture = cv2.bitwise_and(capture, capture, mask=mask)
source = Image.fromarray(source)
capture = Image.fromarray(capture)
source_hash = imagehash.phash(source)
capture_hash = imagehash.phash(capture)
return 1 - ((source_hash - capture_hash)/64.0)