-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
118 lines (107 loc) · 4.42 KB
/
server.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import numpy as np
from PIL import Image
from DensenetMod import FeatureExtractorD
from EfficientNetMod import FeatureExtractorE
from MobileNetMod import FeatureExtractorM
from ConvNextmod import extract_features
from convNextKerasmod import FeatureExtractorConv
from datetime import datetime
from flask import Flask, request, render_template
from pathlib import Path
from io import FileIO
from time import time
app = Flask(__name__)
# Read image features
DenseFeature = FeatureExtractorD()
EfficientFeature = FeatureExtractorE()
MobileFeature = FeatureExtractorM()
ConvKerasFeature = FeatureExtractorConv()
DenseDF = []
EffiDF=[]
DenseEffDF=[]
MobiDF=[]
convDF=[]
convKerasDF=[]
img_paths = []
for feature_path in Path("./static/convNet").glob("*.npy"):
convDF.append(np.load(feature_path))
convDF = np.array(convDF)
for feature_path in Path("./static/convNetKeras").glob("*.npy"):
convKerasDF.append(np.load(feature_path))
convKerasDF = np.array(convKerasDF)
for feature_path in Path("./static/DenseNetFeatures").glob("*.npy"):
DenseDF.append(np.load(feature_path))
# img_paths.append(Path("./static/image") / (feature_path.stem + ".jpeg"))
DenseDF = np.array(DenseDF)
for feature_path in Path("./static/EfficientNetFeatures").glob("*.npy"):
EffiDF.append(np.load(feature_path))
# img_paths.append(Path("./static/image") / (feature_path.stem + ".jpeg"))
EffiDF = np.array(EffiDF)
for feature_path in Path("./static/DenseEffNetFeatures").glob("*.npy"):
DenseEffDF.append(np.load(feature_path))
# img_paths.append(Path("./static/image") / (feature_path.stem + ".jpeg"))
DenseEffDF = np.array(DenseEffDF)
for feature_path in Path("./static/MobileNetFeatures").glob("*.npy"):
MobiDF.append(np.load(feature_path))
# img_paths.append(Path("./static/image") / (feature_path.stem + ".jpeg"))
MobiDF = np.array(MobiDF)
for img in Path("./static/image").glob("*.jpeg"):
img_paths.append(img)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
start = time()
file = request.files['query_img']
scope = request.form.get('scope')
FeatureModel = request.form.get('feature_select')
# Save query image
img = Image.open(file.stream) # PIL image
uploaded_img_path = "static/uploaded/" + datetime.now().isoformat().replace(":", ".") + "_" + file.filename
img.save(uploaded_img_path)
dists=0
if FeatureModel=='DenseNet':
query = DenseFeature.extract(img)
dists = np.linalg.norm(DenseDF-query, axis=1) # L2 distances to features
elif FeatureModel=='EfficientNet':
query=EfficientFeature.extract(img)
dists = np.linalg.norm(EffiDF-query, axis=1) # L2 distances to features
elif FeatureModel=='MobileNet':
query=MobileFeature.extract(img)
dists = np.linalg.norm(MobiDF-query, axis=1) # L2 distances to features
elif FeatureModel=='DenseEffNet':
queryA = DenseFeature.extract(img)
queryB=EfficientFeature.extract(img)
query=np.concatenate((queryA,queryB))
dists = np.linalg.norm(np.subtract(DenseEffDF,query), axis=1) # L2 distances to features
elif FeatureModel=='ConvNext':
query=extract_features(img)
dists = np.linalg.norm(convDF-query, axis=1) # L2 distances to features
elif FeatureModel=='ConvNextKeras':
query=ConvKerasFeature.extract(img)
dists = np.linalg.norm(convKerasDF-query, axis=1) # L2 distances to features
# Run search
scope=int(scope)
ids = np.argsort(dists)[1:scope+1] # Top 10 results
scores = [(dists[id], img_paths[id]) for id in ids]
name_path=[]
for id in scores:
nam=id[1]
nam=repr(nam)
idx=nam.index("_")
nam=nam[26:idx]
name_path.append(nam)
inx=uploaded_img_path.index("_")
inp=uploaded_img_path[inx+1:]
inx=inp.index("_")
inp=inp[0:inx]
c=name_path.count(inp)
recall = c/200
precision=c/scope
speed = time()-start
return render_template('index.html',
query_path=uploaded_img_path,speed=speed,
scores=scores,mod=FeatureModel,precision=precision,recall=recall)
else:
return render_template('index.html')
if __name__=="__main__":
app.run("0.0.0.0",port=5002)