forked from redeye-fang/Content-based-Image-Retrieval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashes.py
61 lines (45 loc) · 1.83 KB
/
hashes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import scipy.fftpack
import streamlit as st
import os
import glob
from PIL import Image
import imagehash
import time
from functools import partial
from multiprocessing import Pool
def generateHash(image_path):
image = Image.open(image_path)
imageName = os.path.basename(image_path)
hashTemp = imagehash.phash(image)
return [imageName, str(hashTemp), image_path]
# return (image_path, str(hashTemp))
class hashes:
def __init__(self, path):
self.path = path
self.flag = False
self.Files = glob.glob(path + '/**/*.*', recursive=True)
self.image_paths = []
self.hashValues = []
for file in self.Files:
if file.endswith((".jpg")):
self.image_paths.append(file)
#### Parallel Implementation
@st.cache(hash_funcs = {st.delta_generator.DeltaGenerator: lambda _: None}, suppress_st_warning=True)
def getHashValues(self):
self.compute_hashes_parallel(self.image_paths, os.cpu_count())
def compute_hashes_serial(self):
for imagePath in self.image_paths:
self.hashValues.append(generateHash(imagePath))
if len(self.image_paths) != 0 and len(self.hashValues) == len(self.image_paths):
self.flag = True
return (self.flag, self.hashValues)
def compute_hashes_parallel(self, num_workers=12):
hashingPool = Pool(processes=num_workers)
self.hashValues = hashingPool.map(partial(generateHash), self.image_paths)
hashingPool.close()
hashingPool.join()
# for imageName, hashTemp, image_path in tmp_list:
# self.hashValues.append([imageName, hashTemp, image_path])
if len(self.image_paths) != 0 and len(self.hashValues) == len(self.image_paths):
self.flag = True
return (self.flag, self.hashValues)