Skip to content

Instantly share code, notes, and snippets.

View AdroitAnandAI's full-sized avatar

Anand P V AdroitAnandAI

View GitHub Profile
@AdroitAnandAI
AdroitAnandAI / wordcount.py
Created June 6, 2021 05:47
To count words
# Word Count using Spark
# https://spark.apache.org/examples.html
text_file = sc.textFile("./sample.txt") //local file
counts = text_file.flatMap(lambda line: line.split(" ")) \
.map(lambda word: (word, 1)) \
.reduceByKey(lambda a, b: a + b)
counts.saveAsTextFile("./count_output")
@AdroitAnandAI
AdroitAnandAI / pi.py
Last active June 6, 2021 05:40
To compute pi value
# Compute value of PI using Monte Carlo Approach
# Source: http://spark.apache.org/examples.html
NUM_SAMPLES = 1000000
def inside(p):
x, y = random.random(), random.random()
return x*x + y*y < 1
# filter: http://spark.apache.org/docs/latest/api/python/pyspark.html
count = sc.parallelize(range(0, NUM_SAMPLES),10) \
@AdroitAnandAI
AdroitAnandAI / cf.m
Created February 11, 2021 14:27
collaborative filtering
function [J, grad] = cofiCostFunc(params, Y, R, num_users, num_movies, ...
num_features, lambda)
% Unfold the U and W matrices from params
X = reshape(params(1:num_movies*num_features), num_movies, num_features);
Theta = reshape(params(num_movies*num_features+1:end), ...
num_users, num_features);
% You need to return the following values correctly
J = 0;
@AdroitAnandAI
AdroitAnandAI / findAnomaly.m
Created February 11, 2021 12:15
Find anomalies from data
function [mu sigma2] = estimateGaussian(X)
% To estimate parameters of Gaussian distribution using data
% Useful variables
[m, n] = size(X);
% You should return these values correctly
mu = zeros(n, 1);
sigma2 = zeros(n, 1);
@AdroitAnandAI
AdroitAnandAI / sentimental_lstm.py
Created February 10, 2021 03:32
Sentimental Analysis
# Training Module of LSTM 2 layer stack
max_review_length = 600
X_train = sequence.pad_sequences(X_train, maxlen=max_review_length)
X_test = sequence.pad_sequences(X_test, maxlen=max_review_length)
# To train the Model
def trainModel(model):
history = model.fit(numpy.array(X_train), numpy.array(y_train), \
@AdroitAnandAI
AdroitAnandAI / speechThread.py
Last active September 20, 2020 16:28
Speech Recognition Thread
# Speech Recognition Thread identifies the command from defined synonyms and inserting to stt Queue
def stopStream(stream_reader):
if stream_reader:
stream_reader.stop_stream()
stream_reader = None
def received_frames(frames, speech, stt):
speech.push_data(frames, finish_processing=False)
@AdroitAnandAI
AdroitAnandAI / sttThread.py
Last active September 20, 2020 15:04
Sound Processing Thread
import os
import cv2
from queue import Queue
from threading import Thread, Event
from audio import audio_helper
from speech_library.speech_manager import SpeechManager
from speech_library.speech_proxy import SPEECH_CONFIG
@AdroitAnandAI
AdroitAnandAI / winkPeak.py
Last active September 20, 2020 15:00
Detect Wink with Peak
# To detect wink from stream of eye images and print the sequence of Event trigger
while(runLoop):
for i in range (1000):
# read the image
image = cv2.imread("eyeImages_day/eye"+str(i) + ".jpg", 1)
# Convert to gray scale as histogram works well on 256 values.
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
@AdroitAnandAI
AdroitAnandAI / curveFit.py
Last active September 20, 2020 15:17
Fit Inverse Sigmoid Curve
# Code to fit the inverse sigmoid curve to tail end of signal
def sigmoid(x, L ,x0, k, b):
y = L / (1 + np.exp(k*(x-x0)))+b
return (y)
def isCurveSigmoid(pixelCounts, count):
try:
xIndex = len(pixelCounts)
@AdroitAnandAI
AdroitAnandAI / eulerCartesian.py
Last active September 21, 2020 03:02
Euler to Cartesian Conversion
# yaw and pitch are important for mouse control
poseArrowX = orientation[0] #* arrowLength
poseArrowY = orientation[1] #* arrowLength
# Taking 2nd and 3rd row for 2D Projection
############################# LEFT EYE ###################################
cv2.arrowedLine(frame, leftEye_Center,
(int((xCenter_left + arrowLength * (cosR * cosY + sinY * sinP * sinR))),
int((yCenter_left + arrowLength * cosP * sinR))), (255, 0, 0), 4)