forked from CSAILVision/places365
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequete.py
141 lines (130 loc) · 4.09 KB
/
requete.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
import json
import time
es = Elasticsearch([{'host': 'localhost', 'port': 9200, 'scheme': 'http'}])
data= json.load(open("all_vids_combined_tuple_1.json", encoding='utf-8'))
data+= json.load(open("all_vids_combined_tuple_2.json", encoding='utf-8'))
data+= json.load(open("all_vids_combined_tuple_3.json", encoding='utf-8'))
data+= json.load(open("all_vids_combined_tuple_4.json", encoding='utf-8'))
data+= json.load(open("all_vids_combined_tuple_5.json", encoding='utf-8'))
def generate_data(data):
for video in data:
features=[]
for feat in video["features"]:
lieu = [(str(x[0]), str(x[1])) for x in feat["lieu"]]
objects= [(str(x[0]), str(x[1])) for x in feat["objects"] if feat["objects"] is not None]
bndbox= [(x[0], x[1], x[2], x[3]) for x in feat["bndbox"]]
features.append({"frame": feat["frame"], "objects": objects, "bndbox": bndbox, "lieu": lieu})
yield {
"_index": "video",
"_type": "features",
"_source": {
"idVideo": video["idVideo"],
"fps": video["fps"],
"features": features
}
}
if es.indices.exists('video')==True:
es.indices.delete(index='video')
bulk(es, generate_data(data))
else :
bulk(es, generate_data(data))
time.sleep(5)
def search_init():
result = es.search(index="video", query= {"match_all": {}},size=1000)
results = []
[results.append(elt['_source']) for elt in result["hits"]["hits"]]
return results
def search_lieu(query):
print(query)
QUERY = { "query": {
"bool": {
"must": [],
"filter": [
{
"bool": {
"should": [
{
"match_phrase": {
"features.lieu": query
}
}
],
"minimum_should_match": 1
}
}
],
"should": [],
"must_not": []
}
}
}
result = es.search(index = "video", body= QUERY, size=1000)
videos = [elt['_source'] for elt in result["hits"]["hits"]]
res = []
for video in videos :
dic_res = {}
dic_frames = {}
for frame in video['features']:
for lieu in frame['lieu']:
if query in lieu[0]:
if video['idVideo'] in dic_res:
num_frame = frame['frame']
dic_frames[num_frame] = lieu
else :
dic_res['idVideo'] = video['idVideo']
num_frame = frame['frame']
dic_frames[num_frame] = lieu
dic_res['frames'] = dic_frames
res.append(dic_res)
return res
def search_object(var_object):
QUERY = {
"query": {
"bool": {
"must": [],
"filter": [
{
"bool": {
"filter": [
{
"bool": {
"should": [
{
"match_phrase": {
"features.objects": var_object
}
}
],
"minimum_should_match": 1
}
}
]
}
}
],
"should": [],
"must_not": []
}
}
}
result = es.search(index = "video", body= QUERY, size=1000)
videos = [elt['_source'] for elt in result["hits"]["hits"]]
res = []
for video in videos :
dic_res = {}
dic_frames = {}
for frame in video['features']:
for object in frame['objects']:
if var_object in object[0]:
if video['idVideo'] in dic_res:
num_frame = frame['frame']
dic_frames[num_frame] = object
else :
dic_res['idVideo'] = video['idVideo']
num_frame = frame['frame']
dic_frames[num_frame] = object
dic_res['frames'] = dic_frames
res.append(dic_res)
return res