AI and ML

Emotion Based Music Player

Problem Statement:

Emotion Based Music Player. Develop an Emotion detection system using Machine Learning and OpenCV.
Integrate it with playing music using python.
The Algorithm should be able to detect two emotions – “Happy” and “Surprised”.

IMAGE

for the first step, we downloaded the data from web link – https://www.shutterstock.com

We collected images from the site and divided them into two categories 1. smiling 2. shocked

link to a dataset –  https://github.com/sainitishkumar/images

Then choosing the model.

We chose KNN as our model for classifying the images.

One group is for happy images and other for shocked.

Project organization:

|— main folder

|— train_model.py

|— test_model.py

|— smiling ( folder containing train data)

|— shocked ( folder containing train data)

|— testimg.png

|— happy.mp3

|— surprised.mp3

|— haarcascade_frontal_face.xml

Python Modules used :

OpenCV , scikit-learn ( sklearn ), numpy , subprocess , os , sys.

 

Training the model :

for detecting faces OpenCV haarcascade_frontalface_alt is used.

sklearn for the KNN algorithm.

subprocess for playing the music.

os and sys for listing directories.

We defined two functions face_detect and load_data, first one for detecting the faces in the images and the second function for loading the data into two variables faces and labels.

CODE :

mainfolder=‘path to main folder’

def face_detect(img):

fd=cv2.CascadeClassifier(“haarcascade_frontalface_alt.xml”)      #Using OpenCV harcasscade frontal face alt to detect faces

img2=img.copy()

gray=cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)

faces=fd.detectMultiScale(gray,1.3,5)

if faces == ():

return []

(x,y,w,h)=faces[0]

img2=gray[y:y+h,x:x+w]

return img2

def load_data(mainfolder):

folders=os.listdir(mainfolder)

happy_path=mainfolder+’/smiling’

surprised_path=mainfolder+’/shocked’

happyfiles=os.listdir(happy_path)

surprisedfiles=os.listdir(surprised_path)

faces=[]

labels=[]

for file in happyfiles:

if ‘jpg’ in file:

img = cv2.imread(happy_path+’/’+file)

face = face_detect(img)

face=face/(face.max())

face=cv2.resize(face,(100,100))

face2=face.astype(numpy.float32)

face2=numpy.ndarray.flatten(face2)

faces.append(face2)

labels.append(‘happy’)

else:

pass

for file in surprisedfiles:

if ‘jpg’ in file:

img = cv2.imread(surprised_path+’/’+file)

face = face_detect(img)

face=face/(face.max())

face=cv2.resize(face,(100,100))

face2=face.astype(numpy.float32)

face2=numpy.ndarray.flatten(face2)

faces.append(face2)

labels.append(‘surprised’)

else:

pass

return faces,labels

faces,labels=load_data(mainfolder)

face_detect and load_data first check whether the images are loaded properly and then they make the images compatible to be trained with KNN model from sklearn.

We use KNeighborsClassifier as our classifier and feed the faces and labels produced from load_data.

from sklearn import neighbors

alg=neighbors.KNeighborsClassifier(n_neighbors=3)  #knn algorithm for image classification

faces2=numpy.asarray(faces)

alg.fit(faces2,labels)          #train our model

print(alg.score(faces2,labels)) #score about 85%

Then to generate our model and use it in other files, we use joblib. dump function to persist the ‘alg’ object into a file named ‘something.pkl’ which can be used in another file without having to retrain every time.

from sklearn.externals import joblib

joblib.dump(alg,’emotion_player.pkl’) #generate .pkl file named emotion_player

Testing the model :

After training the model we now test the model with an image of our choice. First, load the model into a new file.

 

from sklearn.externals import joblib

alg = joblib.load(‘emotion_player.pkl’)

Then read the image with cv2 and detect face and send it to the algorithm to predict the results.

sample = cv2.imread(‘./testimg.jpg’)

face = face_detect(sample)

if face!=[]:

face=face/(face.max())

face=face.astype(numpy.float32)

face=cv2.resize(face,(100,100))

face=numpy.ndarray.flatten(face)

face=face.reshape(1,10000)

output = alg.predict(face)  #predicting output

if output[0]==’surprised’:

subprocess.call([‘afplay’,’surprised.mp3′])

elif output[0] == ‘happy’:

subprocess.call([‘afplay’,’happy.mp3′])

Then according to the predicted results then , subprocess is used to play a particular type of music.

Sources :

  1. shutter stock for images
  2. io for the flow chart

This is all about Emotion Based Music Player

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

error: