Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
satinder147 authored Jun 22, 2018
1 parent 021fe75 commit ef1e0e2
Show file tree
Hide file tree
Showing 15 changed files with 383 additions and 0 deletions.
19 changes: 19 additions & 0 deletions arduino.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import serial

class Arduino:
def __init__(self,baudrate,COM,timeout=1):
self.timeout=timeout
self.COM=COM
self.baudrate=baudrate
self.a=serial.Serial(self.COM,self.baudrate,timeout=self.timeout)

def movleft(self):
self.a.write(b'1')
data=self.a.readline().decode('ascii')
print(data)
def movright(self):
self.a.write(b'2')
data=self.a.readline().decode('ascii')
print(data)


109 changes: 109 additions & 0 deletions blind_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@


from face_detection import detect
from arduino import Arduino
from voice import Voice
import time
import cv2
import numpy as np
from keras.preprocessing.image import img_to_array
from keras.models import load_model
import datetime
from stop import Stop
model_name='blind_with_regularization.model'
COM='COM9'
camera=1
baudrate=9600
width=64
height=64
prob=0
label=''









print("loading model .....")
model=load_model(model_name)
print("model loaded")
ard=Arduino(baudrate,COM)##movleft(),movright()
vce=Voice()#left(),right()
st=Stop()
fac=detect()
current=datetime.datetime.now()
flag=None
cap=cv2.VideoCapture(camera)
ret=True
prev=None
while ret:
ret,frame=cap.read()
frame=cv2.resize(frame,(640,480))

faces=fac.faceDetect(frame)

##stop on left

## you have a stop on '''
current=datetime.datetime.now()
new=current.second
if((current.second)%4==0):
if(prev!=new):
sto,direction=st.detect(frame)
if(direction==0):
vce.stop_right()

elif(direction==1):
vce.stop_left()

cv2.imshow('stop',sto)
faces,dire=fac.faceDetect(frame)
if(dire==0):
vce.peopleOnRight()
if(dire==1):
vce.peopleOnLeft()
cv2.imshow('faces',faces)
frame2=frame
frame2=cv2.resize(frame2,(width,height))
frame2=frame2.astype('float')/255.0
frame2=img_to_array(frame2)
frame2=np.expand_dims(frame2,axis=0)
left,right,center=model.predict(frame2)[0]
if(left>right and left >center):
label='left'
prob=left*100

if(left<right and right >center):
label='right'
prob=left*right
#vce.left()
for i in range(1):
ard.movleft()

if(center>right and left <center):
label='center'
prob=center*100
#vce.left()
for i in range(1):
ard.movleft()
prev=new
cv2.putText(frame,label+" with probability "+str(prob),(10,25),cv2.FONT_HERSHEY_SIMPLEX,0.7,(255,0,0),2)



cv2.imshow('frame',frame)




if(cv2.waitKey(1)&0XFF==ord('q')):
break
cap.release()
cv2.destroyAllWindows()




24 changes: 24 additions & 0 deletions face_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import dlib
import cv2

class detect:
def __init__(self):
self.detector=dlib.get_frontal_face_detector()
def draw(self,frame,x,y,w,h):
frame=cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
return frame,(x+w)

def faceDetect(self,frame):
y=0
frame=cv2.resize(frame,(300,225))
grey=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
rect=self.detector(grey,1)
for i,d in enumerate(rect):
frame,y=self.draw(frame,d.left(),d.top(),d.right()-d.left(),d.bottom()-d.top())
if(y==0):
return frame,-1
elif(y<150):
return frame,1
elif(y>=150):
return frame,0

Binary file added face_left.mp3
Binary file not shown.
Binary file added face_right.mp3
Binary file not shown.
Binary file added left.mp3
Binary file not shown.
35 changes: 35 additions & 0 deletions load_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import cv2
import os
import sys
from keras.preprocessing.image import img_to_array

class load:
def __init__(self,width,height,channels,paths):
self.width=width
self.height=height
self.channels=channels
self.paths=paths
self.data=[]
self.labels=[]
def imgload(self):
i=-1
for folder in self.paths:
i=i+1

path=str(folder[0])
folder=os.listdir(path)

for file in folder:
#print(file+" "+str(i))
img=cv2.imread(path+'/'+file,-1)

img=cv2.resize(img,(self.width,self.height))
img=img_to_array(img)
self.data.append(img)
self.labels.append(i)

return (self.data,self.labels)




88 changes: 88 additions & 0 deletions model_architecture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Activation
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import LeakyReLU
from keras import backend as k
from keras import regularizers

