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
def dhash(self, image, raidNo, hash_size=8):
# Grayscale and shrink the image in one step.
h = str(imagehash.dhash(image))
return h
image = image.convert('L').resize(
(hash_size + 1, hash_size),
Image.ANTIALIAS,
)
pixels = list(image.getdata())
# Compare adjacent pixels.
difference = []
for row in range(hash_size):
for col in range(hash_size):
pixel_left = image.getpixel((col, row))
pixel_right = image.getpixel((col + 1, row))
difference.append(pixel_left > pixel_right)
# Convert the binary array to a hexadecimal string.
decimal_value = 0
def _dhash(self):
for path in self.image_paths:
try:
with Image.open(path) as img:
dhash = imagehash.dhash(img)
self.dhash_data[dhash] = self.dhash_data.get(dhash, []) + [path]
except IOError:
print 'could not find image in', path
# DEBUG # pil_picture.save('/home/user/Desktop/debug_pil.bmp')
try:
# Note : @image must be a PIL instance.
if self.fe_conf.A_HASH.get("is_enabled", False):
self.logger.debug("A-HASH ... ")
answer["A_HASH"] = self.check_null_hash(imagehash.average_hash(pil_picture))
if self.fe_conf.P_HASH.get("is_enabled", False):
self.logger.debug("P_HASH ... ")
answer["P_HASH"] = self.check_null_hash(imagehash.phash(pil_picture))
if self.fe_conf.P_HASH_SIMPLE.get("is_enabled", False):
self.logger.debug("P_HASH_SIMPLE ... ")
answer["P_HASH_SIMPLE"] = self.check_null_hash(imagehash.phash_simple(pil_picture))
if self.fe_conf.D_HASH.get("is_enabled", False):
self.logger.debug("D_HASH ... ")
answer["D_HASH"] = self.check_null_hash(imagehash.dhash(pil_picture))
if self.fe_conf.D_HASH_VERTICAL.get("is_enabled", False):
self.logger.debug("D_HASH_VERTICAL ... ")
answer["D_HASH_VERTICAL"] = self.check_null_hash(imagehash.dhash_vertical(pil_picture))
if self.fe_conf.W_HASH.get("is_enabled", False):
self.logger.debug("W_HASH ... ")
answer["W_HASH"] = self.check_null_hash(imagehash.whash(pil_picture))
if self.fe_conf.TLSH.get("is_enabled", False):
self.logger.debug("TLSH ... ")
answer["TLSH"] = self.check_null_hash(tlsh.hash(curr_picture))
except Exception as e:
self.logger.error("Error during hashing : " + str(e))
return answer
def run(self, task):
self.task = task
image = str2image(task.get_file_data)
# Calculate hash.
self.results["imghash"]["a_hash"] = str(imagehash.average_hash(image, hash_size=self.HASH_SIZE))
self.results["imghash"]["p_hash"] = str(imagehash.phash(image, hash_size=self.HASH_SIZE))
self.results["imghash"]["d_hash"] = str(imagehash.dhash(image, hash_size=self.HASH_SIZE))
# Get similar images.
self.results["similar"]["a_hash"] = self.get_similar_images(self.results["imghash"]["a_hash"], imagehash.average_hash)
self.results["similar"]["p_hash"] = self.get_similar_images(self.results["imghash"]["p_hash"], imagehash.phash)
self.results["similar"]["d_hash"] = self.get_similar_images(self.results["imghash"]["d_hash"], imagehash.dhash)
return self.results
def get_image_hash(self,file):
img = Image.open(file)
h = str(imagehash.dhash(img))
return h
# 人脸识别
def __init__(self, filepath=None, hashfunc=imagehash.dhash):
if not filepath:
return
img = Image.open(filepath)
self.hash = hashfunc(img)
def get_imagehashes(fp: Fileish,
size=FINGERPRINT_SIZE) -> Dict[str, imagehash.ImageHash]:
"""Calculate perceptual hashes for comparison of identical images"""
try:
img = pil_image(fp)
thumb = img.resize((size, size), PIL.Image.BILINEAR).convert('L')
return dict(
ahash=imagehash.average_hash(thumb),
phash=imagehash.phash(thumb),
whash=imagehash.whash(thumb),
dhash=imagehash.dhash(thumb),
)
except OSError: # corrupt image file probably
return {}
if self.is_stream and not ret:
exit = False
for i in range(ManagerCV2._tries_reconnect_stream):
ret, frame = self.video.read()
if ret:
break
if i+1 == ManagerCV2._tries_reconnect_stream:
self.stop_queue()
return
elif not ret:
self.stop_queue()
return
frame_hash = None
if self.detect_scenes:
frame_hash = imagehash.dhash(Image.fromarray(frame))
self.queue.put((frame,frame_hash))
queue_size = self.queue.qsize()
else:
# I want to wait until someone awake me
self.queue_awake.get()