Skip to content

Commit

Permalink
Updated and Handled all python files (tanishaness#31)
Browse files Browse the repository at this point in the history
* Update audio.py

* Update detection.py

* Update face-rec.py

* Update graph.py

* Update head_pose.py

* Update peer_comparison_tool.py

* Update processes.py

* Update pyaudio_test.py

* Update run.py

* Update screen_recorder.py
  • Loading branch information
Sai-ganesh-0004 authored Oct 14, 2024
1 parent 72b8a82 commit dba4662
Show file tree
Hide file tree
Showing 10 changed files with 538 additions and 413 deletions.
91 changes: 57 additions & 34 deletions Backend/audio.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,81 @@
import sounddevice as sd
import numpy as np
import logging

# place holders and global variables
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

# Placeholders and global variables
SOUND_AMPLITUDE = 0
AUDIO_CHEAT = 0

# sound variables
# SUS means next sound packet is worth analyzing
CALLBACKS_PER_SECOND = 38 # callbacks per sec(system dependent)
SUS_FINDING_FREQUENCY = 2 # calculates SUS *n* times every sec
SOUND_AMPLITUDE_THRESHOLD = 20 # amplitude considered for SUS calc
# Sound variables
CALLBACKS_PER_SECOND = 38 # Callbacks per sec (system dependent)
SUS_FINDING_FREQUENCY = 2 # Calculates SUS *n* times every sec
SOUND_AMPLITUDE_THRESHOLD = 20 # Amplitude considered for SUS calc

# packing *n* frames to calculate SUS
# Packing *n* frames to calculate SUS
FRAMES_COUNT = int(CALLBACKS_PER_SECOND/SUS_FINDING_FREQUENCY)
AMPLITUDE_LIST = list([0]*FRAMES_COUNT)
AMPLITUDE_LIST = list([0] * FRAMES_COUNT)
SUS_COUNT = 0
count = 0

def print_sound(indata, outdata, frames, time, status):
avg_amp = 0
global SOUND_AMPLITUDE, SUS_COUNT, count, SOUND_AMPLITUDE_THRESHOLD, AUDIO_CHEAT
vnorm = int(np.linalg.norm(indata)*10)
AMPLITUDE_LIST.append(vnorm)
count += 1
AMPLITUDE_LIST.pop(0)
if count == FRAMES_COUNT:
avg_amp = sum(AMPLITUDE_LIST)/FRAMES_COUNT
SOUND_AMPLITUDE = avg_amp
if SUS_COUNT >= 2:
#print("!!!!!!!!!!!! FBI OPEN UP !!!!!!!!!!!!")
AUDIO_CHEAT = 1
SUS_COUNT = 0
if avg_amp > SOUND_AMPLITUDE_THRESHOLD:
SUS_COUNT += 1
#print("Sus...", SUS_COUNT)
else:
SUS_COUNT = 0
AUDIO_CHEAT = 0
count = 0

try:
vnorm = int(np.linalg.norm(indata) * 10)
AMPLITUDE_LIST.append(vnorm)
count += 1
AMPLITUDE_LIST.pop(0)

if count == FRAMES_COUNT:
avg_amp = sum(AMPLITUDE_LIST) / FRAMES_COUNT
SOUND_AMPLITUDE = avg_amp

if SUS_COUNT >= 2:
AUDIO_CHEAT = 1
SUS_COUNT = 0

if avg_amp > SOUND_AMPLITUDE_THRESHOLD:
SUS_COUNT += 1
else:
SUS_COUNT = 0
AUDIO_CHEAT = 0

count = 0
except Exception as e:
logging.error(f"Error in print_sound: {e}")
# Optionally notify the user
print("An error occurred while processing audio input. Please check the logs.")

def sound():
with sd.Stream(callback=print_sound):
sd.sleep(-1)
try:
with sd.Stream(callback=print_sound):
sd.sleep(-1)
except Exception as e:
logging.error(f"Error in sound function: {e}")
print("An error occurred while starting the audio stream. Please check the logs.")

def sound_analysis():
global AMPLITUDE_LIST, FRAMES_COUNT, SOUND_AMPLITUDE
while True:
AMPLITUDE_LIST.append(SOUND_AMPLITUDE)
AMPLITUDE_LIST.pop(0)
try:
AMPLITUDE_LIST.append(SOUND_AMPLITUDE)
AMPLITUDE_LIST.pop(0)

avg_amp = sum(AMPLITUDE_LIST)/FRAMES_COUNT
avg_amp = sum(AMPLITUDE_LIST) / FRAMES_COUNT

if avg_amp > 10:
print("Sus...")
if avg_amp > 10:
print("Sus...")
except Exception as e:
logging.error(f"Error in sound_analysis: {e}")
print("An error occurred during sound analysis. Please check the logs.")

if __name__ == "__main__":
sound()
try:
sound()
except KeyboardInterrupt:
logging.info("Sound analysis interrupted by user.")
print("Terminated sound analysis.")
173 changes: 97 additions & 76 deletions Backend/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
import head_pose
import matplotlib.pyplot as plt
import numpy as np
import logging

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

PLOT_LENGTH = 200

# place holders
# Placeholders
GLOBAL_CHEAT = 0
PERCENTAGE_CHEAT = 0
CHEAT_THRESH = 0.6
XDATA = list(range(200))
YDATA = [0]*200
YDATA = [0] * 200

# Global flag to check if window is open
is_running=True
is_running = True

def avg(current, previous):
if previous > 1:
Expand All @@ -28,90 +32,107 @@ def avg(current, previous):
return 1 * previous + 0.1 * current

def process():
global GLOBAL_CHEAT, PERCENTAGE_CHEAT, CHEAT_THRESH #head_pose.X_AXIS_CHEAT, head_pose.Y_AXIS_CHEAT, audio.AUDIO_CHEAT
# print(head_pose.X_AXIS_CHEAT, head_pose.Y_AXIS_CHEAT)
# print("entered proess()...")
if GLOBAL_CHEAT == 0:
if head_pose.X_AXIS_CHEAT == 0:
if head_pose.Y_AXIS_CHEAT == 0:
if audio.AUDIO_CHEAT == 0:
PERCENTAGE_CHEAT = avg(0, PERCENTAGE_CHEAT)
else:
PERCENTAGE_CHEAT = avg(0.2, PERCENTAGE_CHEAT)
else:
if audio.AUDIO_CHEAT == 0:
PERCENTAGE_CHEAT = avg(0.2, PERCENTAGE_CHEAT)
else:
PERCENTAGE_CHEAT = avg(0.4, PERCENTAGE_CHEAT)
else:
if head_pose.Y_AXIS_CHEAT == 0:
if audio.AUDIO_CHEAT == 0:
PERCENTAGE_CHEAT = avg(0.1, PERCENTAGE_CHEAT)
else:
PERCENTAGE_CHEAT = avg(0.4, PERCENTAGE_CHEAT)
else:
if audio.AUDIO_CHEAT == 0:
PERCENTAGE_CHEAT = avg(0.15, PERCENTAGE_CHEAT)
else:
PERCENTAGE_CHEAT = avg(0.25, PERCENTAGE_CHEAT)
else:
if head_pose.X_AXIS_CHEAT == 0:
if head_pose.Y_AXIS_CHEAT == 0:
if audio.AUDIO_CHEAT == 0:
PERCENTAGE_CHEAT = avg(0, PERCENTAGE_CHEAT)
global GLOBAL_CHEAT, PERCENTAGE_CHEAT, CHEAT_THRESH

try:
if GLOBAL_CHEAT == 0:
if head_pose.X_AXIS_CHEAT == 0:
if head_pose.Y_AXIS_CHEAT == 0:
if audio.AUDIO_CHEAT == 0:
PERCENTAGE_CHEAT = avg(0, PERCENTAGE_CHEAT)
else:
PERCENTAGE_CHEAT = avg(0.2, PERCENTAGE_CHEAT)
else:
PERCENTAGE_CHEAT = avg(0.55, PERCENTAGE_CHEAT)
if audio.AUDIO_CHEAT == 0:
PERCENTAGE_CHEAT = avg(0.2, PERCENTAGE_CHEAT)
else:
PERCENTAGE_CHEAT = avg(0.4, PERCENTAGE_CHEAT)
else:
if audio.AUDIO_CHEAT == 0:
PERCENTAGE_CHEAT = avg(0.55, PERCENTAGE_CHEAT)
if head_pose.Y_AXIS_CHEAT == 0:
if audio.AUDIO_CHEAT == 0:
PERCENTAGE_CHEAT = avg(0.1, PERCENTAGE_CHEAT)
else:
PERCENTAGE_CHEAT = avg(0.4, PERCENTAGE_CHEAT)
else:
PERCENTAGE_CHEAT = avg(0.85, PERCENTAGE_CHEAT)
if audio.AUDIO_CHEAT == 0:
PERCENTAGE_CHEAT = avg(0.15, PERCENTAGE_CHEAT)
else:
PERCENTAGE_CHEAT = avg(0.25, PERCENTAGE_CHEAT)
else:
if head_pose.Y_AXIS_CHEAT == 0:
if audio.AUDIO_CHEAT == 0:
PERCENTAGE_CHEAT = avg(0.6, PERCENTAGE_CHEAT)
if head_pose.X_AXIS_CHEAT == 0:
if head_pose.Y_AXIS_CHEAT == 0:
if audio.AUDIO_CHEAT == 0:
PERCENTAGE_CHEAT = avg(0, PERCENTAGE_CHEAT)
else:
PERCENTAGE_CHEAT = avg(0.55, PERCENTAGE_CHEAT)
else:
PERCENTAGE_CHEAT = avg(0.85, PERCENTAGE_CHEAT)
if audio.AUDIO_CHEAT == 0:
PERCENTAGE_CHEAT = avg(0.55, PERCENTAGE_CHEAT)
else:
PERCENTAGE_CHEAT = avg(0.85, PERCENTAGE_CHEAT)
else:
if audio.AUDIO_CHEAT == 0:
PERCENTAGE_CHEAT = avg(0.5, PERCENTAGE_CHEAT)
if head_pose.Y_AXIS_CHEAT == 0:
if audio.AUDIO_CHEAT == 0:
PERCENTAGE_CHEAT = avg(0.6, PERCENTAGE_CHEAT)
else:
PERCENTAGE_CHEAT = avg(0.85, PERCENTAGE_CHEAT)
else:
PERCENTAGE_CHEAT = avg(0.85, PERCENTAGE_CHEAT)
if audio.AUDIO_CHEAT == 0:
PERCENTAGE_CHEAT = avg(0.5, PERCENTAGE_CHEAT)
else:
PERCENTAGE_CHEAT = avg(0.85, PERCENTAGE_CHEAT)

if PERCENTAGE_CHEAT > CHEAT_THRESH:
GLOBAL_CHEAT = 1
print("CHEATING")
else:
GLOBAL_CHEAT = 0
print("Cheat percent: ", PERCENTAGE_CHEAT, GLOBAL_CHEAT)
if PERCENTAGE_CHEAT > CHEAT_THRESH:
GLOBAL_CHEAT = 1
print("CHEATING")
else:
GLOBAL_CHEAT = 0
print("Cheat percent: ", PERCENTAGE_CHEAT, GLOBAL_CHEAT)

except Exception as e:
logging.error(f"Error in process: {e}")
print("An error occurred during processing. Please check the logs.")

def on_close(event):
global is_running
is_running=False
is_running = False
# Set flag to False when the window is closed

def run_detection():
global XDATA,YDATA,is_running

fig, axes = plt.subplots()

axes.set_xlim(0, 200)
axes.set_ylim(0,1)
line, = axes.plot(XDATA, YDATA, 'r-')
plt.title("Suspicious Behaviour Detection")
plt.xlabel("Time")
plt.ylabel("Cheat Probablity")

# Connect the close event to the callback
fig.canvas.mpl_connect('close_event', on_close)

while is_running:
YDATA.pop(0)
YDATA.append(PERCENTAGE_CHEAT)
line.set_xdata(XDATA)
line.set_ydata(YDATA)
plt.draw()
plt.pause(1e-17)
process()
time.sleep(1/5)
plt.close(fig)
global XDATA, YDATA, is_running

try:
fig, axes = plt.subplots()

axes.set_xlim(0, 200)
axes.set_ylim(0, 1)
line, = axes.plot(XDATA, YDATA, 'r-')
plt.title("Suspicious Behaviour Detection")
plt.xlabel("Time")
plt.ylabel("Cheat Probability")

# Connect the close event to the callback
fig.canvas.mpl_connect('close_event', on_close)

while is_running:
YDATA.pop(0)
YDATA.append(PERCENTAGE_CHEAT)
line.set_xdata(XDATA)
line.set_ydata(YDATA)
plt.draw()
plt.pause(1e-17)
process()
time.sleep(1 / 5)

plt.close(fig)

except Exception as e:
logging.error(f"Error in run_detection: {e}")
print("An error occurred while running the detection. Please check the logs.")

if __name__ == "__main__":
try:
run_detection()
except KeyboardInterrupt:
logging.info("Detection interrupted by user.")
print("Terminated detection.")
Loading

0 comments on commit dba4662

Please sign in to comment.