class Model:
def __init__(self,width,height,channels,num_classes):
self.width=width
self.height=height
self.channels=channels
self.num_classes=num_classes
def model(self):
model=Sequential()
input_Shape=(self.height,self.width,self.channels)
if(k.image_data_format=="channels_first"):
input_Shape=(self.channels,self.height,self.width)
model.add(Conv2D(20,(5,5),padding="same",input_shape=input_Shape))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(50,(5,5),padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(Flatten())
model.add(Dense(500))
model.add(Activation("relu"))
model.add(Dense(self.num_classes))
model.add(Activation("softmax"))
return model


def model2(self):
model=Sequential()
input_Shape=(self.height,self.width,self.channels)
if(k.image_data_format=="channels_first"):
input_Shape=(self.channels,self.height,self.width)
model.add(Conv2D(20,(5,5),padding="same",input_shape=input_Shape))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(50,(5,5),padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(100,(5,5),padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(Flatten())
model.add(Dense(500))
model.add(Activation("relu"))
model.add(Dense(self.num_classes))
model.add(Activation("softmax"))
return model




def model3(self):
model=Sequential()
input_Shape=(self.height,self.width,self.channels)
if(k.image_data_format=="channels_first"):
input_Shape=(self.channels,self.height,self.width)
model.add(Conv2D(20,(5,5),padding="same",input_shape=input_Shape,kernel_regularizer=regularizers.l2(0.01)))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(50,(5,5),padding="same",kernel_regularizer=regularizers.l2(0.01)))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(100,(5,5),padding="same",kernel_regularizer=regularizers.l2(0.01)))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(Flatten())
model.add(Dense(500))
model.add(LeakyReLU(alpha=0.1))
model.add(Dense(self.num_classes))
model.add(Activation("softmax"))
return model









33 changes: 33 additions & 0 deletions model_trainer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from model_architecture import Model
from load_images import load
import random
import numpy as np
from model_architecture import Model
from sklearn.model_selection import train_test_split
from keras.utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
epochs=25
learning_rate=0.001
bs=32
data=[]
labels=[]
architecture=Model(64,64,3,3)
model=architecture.model3()
model.summary()
ld=load(64,64,3,[['D:/blind project 2/left'],['D:/blind project 2/right'],['D:/blind project 2/center']])

data,labels=ld.imgload()
c=list(zip(data,labels))
random.shuffle(c)
data[:],labels[:]=zip(*c)
data=np.array(data,dtype="float")/255.0
labels=np.array(labels)
trainX,testX,trainY,testY=train_test_split(data,labels,test_size=0.1,random_state=42)
trainY=to_categorical(trainY,num_classes=3)
testY=to_categorical(testY,num_classes=3)
aug=ImageDataGenerator(rotation_range=30,width_shift_range=0.1,height_shift_range=0.1,shear_range=0.2,zoom_range=0.2,fill_mode="nearest")
opt=Adam(lr=learning_rate,decay=learning_rate/epochs)
model.compile(loss="categorical_crossentropy",optimizer=opt,metrics=["accuracy"])
h=model.fit_generator(aug.flow(trainX,trainY,batch_size=bs),validation_data=(testX,testY),steps_per_epoch=len(trainX)//bs,epochs=epochs,verbose=1)
model.save("blind_with_regularization.model")
Binary file added right.mp3
Binary file not shown.
30 changes: 30 additions & 0 deletions stop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import cv2

class Stop:
def __init__(self):
self.cas=cv2.CascadeClassifier('stopsign_classifier.xml')

def detect(self,frame):
x=0
y=0
h=0
w=0
frame=cv2.resize(frame,(640,480))
#cv2.imshow('fra',frame)
frame2=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
#cv2.imshow('fra',frame2)
boxes=self.cas.detectMultiScale(frame2,1.3,5)
for (x,y,w,h) in boxes:
frame=cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
if(x==0):
return frame,-1
elif((x+w)>320):
#print((x+w)/2)
return frame,0
elif((x+w)<=320):
#print((x+w)/2)
return frame,1
else:
return frame,-1


Binary file added stop_left.mp3
Binary file not shown.
Binary file added stop_right.mp3
Binary file not shown.
18 changes: 18 additions & 0 deletions video2image(data Generator).py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import cv2
import numpy as np
cap=cv2.VideoCapture('C:/Users/satinder/Desktop/center5.mp4')
ret=True
i=0
j=3772
while ret:
ret,frame=cap.read()
frame=cv2.resize(frame,(640,480))
cv2.imshow('s',frame)
if(i%5==0):

cv2.imwrite('C:/Users/satinder/Desktop/center/'+str(j)+'.jpg',frame)
j=j+1
i=i+1
if(cv2.waitKey(1)&0XFF==ord('q')):
break
cv2.destroyAllWindows()
27 changes: 27 additions & 0 deletions voice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from pygame import mixer
class Voice:
def right(self):
mixer.init()
mixer.music.load('right.mp3')
mixer.music.play()
def left(self):
mixer.init()
mixer.music.load('left.mp3')
mixer.music.play()
def stop_left(self):
mixer.init()
mixer.music.load('stop_left.mp3')
mixer.music.play()
def stop_right(self):
mixer.init()
mixer.music.load('stop_right.mp3')
mixer.music.play()
def peopleOnRight(self):
mixer.init()
mixer.music.load('face_right.mp3')
mixer.music.play()
def peopleOnLeft(self):
mixer.init()
mixer.music.load('face_left.mp3')
mixer.music.play()

0 comments on commit ef1e0e2

Please sign in to comment.