-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoctopus_demo.py
126 lines (105 loc) · 3.98 KB
/
octopus_demo.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
#
# Copyright 2021-2023 Picovoice Inc.
#
# You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
# file accompanying this source.
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
import argparse
import os
import sys
import threading
import time
import pvoctopus
from tabulate import tabulate
class LoadingAnimation(threading.Thread):
def __init__(self, sleep_time_sec=0.1):
self._sleep_time_sec = sleep_time_sec
self._frames = [
". ",
".. ",
"...",
" ..",
" .",
" "
]
self._done = False
super().__init__()
def run(self):
self._done = False
while not self._done:
for frame in self._frames:
if self._done:
break
sys.stdout.write('\r' + frame)
time.sleep(self._sleep_time_sec)
def stop(self):
self._done = True
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'--access_key',
help='AccessKey provided by Picovoice Console (https://console.picovoice.ai/)',
required=True)
parser.add_argument('--audio_paths', nargs='+', help='Absolute paths to input audio files', required=True)
parser.add_argument('--library_path', help='Absolute path to dynamic library')
parser.add_argument('--model_path', help='Absolute path to the file containing model parameters')
parser.add_argument(
'--search_phrase',
help='Phrase to search in the provided audio paths')
args = parser.parse_args()
try:
octopus = pvoctopus.create(
access_key=args.access_key,
library_path=args.library_path,
model_path=args.model_path)
print("Octopus version: %s" % octopus.version)
except pvoctopus.OctopusError as e:
print(e)
sys.exit(1)
indexing_animation = LoadingAnimation()
metadata_list = list()
indexing_animation.start()
for audio_file in args.audio_paths:
try:
print("\rindexing '%s'" % os.path.basename(audio_file))
metadata_list.append(octopus.index_audio_file(os.path.abspath(audio_file)))
except pvoctopus.OctopusError as e:
print("Failed to process '%s' with '%s'" % (os.path.basename(audio_file), e))
octopus.delete()
sys.exit(1)
finally:
indexing_animation.stop()
try:
search_phrase = args.search_phrase
while True:
if args.search_phrase is None:
search_phrase = input("\rEnter search phrase (Ctrl+c to exit): ")
search_phrase = search_phrase.strip()
for i, metadata in enumerate(metadata_list):
try:
matches = octopus.search(metadata, [str(search_phrase)])
except pvoctopus.OctopusError as e:
print(e)
continue
if len(matches) != 0:
print("Matches in '%s':" % (os.path.basename(args.audio_paths[i])))
results = matches[str(search_phrase)]
result_table = list()
for result in results:
result_table.append([result.start_sec, result.end_sec, result.probability])
print(tabulate(result_table, headers=['Start time (s)', 'End time (s)', 'Probability']))
else:
print("Nothing found!")
print("\n")
if args.search_phrase is not None:
break
except KeyboardInterrupt:
print('Stopping ...')
finally:
octopus.delete()
if __name__ == '__main__':
main()