From bd2a52fb8e1247d243197dded52478a7e0785d9c Mon Sep 17 00:00:00 2001 From: aayushray Date: Mon, 25 Oct 2021 00:07:38 +0530 Subject: [PATCH 1/6] task1 --- Task1.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Task1.py diff --git a/Task1.py b/Task1.py new file mode 100644 index 0000000..c27141a --- /dev/null +++ b/Task1.py @@ -0,0 +1,67 @@ +import cv2 + +(width,height) = (130,100) + +cap = cv2.VideoCapture(0) + +while (cap.isOpened()): + ret, img = cap.read() + img = cv2.flip(img, 1) # will return the mirror view + cv2.rectangle(img, (20, 20), (250,250), (255, 0, 0), 2) #BGR + cv2.imshow("BGR Output", img) + + img1 = img[20:250, 20:250] # Extraction + imCopy = img1.copy() #Copy of the extraction + gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) + blur = cv2.GaussianBlur(gray, (5,5), 0) + cv2.imshow("Blur",blur) + + ret, thresh1 = cv2.threshold(blur, 10, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) #0 to 255 + hand_resize = cv2.resize(thresh1, (width,height)) + cv2.imshow("Threshold", thresh1) + + + + contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #contour tells us the minimum nuber of edges required to express a shape + cv2.drawContours(imCopy, contours, -1, (0, 255, 0)) + cv2.imshow('RETR TREE', imCopy) + + k = 0xFF & cv2.waitKey(10) + if k == 27: + break + +cap.release() +cv2.destroyAllWindows() + +# import cv2 + +# (width, height) = (130, 100) + +# # url = '/video' +# # cap=cv2.VideoCapture(url) + + +# cap=cv2.VideoCapture(0) + +# while (cap.isOpened()): +# ret, img = cap.read() +# img=cv2.flip(img, 1) +# cv2.rectangle(img, (20, 20), (250, 250), (255, 0, 0), 3) +# cv2.imshow("RGB Output", img) +# img1 = img[20:250,20:250] +# imCopy = img1.copy() +# gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) +# blur = cv2.GaussianBlur(gray, (5, 5), 0) +# ret, thresh1 = cv2.threshold(blur, 10, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) +# hand_resize = cv2.resize(thresh1, (width, height)) +# 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() From cf8f3590bfe77e7879186136a29cca7768cc16f7 Mon Sep 17 00:00:00 2001 From: Ayush Ray Date: Mon, 25 Oct 2021 00:12:55 +0530 Subject: [PATCH 2/6] Update Task1.py --- Task1.py | 33 --------------------------------- 1 file changed, 33 deletions(-) diff --git a/Task1.py b/Task1.py index c27141a..84fa33b 100644 --- a/Task1.py +++ b/Task1.py @@ -32,36 +32,3 @@ cap.release() cv2.destroyAllWindows() - -# import cv2 - -# (width, height) = (130, 100) - -# # url = '/video' -# # cap=cv2.VideoCapture(url) - - -# cap=cv2.VideoCapture(0) - -# while (cap.isOpened()): -# ret, img = cap.read() -# img=cv2.flip(img, 1) -# cv2.rectangle(img, (20, 20), (250, 250), (255, 0, 0), 3) -# cv2.imshow("RGB Output", img) -# img1 = img[20:250,20:250] -# imCopy = img1.copy() -# gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) -# blur = cv2.GaussianBlur(gray, (5, 5), 0) -# ret, thresh1 = cv2.threshold(blur, 10, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) -# hand_resize = cv2.resize(thresh1, (width, height)) -# 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() From 437d795a1473d8a32d0510dc446a561872204f73 Mon Sep 17 00:00:00 2001 From: Ayush Ray Date: Mon, 25 Oct 2021 00:13:57 +0530 Subject: [PATCH 3/6] Delete Task1.py --- Task1.py | 34 ---------------------------------- 1 file changed, 34 deletions(-) delete mode 100644 Task1.py diff --git a/Task1.py b/Task1.py deleted file mode 100644 index 84fa33b..0000000 --- a/Task1.py +++ /dev/null @@ -1,34 +0,0 @@ -import cv2 - -(width,height) = (130,100) - -cap = cv2.VideoCapture(0) - -while (cap.isOpened()): - ret, img = cap.read() - img = cv2.flip(img, 1) # will return the mirror view - cv2.rectangle(img, (20, 20), (250,250), (255, 0, 0), 2) #BGR - cv2.imshow("BGR Output", img) - - img1 = img[20:250, 20:250] # Extraction - imCopy = img1.copy() #Copy of the extraction - gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) - blur = cv2.GaussianBlur(gray, (5,5), 0) - cv2.imshow("Blur",blur) - - ret, thresh1 = cv2.threshold(blur, 10, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) #0 to 255 - hand_resize = cv2.resize(thresh1, (width,height)) - cv2.imshow("Threshold", thresh1) - - - - contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #contour tells us the minimum nuber of edges required to express a shape - cv2.drawContours(imCopy, contours, -1, (0, 255, 0)) - cv2.imshow('RETR TREE', imCopy) - - k = 0xFF & cv2.waitKey(10) - if k == 27: - break - -cap.release() -cv2.destroyAllWindows() From f7ce1b05e193043239be40fa8b7799f275042fe5 Mon Sep 17 00:00:00 2001 From: aayushray Date: Thu, 28 Oct 2021 01:05:41 +0530 Subject: [PATCH 4/6] Task 3: Training of Data --- Task 3/train_model.py | 52 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Task 3/train_model.py diff --git a/Task 3/train_model.py b/Task 3/train_model.py new file mode 100644 index 0000000..ad5707a --- /dev/null +++ b/Task 3/train_model.py @@ -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=6, 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) #epoch + +training_set = train_datagen.flow_from_directory('data/train', + target_size=(64, 64), + batch_size=5, + color_mode='grayscale', + class_mode='categorical') + +test_set = test_datagen.flow_from_directory('data/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-bw.json", "w") as json_file: + json_file.write(model_json) +classifier.save_weights('model-bw.h5') + From a2d7962c2951cd2ad79d4fb9869cda658fa046b9 Mon Sep 17 00:00:00 2001 From: aayushray Date: Thu, 28 Oct 2021 01:15:40 +0530 Subject: [PATCH 5/6] Task 4: Prediction of Model --- Task 4/prediction.py | 54 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Task 4/prediction.py diff --git a/Task 4/prediction.py b/Task 4/prediction.py new file mode 100644 index 0000000..5151d08 --- /dev/null +++ b/Task 4/prediction.py @@ -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() From 03695953114349845395afb2d6fb5a8680ff243b Mon Sep 17 00:00:00 2001 From: Ayush Ray Date: Thu, 28 Oct 2021 01:16:56 +0530 Subject: [PATCH 6/6] Delete Task 3 directory --- Task 3/train_model.py | 52 ------------------------------------------- 1 file changed, 52 deletions(-) delete mode 100644 Task 3/train_model.py diff --git a/Task 3/train_model.py b/Task 3/train_model.py deleted file mode 100644 index ad5707a..0000000 --- a/Task 3/train_model.py +++ /dev/null @@ -1,52 +0,0 @@ -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=6, 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) #epoch - -training_set = train_datagen.flow_from_directory('data/train', - target_size=(64, 64), - batch_size=5, - color_mode='grayscale', - class_mode='categorical') - -test_set = test_datagen.flow_from_directory('data/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-bw.json", "w") as json_file: - json_file.write(model_json) -classifier.save_weights('model-bw.h5') -