forked from anminhhung/Pipeline_HCM_AI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
138 lines (100 loc) · 3.78 KB
/
app.py
File metadata and controls
138 lines (100 loc) · 3.78 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from flask import Flask, render_template, Response, request, send_file, jsonify
import cv2
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
import numpy as np
import pandas as pd
import glob
import json
from utils.query_processing import Translation
from utils.faiss import Myfaiss
# http://0.0.0.0:5001/home?index=0
# app = Flask(__name__, template_folder='templates', static_folder='static')
app = Flask(__name__, template_folder='templates')
####### CONFIG #########
with open('image_path.json') as json_file:
json_dict = json.load(json_file)
DictImagePath = {}
for key, value in json_dict.items():
DictImagePath[int(key)] = value
LenDictPath = len(DictImagePath)
bin_file='faiss_normal_ViT.bin'
MyFaiss = Myfaiss(bin_file, DictImagePath, 'cpu', Translation(), "ViT-B/32")
########################
@app.route('/home')
@app.route('/')
def thumbnailimg():
print("load_iddoc")
pagefile = []
index = int(request.args.get('index'))
if index == None:
index = 0
imgperindex = 100
# imgpath = request.args.get('imgpath') + "/"
pagefile = []
page_filelist = []
list_idx = []
if LenDictPath-1 > index+imgperindex:
first_index = index * imgperindex
last_index = index*imgperindex + imgperindex
tmp_index = first_index
while tmp_index < last_index:
page_filelist.append(DictImagePath[tmp_index])
list_idx.append(tmp_index)
tmp_index += 1
else:
first_index = index * imgperindex
last_index = LenDictPath
tmp_index = first_index
while tmp_index < last_index:
page_filelist.append(DictImagePath[tmp_index])
list_idx.append(tmp_index)
tmp_index += 1
for imgpath, id in zip(page_filelist, list_idx):
pagefile.append({'imgpath': imgpath, 'id': id})
data = {'num_page': int(LenDictPath/imgperindex)+1, 'pagefile': pagefile}
return render_template('home.html', data=data)
@app.route('/imgsearch')
def image_search():
print("image search")
pagefile = []
id_query = int(request.args.get('imgid'))
_, list_ids, _, list_image_paths = MyFaiss.image_search(id_query, k=50)
imgperindex = 100
for imgpath, id in zip(list_image_paths, list_ids):
pagefile.append({'imgpath': imgpath, 'id': int(id)})
data = {'num_page': int(LenDictPath/imgperindex)+1, 'pagefile': pagefile}
return render_template('home.html', data=data)
@app.route('/textsearch')
def text_search():
print("text search")
pagefile = []
text_query = request.args.get('textquery')
_, list_ids, _, list_image_paths = MyFaiss.text_search(text_query, k=50)
imgperindex = 100
for imgpath, id in zip(list_image_paths, list_ids):
pagefile.append({'imgpath': imgpath, 'id': int(id)})
data = {'num_page': int(LenDictPath/imgperindex)+1, 'pagefile': pagefile}
return render_template('home.html', data=data)
@app.route('/get_img')
def get_img():
# print("get_img")
fpath = request.args.get('fpath')
# fpath = fpath
list_image_name = fpath.split("/")
image_name = "/".join(list_image_name[-2:])
if os.path.exists(fpath):
img = cv2.imread(fpath)
else:
print("load 404.jph")
img = cv2.imread("./static/images/404.jpg")
img = cv2.resize(img, (1280,720))
# print(img.shape)
img = cv2.putText(img, image_name, (30, 80), cv2.FONT_HERSHEY_SIMPLEX,
3, (255, 0, 0), 4, cv2.LINE_AA)
ret, jpeg = cv2.imencode('.jpg', img)
return Response((b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n\r\n'),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0", port=5001)