Skip to content

Commit

Permalink
updtae
Browse files Browse the repository at this point in the history
  • Loading branch information
nishant0708 committed Dec 1, 2024
1 parent a061950 commit 201d3a2
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 43 deletions.
120 changes: 82 additions & 38 deletions backend/server.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
from flask import Flask, render_template, Response, jsonify, request
import cv2
from mtcnn.mtcnn import MTCNN
from pydub import AudioSegment
import subprocess # For calling ffmpeg directly
import pyaudio
import wave
import threading
import speech_recognition as sr
import cloudinary
import cloudinary.uploader
import random
import os
import time
from flask_cors import CORS
import os

app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})
port = int(os.environ.get("PORT", 8000)) # Render uses dynamic ports

# Initialize global variables
cap = None
audio = None
detector = MTCNN()
recognizer = sr.Recognizer()
lock = threading.Lock()

# Status flags
camera_ready = False
audio_ready = False
recording_in_progress = False
test_ready = False

Expand All @@ -33,11 +37,10 @@
)

def initialize_devices():
global cap, camera_ready
global cap, audio, camera_ready, audio_ready

try:
# Initialize camera
print("Camera initialized successfully")
if cap is None:
cap = cv2.VideoCapture(0)
if cap.isOpened():
Expand All @@ -47,41 +50,32 @@ def initialize_devices():
print("Failed to initialize camera")
return False

return camera_ready
# Initialize audio
if audio is None:
audio = pyaudio.PyAudio()
try:
# Test audio input
stream = audio.open(
format=pyaudio.paInt16,
channels=1,
rate=16000,
input=True,
frames_per_buffer=1024,
start=False
)
stream.close()
audio_ready = True
print("Audio initialized successfully")
except Exception as e:
print(f"Failed to initialize audio: {e}")
return False

return camera_ready and audio_ready

except Exception as e:
print(f"Error initializing devices: {e}")
return False

def record_audio_with_ffmpeg(name, papercode, duration=5):
try:
file_basename = f"{name}_{papercode}"
filename = f"{file_basename}_audio.wav"

# Use ffmpeg to record audio from the default system microphone
subprocess.run([
"ffmpeg", "-y", "-f", "alsa", "-i", "default",
"-t", str(duration), filename
])

# Upload to Cloudinary
response = cloudinary.uploader.upload(
filename,
resource_type="raw",
public_id=f"{file_basename}_audio",
folder="media"
)

# Clean up temporary file
if os.path.exists(filename):
os.remove(filename)

return response['secure_url']

except Exception as e:
print(f"Error recording audio: {e}")
return None

def record_video(name, papercode, duration=5):
global recording_in_progress

Expand Down Expand Up @@ -136,19 +130,66 @@ def record_video(name, papercode, duration=5):
recording_in_progress = False
return None


def record_audio(name, papercode, duration=5):
try:
file_basename = f"{name}_{papercode}"
filename = f"{file_basename}_audio.wav"

stream = audio.open(
format=pyaudio.paInt16,
channels=1,
rate=16000,
input=True,
frames_per_buffer=1024
)

frames = []
for _ in range(0, int(16000 / 1024 * duration)):
data = stream.read(1024)
frames.append(data)

stream.stop_stream()
stream.close()

with wave.open(filename, 'wb') as wf:
wf.setnchannels(1)
wf.setsampwidth(audio.get_sample_size(pyaudio.paInt16))
wf.setframerate(16000)
wf.writeframes(b''.join(frames))

# Upload to Cloudinary
response = cloudinary.uploader.upload(
filename,
resource_type="raw",
public_id=f"{file_basename}_audio",
folder="media"
)

# Clean up temporary file
if os.path.exists(filename):
os.remove(filename)

return response['secure_url']

except Exception as e:
print(f"Error recording audio: {e}")
return None

def run_test_recording(name, papercode, login_status):
global test_ready
try:
while login_status: # Keep recording until the login status is false
video_url = record_video(name, papercode)
audio_url = record_audio_with_ffmpeg(name, papercode)
audio_url = record_audio(name, papercode)

if video_url and audio_url:
test_ready = True
return {'status': 'success', 'video_url': video_url, 'audio_url': audio_url}
else:
return {'status': 'error', 'message': 'Failed to record video or audio'}

# If login_status becomes false, stop recording and cleanup
return {'status': 'error', 'message': 'Recording stopped due to logout'}

except Exception as e:
Expand All @@ -174,16 +215,19 @@ def start_test():
def check_test_status():
return jsonify({
'camera_ready': camera_ready,
'audio_ready': audio_ready,
'recording_in_progress': recording_in_progress,
'test_ready': test_ready
})


@app.route('/initialize_devices')
def check_devices():
success = initialize_devices()
return jsonify({
'success': success,
'camera_ready': camera_ready
'camera_ready': camera_ready,
'audio_ready': audio_ready
})

def generate_frames():
Expand All @@ -204,4 +248,4 @@ def video_feed():
mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == '__main__':
app.run(host='0.0.0.0', port=port, debug=True)
app.run(debug=True)
10 changes: 5 additions & 5 deletions src/verification/verification.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const Verification = () => {
}, [])

const checkDevices = () => {
fetch('https://iips-exam-student-portal.onrender.com/initialize_devices')
fetch('http://127.0.0.1:5000/initialize_devices')
.then((response) => response.json())
.then((data) => {
setDeviceStatus({
Expand Down Expand Up @@ -61,7 +61,7 @@ const Verification = () => {
setIsRecording(true);
setTestStatus('Please wait...');

fetch('https://iips-exam-student-portal.onrender.com/start_test', {
fetch('http://127.0.0.1:5000/start_test', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
Expand All @@ -72,7 +72,7 @@ const Verification = () => {
})
.then(() => {
const intervalId = setInterval(() => {
fetch('https://iips-exam-student-portal.onrender.com/check_test_status')
fetch('http://127.0.0.1:5000/check_test_status')
.then((response) => response.json())
.then((statusData) => {
if (statusData.recording_in_progress) {
Expand All @@ -98,7 +98,7 @@ const Verification = () => {
setIsRecording(false);
setTestStatus('Recording stopped due to logout.');
// Send a request to stop the recording on the server
fetch('https://iips-exam-student-portal.onrender.com/start_test', {
fetch('http://127.0.0.1:5000/start_test', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
Expand Down Expand Up @@ -128,7 +128,7 @@ const Verification = () => {
<img
id="verification_webcam"
className="verification_webcam"
src="https://iips-exam-student-portal.onrender.com/video_feed"
src="http://127.0.0.1:5000/video_feed"
alt="Webcam feed"
/>

Expand Down

0 comments on commit 201d3a2

Please sign in to comment.