-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathImageSearch_parallel.py
65 lines (46 loc) · 1.73 KB
/
ImageSearch_parallel.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
62
63
64
65
import os
import sys
import threading
import concurrent.futures
import time
import cv2
import matplotlib.pyplot as plt
from FeatureVectors import FeatureVectors
from QuerySearch import QuerySearch
def extractFeatureVectors(image_path):
# Extracts feature vectors for input image
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (500, 500))
featureVectors = FeatureVectors(image)
vectors = featureVectors.getFeatureVector()
imageName = image_path.split("/")[-1]
return [imageName, vectors]
def ThreadedFeatureExtraction(images_list):
# Performing feature extraction of databse images using multithreading
features = {}
with concurrent.futures.ThreadPoolExecutor() as executor:
results = [executor.submit(extractFeatureVectors, image_path)
for image_path in images_list]
for f in concurrent.futures.as_completed(results):
imageName, vectors = f.result()
features[imageName] = vectors
return features
def getImg(img):
image_db_path = "Image_Database/"
image = cv2.imread(image_db_path+img)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
return image
def ImageSearch(queryImage):
# Performs Image Search using Query image
image_db_path = "Image_Database/"
image_paths = []
for img in os.listdir(image_db_path):
image_paths.append(image_db_path+img)
features = ThreadedFeatureExtraction(image_paths)
queryImage_path = image_db_path+queryImage
imageName, queryVector = extractFeatureVectors(queryImage_path)
search = QuerySearch(queryVector, features)
results = search.performSearch()
results.sort(key=lambda res: res[1])
return results