-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
64 lines (49 loc) · 1.89 KB
/
main.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
from conversions import *
from indexer import *
from audio_parser import *
from image_parser import *
import math
import sys
path = './input'
es = new_search_engine()
if not es.ping():
print("ElasticSearch is not running!")
exit()
if len(sys.argv) > 1 and ("-p" in sys.argv or "--parse" in sys.argv):
print("Initializing Models...")
image_model = new_image_model()
audio_model = new_audio_model()
print("Beginning Indexing...")
for file_name in os.listdir(path):
audio_file = extract_audio(path + '/' + file_name)
phrases = parse_file(audio_model, audio_file)
documents = []
for phrase in phrases:
if "result" in phrase:
timestamp = seconds_to_timestamp(math.ceil(float(phrase["result"][0]["start"])))
image = extract_image(file_name=path + '/' + file_name, timestamp=timestamp)
res = detect_objects(image_model, image)
subdocument = {
"Timestamp": phrase["result"][0]["start"],
"Audio": phrase["text"],
"Filename": file_name,
"Image": " ".join([i['name'] for i in res])
}
documents.append(subdocument)
index_documents(es, documents)
clean_up_audio()
clean_up_image()
print("Search engine ready!")
get_full_document = False
if "-f" in sys.argv or "--full" in sys.argv:
get_full_document = True
while True:
query = input("Enter a query:")
res = search_documents(es, query, get_full_document=get_full_document)
output = [{"Timestamp": i["_source"]["Timestamp"],
"Audio": i["_source"]["Audio"],
"Image": i["_source"]["Image"],
"File": i["_source"]["Filename"]}
for i in res['hits']['hits']]
for i in range(len(output)):
print("#" + str(i + 1) + ": " + str(output[i]))