Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
vardanagarwal
GitHub Repository: vardanagarwal/Proctoring-AI
Path: blob/master/audio_part.py
453 views
1
import speech_recognition as sr
2
import pyaudio
3
import wave
4
import time
5
import threading
6
import os
7
8
def read_audio(stream, filename):
9
chunk = 1024 # Record in chunks of 1024 samples
10
sample_format = pyaudio.paInt16 # 16 bits per sample
11
channels = 2
12
fs = 44100 # Record at 44100 samples per second
13
seconds = 10 # Number of seconds to record at once
14
filename = filename
15
frames = [] # Initialize array to store frames
16
17
for i in range(0, int(fs / chunk * seconds)):
18
data = stream.read(chunk)
19
frames.append(data)
20
21
# Save the recorded data as a WAV file
22
wf = wave.open(filename, 'wb')
23
wf.setnchannels(channels)
24
wf.setsampwidth(p.get_sample_size(sample_format))
25
wf.setframerate(fs)
26
wf.writeframes(b''.join(frames))
27
wf.close()
28
# Stop and close the stream
29
stream.stop_stream()
30
stream.close()
31
32
def convert(i):
33
if i >= 0:
34
sound = 'record' + str(i) +'.wav'
35
r = sr.Recognizer()
36
37
with sr.AudioFile(sound) as source:
38
r.adjust_for_ambient_noise(source)
39
print("Converting Audio To Text and saving to file..... ")
40
audio = r.listen(source)
41
try:
42
value = r.recognize_google(audio) ##### API call to google for speech recognition
43
os.remove(sound)
44
if str is bytes:
45
result = u"{}".format(value).encode("utf-8")
46
else:
47
result = "{}".format(value)
48
49
with open("test.txt","a") as f:
50
f.write(result)
51
f.write(" ")
52
f.close()
53
54
except sr.UnknownValueError:
55
print("")
56
except sr.RequestError as e:
57
print("{0}".format(e))
58
except KeyboardInterrupt:
59
pass
60
61
p = pyaudio.PyAudio() # Create an interface to PortAudio
62
chunk = 1024 # Record in chunks of 1024 samples
63
sample_format = pyaudio.paInt16 # 16 bits per sample
64
channels = 2
65
fs = 44100
66
67
def save_audios(i):
68
stream = p.open(format=sample_format,channels=channels,rate=fs,
69
frames_per_buffer=chunk,input=True)
70
filename = 'record'+str(i)+'.wav'
71
read_audio(stream, filename)
72
73
for i in range(30//10): # Number of total seconds to record/ Number of seconds per recording
74
t1 = threading.Thread(target=save_audios, args=[i])
75
x = i-1
76
t2 = threading.Thread(target=convert, args=[x]) # send one earlier than being recorded
77
t1.start()
78
t2.start()
79
t1.join()
80
t2.join()
81
if i==2:
82
flag = True
83
if flag:
84
convert(i)
85
p.terminate()
86
87
88
from nltk.corpus import stopwords
89
from nltk.tokenize import word_tokenize
90
91
file = open("test.txt") ## Student speech file
92
data = file.read()
93
file.close()
94
stop_words = set(stopwords.words('english'))
95
word_tokens = word_tokenize(data) ######### tokenizing sentence
96
filtered_sentence = [w for w in word_tokens if not w in stop_words]
97
filtered_sentence = []
98
99
for w in word_tokens: ####### Removing stop words
100
if w not in stop_words:
101
filtered_sentence.append(w)
102
103
####### creating a final file
104
f=open('final.txt','w')
105
for ele in filtered_sentence:
106
f.write(ele+' ')
107
f.close()
108
109
##### checking whether proctor needs to be alerted or not
110
file = open("paper.txt") ## Question file
111
data = file.read()
112
file.close()
113
stop_words = set(stopwords.words('english'))
114
word_tokens = word_tokenize(data) ######### tokenizing sentence
115
filtered_questions = [w for w in word_tokens if not w in stop_words]
116
filtered_questions = []
117
118
for w in word_tokens: ####### Removing stop words
119
if w not in stop_words:
120
filtered_questions.append(w)
121
122
def common_member(a, b):
123
a_set = set(a)
124
b_set = set(b)
125
126
# check length
127
if len(a_set.intersection(b_set)) > 0:
128
return(a_set.intersection(b_set))
129
else:
130
return([])
131
132
comm = common_member(filtered_questions, filtered_sentence)
133
print('Number of common elements:', len(comm))
134
print(comm)
135
136