Commit 05ac3132 authored by I.K Seneviratne's avatar I.K Seneviratne

Merge branch 'summarization_changes' into 'QA_RELEASE'

Summarization changes

See merge request !3
parents a999f5c4 ade8f9f2
...@@ -4,4 +4,7 @@ ...@@ -4,4 +4,7 @@
<option name="languageLevel" value="ES6" /> <option name="languageLevel" value="ES6" />
</component> </component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7" project-jdk-type="Python SDK" />
<component name="PyCharmProfessionalAdvertiser">
<option name="shown" value="true" />
</component>
</project> </project>
\ No newline at end of file
from django.contrib import admin from django.contrib import admin
from .models import Student, Attendance
admin.site.register(Student)
admin.site.register(Attendance)
# Register your models here. # Register your models here.
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.parsers import JSONParser
from .models import Student, Subject, Attendance
from .serializers import StudentSerializer, SubjectSerializer, AttendanceSerializer, FileSerializer
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.parsers import MultiPartParser, FormParser
from . import record
from rest_framework.views import *
class StudentAPIView(APIView):
def get(self, request):
students = Student.objects.all()
serializer = StudentSerializer(students, many=True)
return Response(serializer.data)
def post(self, request):
serializer = StudentSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class StudentDetails(APIView):
def get_object(self, pk):
try:
return Student.objects.get(studentId=pk)
except Student.DoesNotExist:
return HttpResponse(status=status.HTTP_404_NOT_FOUND)
def get(self, request, pk):
student = self.get_object(pk)
serializer = StudentSerializer(student)
return Response(serializer.data)
def put(self, request, pk):
student = self.get_object(pk)
serializer = StudentSerializer(student, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, pk):
student = self.get_object(pk)
student.delete(student)
return HttpResponse(status=status.HTTP_204_NO_CONTENT)
@api_view(['GET', 'POST'])
def student_list(request):
if request.method == 'GET':
students = Student.objects.all()
serializer = StudentSerializer(students, many=True)
return Response(serializer.data)
elif request.method == 'POST':
serializer = StudentSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@api_view(['GEt', 'PUT', 'DELETE'])
def student_detail(request, pk):
try:
student = Student.objects.get(studentId=pk)
except Student.DoesNotExist:
return HttpResponse(status=status.HTTP_404_NOT_FOUND)
if request.method == 'GET':
serializer = StudentSerializer(student)
return Response(serializer.data)
elif request.method == 'PUT':
serializer = StudentSerializer(student, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
elif request.method == 'DELETE':
student.delete()
return HttpResponse(status=status.HTTP_204_NO_CONTENT)
@api_view(['GET', 'POST'])
def subject_list(request):
if request.method == 'GET':
subjects = Subject.objects.all()
serializer = SubjectSerializer(subjects, many=True)
return Response(serializer.data)
elif request.method == 'POST':
serializer = SubjectSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return JsonResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@csrf_exempt
def subject_detail (request, pk):
try:
subject = Subject.objects.get(subjectId=pk)
except subject.DoesNotExist:
return HttpResponse(status=404)
if request.method == 'GET':
serializer = SubjectSerializer(subject)
return JsonResponse(serializer.data)
elif request.method == 'PUT':
data = JSONParser().parse(request)
serializer = SubjectSerializer(subject, data=data)
if serializer.is_valid():
serializer.save()
return JsonResponse(serializer.data)
return JsonResponse(serializer.errors, status=400)
elif request.method == 'DELETE':
subject.delete()
return HttpResponse(status=204)
@api_view(['GET', 'POST'])
def attendance_list(request):
if request.method == 'GET':
attendance = Attendance.objects.all()
serializer = AttendanceSerializer(attendance, many=True)
return Response(serializer.data)
elif request.method == 'POST':
serializer = AttendanceSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return JsonResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class FileView(APIView):
# parser_classes = (MultiPartParser, FormParser)
def post(self, request, *args, **kwargs):
file_serializer = FileSerializer(data=request.data)
if file_serializer.is_valid():
file_serializer.save()
return Response(file_serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
# this API will initiate the lecture
class InitiateLecture(APIView):
def get(self, request):
record.initiate()
return Response({
"response": "success"
})
# Generated by Django 2.2.12 on 2020-09-23 11:08
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
('FirstApp', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='File',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('file', models.FileField(upload_to='')),
('remark', models.CharField(max_length=20)),
('timestamp', models.DateTimeField(auto_now_add=True)),
],
),
migrations.CreateModel(
name='Student',
fields=[
('studentId', models.CharField(max_length=10, primary_key=True, serialize=False)),
('studentFirstName', models.CharField(max_length=100)),
('studentLastName', models.CharField(max_length=100)),
('password', models.CharField(max_length=100)),
('year', models.CharField(max_length=100)),
('semester', models.CharField(max_length=100)),
('batch', models.CharField(max_length=100)),
('faculty', models.CharField(max_length=100)),
],
),
migrations.CreateModel(
name='Lecture',
fields=[
('lectureID', models.CharField(max_length=10, primary_key=True, serialize=False)),
('startTime', models.DateField()),
('endTime', models.DateField()),
('day', models.CharField(max_length=20)),
('subject', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='FirstApp.Subject')),
],
),
migrations.CreateModel(
name='Attendance',
fields=[
('attendanceID', models.CharField(max_length=10, primary_key=True, serialize=False)),
('date', models.DateField()),
('attendance', models.BooleanField()),
('feedback', models.CharField(blank=True, max_length=50, null=True)),
('student', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='AttendanceApp.Student')),
('subject', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='FirstApp.Subject')),
],
),
]
# from django.db import models
from djongo import models
from FirstApp.MongoModels import Subject
class Student(models.Model):
studentId = models.CharField(primary_key=True, max_length=10)
studentFirstName = models.CharField(max_length=100)
studentLastName = models.CharField(max_length=100)
password = models.CharField(max_length=100)
year = models.CharField(max_length=100)
semester = models.CharField(max_length=100)
batch = models.CharField(max_length=100)
faculty = models.CharField(max_length=100)
def __str__(self):
return self.studentId
# class Subject(models.Model):
# subjectId = models.CharField(primary_key=True, max_length=10)
# subjectName = models.CharField(max_length=100)
# LecturerInCharge = models.CharField(max_length=100)
#
# def __str__(self):
# return self.subjectId
class Attendance(models.Model):
attendanceID = models.CharField(primary_key=True, max_length=10)
student = models.ForeignKey(Student, on_delete=models.CASCADE)
subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
date = models.DateField()
attendance = models.BooleanField()
feedback = models.CharField(max_length=50, null=True, blank=True)
def __str__(self):
return self.attendanceID
class File(models.Model):
file = models.FileField(blank=False, null=False)
remark = models.CharField(max_length=20)
timestamp = models.DateTimeField(auto_now_add=True)
class Lecture(models.Model):
lectureID = models.CharField(primary_key=True, max_length=10)
subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
startTime = models.DateField()
endTime = models.DateField()
day = models.CharField(max_length=20)
from django.db import models from django.db import models
# Create your models here. # Create your models here.
from rest_framework import serializers
from FirstApp.serializers import SubjectSerializer
from .models import Student, Subject, Attendance, File
class StudentSerializer(serializers.ModelSerializer):
class Meta:
model = Student
fields = '__all__'
#
# class SubjectSerializer(serializers.ModelSerializer):
# class Meta:
# model = Subject
# fields = '__all__'
class AttendanceSerializer(serializers.ModelSerializer):
subject = SubjectSerializer()
class Meta:
model = Attendance
fields = '__all__'
class FileSerializer(serializers.ModelSerializer):
class Meta():
model = File
fields = ('file', 'remark', 'timestamp')
from django.urls import path, re_path, include from django.urls import path
from .api import student_list, student_detail, subject_list, subject_detail, attendance_list, StudentAPIView, \
StudentDetails
from django.conf.urls import url from django.conf.urls import url
from .api import FileView, InitiateLecture
from . import views from . import views
urlpatterns = [ urlpatterns = [
path('', views.first) path('students/', student_list),
path('students/<str:pk>', student_detail),
path('subjects/', subject_list),
path('subjects/<str:pk>', subject_detail),
path('attendance/', attendance_list),
path('student/', StudentAPIView.as_view()),
path('initiate-lecture', views.initiate_lecture),
# class based
path('student/', StudentAPIView.as_view()),
path('student/<str:pk>', StudentDetails.as_view()),
url(r'^upload/$', FileView.as_view(), name='file-upload'),
# this url will initiate the lecture
url(r'^process-initiate-lecture/$', InitiateLecture.as_view())
] ]
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse
# Create your views here. def initiate_lecture(request):
def first(request):
return HttpResponse('<h1>Hello Attendance App</h1>') return render(request, "AttendanceApp/Initiate_lecture.html")
\ No newline at end of file
...@@ -83,6 +83,33 @@ ...@@ -83,6 +83,33 @@
</div> </div>
</li> </li>
<!-- Nav Item - Pages Collapse Menu -->
<li class="nav-item">
<a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseThree" aria-expanded="true" aria-controls="collapseThree">
<i class="fas fa-fw fa-cog"></i>
<span>Lecture</span>
</a>
<div id="collapseThree" class="collapse" aria-labelledby="headingThree" data-parent="#accordionSidebar">
<div class="bg-white py-2 collapse-inner rounded">
<h6 class="collapse-header">Components:</h6>
<a class="collapse-item" href="/summary/lecture">Summarization</a>
</div>
</div>
</li>
<li class="nav-item">
<a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseFour" aria-expanded="true" aria-controls="collapseThree">
<i class="fas fa-fw fa-cog"></i>
<span>Attendance</span>
</a>
<div id="collapseFour" class="collapse" aria-labelledby="headingThree" data-parent="#accordionSidebar">
<div class="bg-white py-2 collapse-inner rounded">
<h6 class="collapse-header">Components:</h6>
<a class="collapse-item" href="/attendance/initiate-lecture">initiate lecture</a>
</div>
</div>
</li>
<!-- Nav Item - Utilities Collapse Menu --> <!-- Nav Item - Utilities Collapse Menu -->
<li class="nav-item"> <li class="nav-item">
<a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseUtilities" aria-expanded="true" aria-controls="collapseUtilities"> <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#collapseUtilities" aria-expanded="true" aria-controls="collapseUtilities">
......
import nltk
read_lines = [line.rstrip('\n') for line in open("audioToText01.txt", "r")]
sentences_list = []
sentence_list = nltk.sent_tokenize(read_lines)
word_search = "important"
sentences_with_word = []
for sentence in sentences_list:
if sentence.count(word_search)>0:
sentences_with_word.append(sentence)
words_search = ["exam", "assignment"]
word_sentence_dictionary = {"exam":[],"assignment":[]}
for word in words_search:
sentences_with_word = []
for sentence in sentences_list:
if sentence.count(word)>0:
sentences_with_word.append(sentence)
word_sentence_dictionary[word] = sentences_with_word
\ No newline at end of file
import spacy
from spacy.lang.pt.stop_words import STOP_WORDS
from sklearn.feature_extraction.text import CountVectorizer
import pt_core_news_sm
nlp = pt_core_news_sm.load()
with open("audioToText01.txt", "r", encoding="utf-8") as f:
text = " ".join(f.readlines())
doc = nlp(text)
corpus = [sent.text.lower() for sent in doc.sents ]
cv = CountVectorizer(stop_words=list(STOP_WORDS))
cv_fit=cv.fit_transform(corpus)
word_list = cv.get_feature_names()
count_list = cv_fit.toarray().sum(axis=0)
word_frequency = dict(zip(word_list,count_list))
val=sorted(word_frequency.values())
higher_word_frequencies = [word for word,freq in word_frequency.items() if freq in val[-3:]]
print("\nWords with higher frequencies: ", higher_word_frequencies)
# gets relative frequency of words
higher_frequency = val[-1]
for word in word_frequency.keys():
word_frequency[word] = (word_frequency[word]/higher_frequency)
sentence_rank={}
for sent in doc.sents:
for word in sent :
if word.text.lower() in word_frequency.keys():
if sent in sentence_rank.keys():
sentence_rank[sent]+=word_frequency[word.text.lower()]
else:
sentence_rank[sent]=word_frequency[word.text.lower()]
top_sentences=(sorted(sentence_rank.values())[::-1])
top_sent=top_sentences[:3]
summary=[]
for sent,strength in sentence_rank.items():
if strength in top_sent:
summary.append(sent)
else:
continue
for i in summary:
file = open('Summary01.txt', 'w')
file.write(str(i))
file.close()
\ No newline at end of file
...@@ -4,4 +4,7 @@ from django.contrib import admin ...@@ -4,4 +4,7 @@ from django.contrib import admin
from LectureSummarizingApp.models import * from LectureSummarizingApp.models import *
admin.site.register(LectureAudio) admin.site.register(LectureAudio)
admin.site.register(LectureAudioNoiseRemoved)
admin.site.register(LectureSpeechToText)
admin.site.register(LectureNotices)
admin.site.register(LectureAudioSummary) admin.site.register(LectureAudioSummary)
from rest_framework.views import APIView from rest_framework.views import APIView
from rest_framework.response import Response from rest_framework.response import Response
from LectureSummarizingApp.models import LectureAudio from LectureSummarizingApp.models import LectureAudio, LectureAudioNoiseRemoved, LectureSpeechToText, \
from LectureSummarizingApp.serializer import LectureAudioSerializer LectureAudioSummary, LectureNotices
from LectureSummarizingApp.serializer import LectureAudioSerializer, LectureAudioNoiseRemovedSerializer, \
LectureSpeechToTextSerializer, LectureAudioSummarySerializer, LectureNoticesSerializer
from . import speech_to_text as stt
# this API will retrieve lecture audio details # this API will retrieve lecture audio details
class LectureAudioAPI(APIView): class LectureAudioAPI(APIView):
def get(self, request): def get(self, request):
lecture_audio = LectureAudio.objects.all() lecture_audio = LectureAudio.objects.all().order_by('lecturer_date')
lecture_audio_serializer = LectureAudioSerializer(lecture_audio, many=True) lecture_audio_serializer = LectureAudioSerializer(lecture_audio, many=True)
return Response(lecture_audio_serializer.data)
class audioNoiseRemovedList(APIView):
def get(self, request):
lecture_audio_noise_removed = LectureAudioNoiseRemoved.objects.all()
serializer = LectureAudioNoiseRemovedSerializer(lecture_audio_noise_removed, many=True)
return Response(serializer.data)
def post(self, request):
LectureAudioNoiseRemoved(
lecture_audio_noise_removed_id=request.data["lecture_audio_noise_removed_id"],
lecture_audio_id=request.data["lecture_audio_id"],
lecturer_date=request.data["lecturer_date"],
lecture_audio_name=request.data["lecture_audio_name"],
lecture_audio_length=request.data["lecture_audio_length"]
).save()
return Response({"response": request.data})
data = lecture_audio_serializer.data class audioToTextList(APIView):
def get(self, request):
lecture_speech_to_text_id = LectureSpeechToText.objects.all()
serializer = LectureSpeechToTextSerializer(lecture_speech_to_text_id, many=True)
# return Response(serializer.data)
video_name = request.query_params.get("video_name")
print('video name: ', video_name)
stt.speech_to_text(video_name)
return Response({ return Response({
"response": data "response": "successful"
}) })
def post(self, request):
# video_name = request.data["video_name"]
#
# print('video name: ', video_name)
#
# stt.speech_to_text(video_name)
LectureSpeechToText(
lecture_speech_to_text_id=request.data["lecture_speech_to_text_id"],
lecture_audio_id=request.data["lecture_audio_id"],
audio_original_text=request.data["audio_original_text"]
).save()
return Response({"response": request.data})
class lectureSummaryList(APIView):
def get(self, request):
lecture_audio_summary_id = LectureAudioSummary.objects.all()
serializer = LectureAudioSummarySerializer(lecture_audio_summary_id, many=True)
return Response(serializer.data)
def post(self, request):
LectureAudioSummary(
lecture_speech_to_text_id=request.data["lecture_speech_to_text_id"],
lecture_audio_id=request.data["lecture_audio_id"],
audio_original_text=request.data["audio_original_text"],
audio_summary=request.data["audio_summary"]
).save()
return Response({"response": request.data})
class lectureNoticeList(APIView):
def get(self, request):
lecture_notice_id = LectureNotices.objects.all()
serializer = LectureNoticesSerializer(lecture_notice_id, many=True)
return Response(serializer.data)
def post(self, request):
LectureNotices(
lecture_notice_id=request.data["lecture_notice_id"],
lecture_audio_id=request.data["lecture_audio_id"],
notice_text=request.data["notice_text"]
).save()
return Response({"response": request.data})
error
\ No newline at end of file
# Generated by Django 2.2.12 on 2020-09-22 18:21
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('FirstApp', '0001_initial'),
('LectureSummarizingApp', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='LectureAudioNoiseRemoved',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('lecture_audio_id', models.CharField(max_length=10)),
('lecturer_date', models.DateField()),
('lecture_audio_name', models.CharField(max_length=50)),
('lecture_audio_length', models.DurationField()),
('lecturer', models.ForeignKey(default=0, on_delete=django.db.models.deletion.CASCADE, to='FirstApp.Lecturer')),
('subject', models.ForeignKey(default=0, on_delete=django.db.models.deletion.CASCADE, to='FirstApp.Subject')),
],
),
]
# Generated by Django 2.2.12 on 2020-09-22 18:32
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('LectureSummarizingApp', '0002_lectureaudionoiseremoved'),
]
operations = [
migrations.RenameField(
model_name='lectureaudionoiseremoved',
old_name='lecture_audio_id',
new_name='lecture_audio_noise_removed_id',
),
]
# Generated by Django 2.2.12 on 2020-09-23 04:34
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('LectureSummarizingApp', '0003_auto_20200923_0002'),
]
operations = [
migrations.RemoveField(
model_name='lectureaudionoiseremoved',
name='lecturer',
),
migrations.RemoveField(
model_name='lectureaudionoiseremoved',
name='subject',
),
migrations.AddField(
model_name='lectureaudionoiseremoved',
name='lecture_audio_id',
field=models.ForeignKey(default=0, on_delete=django.db.models.deletion.CASCADE, to='LectureSummarizingApp.LectureAudio'),
),
migrations.CreateModel(
name='LectureSpeechToText',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('lecture_speech_to_text_id', models.CharField(max_length=10)),
('audio_original_text', models.TextField()),
('lecture_audio_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='LectureSummarizingApp.LectureAudio')),
],
),
migrations.CreateModel(
name='LectureNotices',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('lecture_notice_id', models.CharField(max_length=10)),
('notice_text', models.TextField()),
('lecture_audio_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='LectureSummarizingApp.LectureAudio')),
],
),
]
...@@ -16,6 +16,27 @@ class LectureAudio (models.Model): ...@@ -16,6 +16,27 @@ class LectureAudio (models.Model):
def __str__(self): def __str__(self):
return self.lecture_audio_id return self.lecture_audio_id
class LectureAudioNoiseRemoved (models.Model):
lecture_audio_noise_removed_id = models.CharField(max_length=10)
lecture_audio_id = models.ForeignKey(LectureAudio, on_delete=models.CASCADE, default=0)
lecturer_date = models.DateField()
lecture_audio_name = models.CharField(max_length=50)
lecture_audio_length = models.DurationField()
def __str__(self):
return self.lecture_audio_noise_removed_id
class LectureSpeechToText (models.Model):
lecture_speech_to_text_id = models.CharField(max_length=10)
lecture_audio_id = models.ForeignKey(LectureAudio, on_delete=models.CASCADE)
audio_original_text = models.TextField()
def __str__(self):
return self.lecture_speech_to_text_id
class LectureAudioSummary (models.Model): class LectureAudioSummary (models.Model):
lecture_audio_summary_id = models.CharField(max_length=10) lecture_audio_summary_id = models.CharField(max_length=10)
lecture_audio_id = models.ForeignKey(LectureAudio, on_delete=models.CASCADE) lecture_audio_id = models.ForeignKey(LectureAudio, on_delete=models.CASCADE)
...@@ -24,3 +45,13 @@ class LectureAudioSummary (models.Model): ...@@ -24,3 +45,13 @@ class LectureAudioSummary (models.Model):
def __str__(self): def __str__(self):
return self.lecture_audio_summary_id return self.lecture_audio_summary_id
class LectureNotices (models.Model):
lecture_notice_id = models.CharField(max_length=10)
lecture_audio_id = models.ForeignKey(LectureAudio, on_delete=models.CASCADE)
notice_text = models.TextField()
def __str__(self):
return self.lecture_notice_id
import librosa
from pysndfx import AudioEffectsChain
import numpy as np
import math
import python_speech_features
import scipy as sp
from scipy import signal
import soundfile
def read_file(file_name):
sample_file = file_name
sample_directory = 'lectures/'
sample_path = sample_directory + sample_file
# generating audio time series and a sampling rate (int)
y, sr = librosa.load(sample_path)
return y, sr
# '''CENTROID'''
#
# def reduce_noise_centroid_s(y, sr):
#
# cent = librosa.feature.spectral_centroid(y=y, sr=sr)
# threshold_h = np.max(cent)
# threshold_l = np.min(cent)
# less_noise = AudioEffectsChain().lowshelf(gain=-12.0, frequency=threshold_l, slope=0.5).highshelf(gain=-12.0, frequency=threshold_h, slope=0.5).limiter(gain=6.0)
# y_cleaned = less_noise(y)
# return y_cleaned
'''MFCC'''
def mffc_highshelf(y, sr):
mfcc = python_speech_features.base.mfcc(y)
mfcc = python_speech_features.base.logfbank(y)
mfcc = python_speech_features.base.lifter(mfcc)
sum_of_squares = []
index = -1
for r in mfcc:
sum_of_squares.append(0)
index = index + 1
for n in r:
sum_of_squares[index] = sum_of_squares[index] + n**2
strongest_frame = sum_of_squares.index(max(sum_of_squares))
hz = python_speech_features.base.mel2hz(mfcc[strongest_frame])
max_hz = max(hz)
min_hz = min(hz)
speech_booster = AudioEffectsChain().highshelf(frequency=min_hz*(-1)*1.2, gain=-12.0, slope=0.6).limiter(gain=8.0)
y_speach_boosted = speech_booster(y)
return (y_speach_boosted)
def mfcc_lowshelf(y, sr):
mfcc = python_speech_features.base.mfcc(y)
mfcc = python_speech_features.base.logfbank(y)
mfcc = python_speech_features.base.lifter(mfcc)
sum_of_squares = []
index = -1
for r in mfcc:
sum_of_squares.append(0)
index = index + 1
for n in r:
sum_of_squares[index] = sum_of_squares[index] + n**2
strongest_frame = sum_of_squares.index(max(sum_of_squares))
hz = python_speech_features.base.mel2hz(mfcc[strongest_frame])
max_hz = max(hz)
min_hz = min(hz)
speech_booster = AudioEffectsChain().lowshelf(frequency=min_hz*(-1), gain=12.0, slope=0.5)
y_speach_boosted = speech_booster(y)
return (y_speach_boosted)
def trim_silence(y):
y_trimmed, index = librosa.effects.trim(y, top_db=20, frame_length=2, hop_length=500)
trimmed_length = librosa.get_duration(y) - librosa.get_duration(y_trimmed)
return y_trimmed, trimmed_length
def enhance(y):
apply_audio_effects = AudioEffectsChain().lowshelf(gain=10.0, frequency=260, slope=0.1).reverb(reverberance=25, hf_damping=5, room_scale=5, stereo_depth=50, pre_delay=20, wet_gain=0, wet_only=False)#.normalize()
y_enhanced = apply_audio_effects(y)
return y_enhanced
def output_file(destination ,filename, y, sr, ext=""):
destination = destination + filename[:-4] + ext + '.wav'
librosa.output.write_wav(destination, y, sr)
lectures = ['Lecture01.wav']
for s in lectures:
filename = s
y, sr = read_file(filename)
# y_reduced_centroid_s = reduce_noise_centroid_s(y, sr)
y_reduced_mfcc_lowshelf = mfcc_lowshelf(y, sr)
y_reduced_mfcc_highshelf = mffc_highshelf(y, sr)
# trimming silences
# y_reduced_centroid_s, time_trimmed = trim_silence(y_reduced_centroid_s)
y_reduced_mfcc_up, time_trimmed = trim_silence(mfcc_lowshelf)
y_reduced_mfcc_down, time_trimmed = trim_silence(mffc_highshelf)
# output_file('lectures_trimmed_noise_reduced/' ,filename, y_reduced_centroid_s, sr, '_ctr_s')
output_file('lectures_trimmed_noise_reduced/' ,filename, y_reduced_mfcc_up, sr, '_mfcc_up')
# output_file('lectures_trimmed_noise_reduced/' ,filename, y_reduced_mfcc_down, sr, '_mfcc_down')
# output_file('lectures_trimmed_noise_reduced/' ,filename, y, sr, '_org')
...@@ -14,9 +14,37 @@ class LectureAudioSerializer(serializers.ModelSerializer): ...@@ -14,9 +14,37 @@ class LectureAudioSerializer(serializers.ModelSerializer):
fields = '__all__' fields = '__all__'
class LectureAudioNoiseRemovedSerializer(serializers.ModelSerializer):
lecture_audio_id = LectureAudioSerializer()
class Meta:
model = LectureAudioNoiseRemoved
fields = '__all__'
class LectureSpeechToTextSerializer(serializers.ModelSerializer):
# lecture_speech_to_text_id = LectureAudioNoiseRemovedSerializer()
lecture_audio_id = LectureAudioSerializer()
class Meta:
model = LectureSpeechToText
fields = '__all__'
class LectureAudioSummarySerializer(serializers.ModelSerializer): class LectureAudioSummarySerializer(serializers.ModelSerializer):
# lecture_audio_noise_removed_id = LectureSpeechToTextSerializer()
lecture_audio_id = LectureAudioSerializer() lecture_audio_id = LectureAudioSerializer()
class Meta: class Meta:
model = LectureAudioSummary model = LectureAudioSummary
fields = '__all__' fields = '__all__'
class LectureNoticesSerializer(serializers.ModelSerializer):
# lecture_audio_noise_removed_id = LectureSpeechToTextSerializer()
lecture_audio_id = LectureAudioSerializer()
class Meta:
# model = LectureAudioSummary
model = LectureNotices
fields = '__all__'
\ No newline at end of file
import speech_recognition as sr
import os
def speech_to_text(video_name):
r = sr.Recognizer()
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
VIDEO_PATH = os.path.join(BASE_DIR, "lectures\\{}".format(video_name))
with sr.AudioFile(VIDEO_PATH) as source:
audio = r.listen(source)
file = open('audioToText01.txt', 'w')
try:
text = r.recognize_google(audio)
file.write(text)
except:
file.write('error')
file.close()
\ No newline at end of file
...@@ -8,6 +8,7 @@ router = routers.DefaultRouter() ...@@ -8,6 +8,7 @@ router = routers.DefaultRouter()
# router.register(r'^register', views.register) # router.register(r'^register', views.register)
urlpatterns = [ urlpatterns = [
path('lecture', views.summarization),
# path('', views.hello), # path('', views.hello),
# path('login', views.login), # path('login', views.login),
# path('register', views.register), # path('register', views.register),
...@@ -25,7 +26,15 @@ urlpatterns = [ ...@@ -25,7 +26,15 @@ urlpatterns = [
# # path('Video', views.hello) # # path('Video', views.hello)
# API to retrieve activity recognition # API to retrieve activity recognition
url(r'^get-lecture-audio/$', api.LectureAudioAPI.as_view()), url(r'^lecture-audio/$', api.LectureAudioAPI.as_view()),
url(r'^lecture-audio-noise-removed/$', api.audioNoiseRemovedList.as_view()),
url(r'^lecture-audio-to-text/', api.audioToTextList.as_view()),
url(r'^lecture-summary/$', api.lectureSummaryList.as_view()),
url(r'^lecture-notices/$', api.lectureNoticeList.as_view()),
# # API to retrieve audio analysis # # API to retrieve audio analysis
# url(r'^get-audio-analysis', api.GetLectureAudioAnalysis.as_view()), # url(r'^get-audio-analysis', api.GetLectureAudioAnalysis.as_view()),
......
from django.shortcuts import render from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import viewsets
from .models import LectureAudio, LectureAudioNoiseRemoved, LectureSpeechToText, LectureAudioSummary, LectureNotices
from .serializer import LectureAudioSerializer, LectureAudioNoiseRemovedSerializer, LectureAudioSummarySerializer, \
LectureSpeechToTextSerializer, LectureNoticesSerializer
# Create your views here. # Create your views here.
def summarization(request):
lec_audio = LectureAudio.objects.all()
lec_audio_serializer = LectureAudioSerializer(lec_audio, many=True)
data = lec_audio_serializer.data
lec_noiseless_audio = LectureAudioNoiseRemoved.objects.all()
lec_noiseless_audio_ser = LectureAudioNoiseRemovedSerializer(lec_noiseless_audio, many=True)
noiseless_data = lec_noiseless_audio_ser.data
lec_text = LectureSpeechToText.objects.all()
lec_text_ser = LectureSpeechToTextSerializer(lec_text, many=True)
lecture_text_data = lec_text_ser.data
lec_summary = LectureAudioSummary.objects.all()
lec_summary_ser = LectureAudioSummarySerializer(lec_summary, many=True)
lec_summary_data = lec_summary_ser.data
lec_notice = LectureNotices.objects.all()
lec_notice_ser = LectureNoticesSerializer(lec_notice, many=True)
lec_notice_data = lec_notice_ser.data
return render(request, "LectureSummarizingApp/summarization.html", {"lec_audio_data": data, "noiseless_data": noiseless_data,"lecture_text_data": lecture_text_data, "lec_summary_data" : lec_summary_data, "lec_notice_data":lec_notice_data})
class audioList(APIView):
def get(self, request):
lecture_audio = LectureAudio.objects.all()
serializer = LectureAudioSerializer(lecture_audio, many=True)
return Response(serializer.data)
def post(self):
pass
class audioNoiseRemovedList(APIView):
def get(self, request):
lecture_audio_noise_removed = LectureAudioNoiseRemoved.objects.all()
serializer = LectureAudioNoiseRemovedSerializer(lecture_audio_noise_removed, many=True)
return Response(serializer.data)
def post(self, request):
LectureAudioNoiseRemoved(
lecture_audio_noise_removed_id=request.data["lecture_audio_noise_removed_id"],
lecture_audio_id=request.data["lecture_audio_id"],
lecturer_date=request.data["lecturer_date"],
lecture_audio_name=request.data["lecture_audio_name"],
lecture_audio_length=request.data["lecture_audio_length"]
).save()
return Response({"response": request.data})
class audioToTextList(APIView):
def get(self, request):
lecture_speech_to_text_id = LectureSpeechToText.objects.all()
serializer = LectureSpeechToTextSerializer(lecture_speech_to_text_id, many=True)
return Response(serializer.data)
def post(self, request):
LectureSpeechToText(
lecture_speech_to_text_id=request.data["lecture_speech_to_text_id"],
lecture_audio_id=request.data["lecture_audio_id"],
audio_original_text=request.data["audio_original_text"]
).save()
return Response({"response": request.data})
class lectureSummaryList(APIView):
def get(self, request):
lecture_audio_summary_id = LectureAudioSummary.objects.all()
serializer = LectureAudioSummarySerializer(lecture_audio_summary_id, many=True)
return Response(serializer.data)
def post(self, request):
LectureAudioSummary(
lecture_speech_to_text_id=request.data["lecture_speech_to_text_id"],
lecture_audio_id=request.data["lecture_audio_id"],
audio_original_text=request.data["audio_original_text"],
audio_summary=request.data["audio_summary"]
).save()
return Response({"response": request.data})
class lectureNoticeList(APIView):
def get(self, request):
lecture_notice_id = LectureNotices.objects.all()
serializer = LectureNoticesSerializer(lecture_notice_id, many=True)
return Response(serializer.data)
def post(self, request):
LectureNotices(
lecture_notice_id=request.data["lecture_notice_id"],
lecture_audio_id=request.data["lecture_audio_id"],
notice_text=request.data["notice_text"]
).save()
return Response({"response": request.data})
\ No newline at end of file
...@@ -23,5 +23,6 @@ urlpatterns = [ ...@@ -23,5 +23,6 @@ urlpatterns = [
path('', include('FirstApp.urls')), path('', include('FirstApp.urls')),
path('attendance/', include('AttendanceApp.urls')), path('attendance/', include('AttendanceApp.urls')),
path('lecturer/', include('MonitorLecturerApp.urls')), path('lecturer/', include('MonitorLecturerApp.urls')),
# path('lecturer/', include('MonitorLecturerApp.urls')),
path('summary/', include('LectureSummarizingApp.urls')) path('summary/', include('LectureSummarizingApp.urls'))
] ]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment