Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open innovation project #51

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions 2ndprogram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import cv2
img=cv2.imread("pic2.jpg")
img1=cv2.resize(img,(300,300))
gray_image=cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)

ret,thresh1=cv2.threshold(gray_image,120,255,cv2.THRESH_BINARY)
ret,thresh2=cv2.threshold(gray_image,120,255,cv2.THRESH_BINARY_INV)
ret,thresh3=cv2.threshold(gray_image,120,255,cv2.THRESH_TRUNC)
ret,thresh4=cv2.threshold(gray_image,120,255,cv2.THRESH_TOZERO)
ret,thresh5=cv2.threshold(gray_image,120,255,cv2.THRESH_TOZERO_INV)
thresh6=cv2.adaptiveThreshold(gray_image,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,199,5)
thresh7=cv2.adaptiveThreshold(gray_image,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,199,5)
ret,thresh8=cv2.threshold(gray_image,120,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imshow("original",img1)
cv2.imshow('Binary Threshold', thresh1)
cv2.imshow('Binary Threshold Inverted', thresh2)
cv2.imshow('Truncated Threshold', thresh3)
cv2.imshow('Set to 0', thresh4)
cv2.imshow('Set to 0 Inverted', thresh5)
cv2.imshow('Adaptive Mean', thresh6)
cv2.imshow('Adaptive Gaussian', thresh7)
cv2.imshow('Otsu Threshold', thresh8)

cv2.waitKey(0)
cv2.destroyAllWindows()
25 changes: 25 additions & 0 deletions camera2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import cv2

cap=cv2.VideoCapture(0)
while (cap.isOpened()):
ret,img=cap.read()
img=cv2.flip(img,1)
cv2.rectangle(img,(30,30),(280,280),(255,255,1),3)
cv2.imshow("frame",img)
img1=img[30:280,30:280]
imcopy=img1.copy()
cv2.imshow("frame2",img1)
white=cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)
blur=cv2.GaussianBlur(white,(5,5),0)
ret, thresh1 = cv2.threshold(blur, 10, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
hand_resize = cv2.resize(thresh1, (130, 100))
cv2.imshow("Threshold", thresh1)
contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(imcopy, contours, -1, (0, 255, 0))
cv2.imshow('Draw Contours',imcopy)

k = 0xFF & cv2.waitKey(10)
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
79 changes: 79 additions & 0 deletions collect-data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import cv2
import os

if not os.path.exists("data"):
os.makedirs("data")
os.makedirs("data/train")
os.makedirs("data/train/0")
os.makedirs("data/train/1")
os.makedirs("data/train/2")
os.makedirs("data/train/3")
os.makedirs("data/train/4")
os.makedirs("data/train/5")


mode = 'train'
directory = 'data/'+mode+'/'

# url = '<YOUR IP ADDRESS>/video'
# cap=cv2.VideoCapture(url)

cap=cv2.VideoCapture(0)

while True:
_, frame = cap.read()
frame = cv2.flip(frame, 1)

cv2.putText(frame, "sayantani-12", (150, 300), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (225,255,255), 3)

count = {'zero': len(os.listdir(directory+"/0")),
'one': len(os.listdir(directory+"/1")),
'two': len(os.listdir(directory+"/2")),
'three': len(os.listdir(directory+"/3")),
'four': len(os.listdir(directory+"/4")),
'five': len(os.listdir(directory+"/5"))}

cv2.putText(frame, "MODE : "+mode, (10, 50), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (225,255,255), 1)
cv2.putText(frame, "IMAGE COUNT", (10, 100), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (225,255,255), 1)
cv2.putText(frame, "ZERO : "+str(count['zero']), (10, 120), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,255,255), 1)
cv2.putText(frame, "ONE : "+str(count['one']), (10, 140), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,255,255), 1)
cv2.putText(frame, "TWO : "+str(count['two']), (10, 160), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,255,255), 1)
cv2.putText(frame, "THREE : "+str(count['three']), (10, 180), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,255,255), 1)
cv2.putText(frame, "FOUR : "+str(count['four']), (10, 200), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,255,255), 1)
cv2.putText(frame, "FIVE : "+str(count['five']), (10, 220), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,255,255), 1)


x1 = int(0.5*frame.shape[1])
y1 = 10
x2 = frame.shape[1]-10
y2 = int(0.5*frame.shape[1])
cv2.rectangle(frame, (x1-1, y1-1), (x2+1, y2+1), (2,50,255) ,3)
roi = frame[y1:y2, x1:x2]
roi = cv2.resize(roi, (200, 200))
cv2.putText(frame, "R.O.I", (440, 350), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,225,0), 3)
cv2.imshow("Frame", frame)

roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
_, roi = cv2.threshold(roi, 120, 255, cv2.THRESH_BINARY)
cv2.imshow("ROI", roi)



interrupt = cv2.waitKey(10)
if interrupt & 0xFF == 27:
break
if interrupt & 0xFF == ord('0'):
cv2.imwrite(directory+'0/'+str(count['zero'])+'.jpg', roi)
if interrupt & 0xFF == ord('1'):
cv2.imwrite(directory+'1/'+str(count['one'])+'.jpg', roi)
if interrupt & 0xFF == ord('2'):
cv2.imwrite(directory+'2/'+str(count['two'])+'.jpg', roi)
if interrupt & 0xFF == ord('3'):
cv2.imwrite(directory+'3/'+str(count['three'])+'.jpg', roi)
if interrupt & 0xFF == ord('4'):
cv2.imwrite(directory+'4/'+str(count['four'])+'.jpg', roi)
if interrupt & 0xFF == ord('5'):
cv2.imwrite(directory+'5/'+str(count['five'])+'.jpg', roi)

cap.release()
cv2.destroyAllWindows()
74 changes: 74 additions & 0 deletions collectdata1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import cv2
import os

if not os.path.exists("data1"):
os.makedirs("data1")
os.makedirs("data1/train")
os.makedirs("data1/train/hello")
os.makedirs("data1/train/peace")
os.makedirs("data1/train/best of luck")
os.makedirs("data1/train/call")
os.makedirs("data1/train/yo man!")



mode = 'train'
directory = 'data1/'+mode+'/'

cap=cv2.VideoCapture(0)

while True:
_, frame = cap.read()
frame = cv2.flip(frame, 1)

cv2.putText(frame, "sayantani-12", (150, 300), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (225,255,255), 3)

count = {'hello': len(os.listdir(directory+"hello")),
'peace': len(os.listdir(directory+"peace")),
'best of luck': len(os.listdir(directory+"best of luck")),
'call': len(os.listdir(directory+"call")),
'yo': len(os.listdir(directory+"yo man!"))}

cv2.putText(frame, "MODE : "+mode, (10, 50), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (225,255,255), 1)
cv2.putText(frame, "IMAGE COUNT", (10, 100), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (225,255,255), 1)
cv2.putText(frame, "hell0 : "+str(count['hello']), (10, 120), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,255,255), 1)
cv2.putText(frame, "peace : "+str(count['peace']), (10, 140), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,255,255), 1)
cv2.putText(frame, "best of luck : "+str(count['best of luck']), (10, 160), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,255,255), 1)
cv2.putText(frame, "call : "+str(count['call']), (10, 180), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,255,255), 1)
cv2.putText(frame, "yo! : "+str(count['yo']), (10, 200), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,255,255), 1)



x1 = int(0.5*frame.shape[1])
y1 = 10
x2 = frame.shape[1]-10
y2 = int(0.5*frame.shape[1])
cv2.rectangle(frame, (x1-1, y1-1), (x2+1, y2+1), (255,50,255) ,3)
roi = frame[y1:y2, x1:x2]
roi = cv2.resize(roi, (200, 200))
cv2.putText(frame, "R.O.I", (440, 350), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (100,225,50), 3)
cv2.imshow("Frame", frame)

roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
_, roi = cv2.threshold(roi, 120, 255, cv2.THRESH_BINARY)
cv2.imshow("ROI", roi)



interrupt = cv2.waitKey(10)
if interrupt & 0xFF == 27:
break
if interrupt & 0xFF == ord('0'):
cv2.imwrite(directory+'hello/'+str(count['hello'])+'.jpg', roi)
if interrupt & 0xFF == ord('1'):
cv2.imwrite(directory+'peace/'+str(count['peace'])+'.jpg', roi)
if interrupt & 0xFF == ord('2'):
cv2.imwrite(directory+'best of luck/'+str(count['best of luck'])+'.jpg', roi)
if interrupt & 0xFF == ord('3'):
cv2.imwrite(directory+'call/'+str(count['call'])+'.jpg', roi)
if interrupt & 0xFF == ord('4'):
cv2.imwrite(directory+'yo man!/'+str(count['yo'])+'.jpg', roi)


cap.release()
cv2.destroyAllWindows()
54 changes: 54 additions & 0 deletions prediction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from keras.models import model_from_json
import operator
import cv2

json_file = open("model-bw.json", "r")
model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(model_json)
loaded_model.load_weights("model-bw.h5")
print("Loaded model from disk")

cap = cv2.VideoCapture(0)

categories = {0: 'ZERO', 1: 'ONE', 2: 'TWO', 3: 'THREE', 4: 'FOUR', 5: 'FIVE'}

while True:
_, frame = cap.read()
frame = cv2.flip(frame, 1)

x1 = int(0.5*frame.shape[1])
y1 = 10
x2 = frame.shape[1]-10
y2 = int(0.5*frame.shape[1])

cv2.putText(frame, "Expressando - TDOC 2021", (175, 450), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (225,255,0), 3)
cv2.rectangle(frame, (x1-1, y1-1), (x2+1, y2+1), (255,255,255) ,3)
roi = frame[y1:y2, x1:x2]

roi = cv2.resize(roi, (64, 64))
roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
cv2.putText(frame, "R.O.I", (440, 350), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,225,0), 3)

_, test_image = cv2.threshold(roi, 120, 255, cv2.THRESH_BINARY)
cv2.imshow("ROI", test_image)

result = loaded_model.predict(test_image.reshape(1, 64, 64, 1))
prediction = {'ZERO': result[0][0],
'ONE': result[0][1],
'TWO': result[0][2],
'THREE': result[0][3],
'FOUR': result[0][4],
'FIVE': result[0][5]}
prediction = sorted(prediction.items(), key=operator.itemgetter(1), reverse=True) #(0.9 = FIVE, 0.7, 0.6, 0.5, 0.4)
cv2.putText(frame, "PREDICTION:", (30, 90), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)
cv2.putText(frame, prediction[0][0], (80, 130), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)
cv2.imshow("Frame", frame)

interrupt = cv2.waitKey(10)
if interrupt & 0xFF == 27:
break


cap.release()
cv2.destroyAllWindows()
53 changes: 53 additions & 0 deletions prediction1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from keras.models import model_from_json
import operator
import cv2

json_file = open("model-bw1.json", "r")
model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(model_json)
loaded_model.load_weights("model-bw1.h5")
print("Loaded model from disk")

cap = cv2.VideoCapture(0)

categories = {0: "best of luck", 1: "call", 2: "hello", 3: "peace" , 4: "yo man!"}

while True:
_, frame = cap.read()
frame = cv2.flip(frame, 1)

x1 = int(0.5*frame.shape[1])
y1 = 10
x2 = frame.shape[1]-10
y2 = int(0.5*frame.shape[1])

cv2.putText(frame, "Expressando - TDOC 2021", (175, 450), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (225,255,0), 3)
cv2.rectangle(frame, (x1-1, y1-1), (x2+1, y2+1), (255,255,255) ,3)
roi = frame[y1:y2, x1:x2]

roi = cv2.resize(roi, (64, 64))
roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
cv2.putText(frame, "R.O.I", (440, 350), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,225,0), 3)

_, test_image = cv2.threshold(roi, 120, 255, cv2.THRESH_BINARY)
cv2.imshow("ROI", test_image)

result = loaded_model.predict(test_image.reshape(1, 64, 64, 1))
prediction = {'best of luck': result[0][0],
'call': result[0][1],
'hello': result[0][2],
'peace': result[0][3],
'yo man!': result[0][4]}
prediction = sorted(prediction.items(), key=operator.itemgetter(1), reverse=True)
cv2.putText(frame, "PREDICTION:", (30, 90), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)
cv2.putText(frame, prediction[0][0], (80, 130), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)
cv2.imshow("Frame", frame)

interrupt = cv2.waitKey(10)
if interrupt & 0xFF == 27:
break


cap.release()
cv2.destroyAllWindows()
52 changes: 52 additions & 0 deletions train.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from keras.models import Sequential
from keras.layers import Convolution2D, MaxPooling2D, Flatten, Dense

classifier = Sequential()

classifier.add(Convolution2D(32, (3, 3), input_shape=(64, 64, 1), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))

classifier.add(Convolution2D(32, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))

classifier.add(Flatten())

classifier.add(Dense(units=128, activation='relu'))
classifier.add(Dense(units=5, activation='softmax'))

classifier.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])


from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

training_set = train_datagen.flow_from_directory('data1/train',
target_size=(64, 64),
batch_size=5,
color_mode='grayscale',
class_mode='categorical')

test_set = test_datagen.flow_from_directory('data1/test',
target_size=(64, 64),
batch_size=5,
color_mode='grayscale',
class_mode='categorical')

classifier.fit_generator(
training_set,
epochs=10,
validation_data=test_set)

#Saving
model_json = classifier.to_json()
with open("model-bw1.json", "w") as json_file:
json_file.write(model_json)
classifier.save_weights('model-bw1.h5')

Loading