├── pics1 ├── 1.jpg ├── 10.jpg ├── 11.jpg ├── 12.jpg ├── 13.jpg ├── 14.jpg ├── 15.jpg ├── 16.jpg ├── 17.jpg ├── 18.jpg ├── 19.jpg ├── 2.jpg ├── 20.jpg ├── 21.jpg ├── 22.jpg ├── 23.jpg ├── 24.jpg ├── 25.jpg ├── 26.jpg ├── 27.jpg ├── 28.jpg ├── 29.jpg ├── 3.jpg ├── 30.jpg ├── 31.jpg ├── 32.jpg ├── 33.jpg ├── 34.jpg ├── 35.jpg ├── 36.jpg ├── 37.jpg ├── 38.jpg ├── 39.jpg ├── 4.jpg ├── 40.jpg ├── 41.jpg ├── 42.jpg ├── 43.jpg ├── 44.jpg ├── 45.jpg ├── 46.jpg ├── 47.jpg ├── 48.jpg ├── 49.jpg ├── 5.jpg ├── 50.jpg ├── 51.jpg ├── 52.jpg ├── 53.jpg ├── 54.jpg ├── 55.jpg ├── 56.jpg ├── 57.jpg ├── 58.jpg ├── 59.jpg ├── 6.jpg ├── 60.jpg ├── 7.jpg ├── 8.jpg └── 9.jpg ├── untitled2.py ├── untitled0.py ├── README.md ├── untitled1.py ├── Mod1.py ├── abc.py ├── Mod2.py └── Q1.py /pics1/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/1.jpg -------------------------------------------------------------------------------- /pics1/10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/10.jpg -------------------------------------------------------------------------------- /pics1/11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/11.jpg -------------------------------------------------------------------------------- /pics1/12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/12.jpg -------------------------------------------------------------------------------- /pics1/13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/13.jpg -------------------------------------------------------------------------------- /pics1/14.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/14.jpg -------------------------------------------------------------------------------- /pics1/15.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/15.jpg -------------------------------------------------------------------------------- /pics1/16.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/16.jpg -------------------------------------------------------------------------------- /pics1/17.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/17.jpg -------------------------------------------------------------------------------- /pics1/18.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/18.jpg -------------------------------------------------------------------------------- /pics1/19.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/19.jpg -------------------------------------------------------------------------------- /pics1/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/2.jpg -------------------------------------------------------------------------------- /pics1/20.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/20.jpg -------------------------------------------------------------------------------- /pics1/21.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/21.jpg -------------------------------------------------------------------------------- /pics1/22.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/22.jpg -------------------------------------------------------------------------------- /pics1/23.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/23.jpg -------------------------------------------------------------------------------- /pics1/24.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/24.jpg -------------------------------------------------------------------------------- /pics1/25.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/25.jpg -------------------------------------------------------------------------------- /pics1/26.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/26.jpg -------------------------------------------------------------------------------- /pics1/27.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/27.jpg -------------------------------------------------------------------------------- /pics1/28.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/28.jpg -------------------------------------------------------------------------------- /pics1/29.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/29.jpg -------------------------------------------------------------------------------- /pics1/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/3.jpg -------------------------------------------------------------------------------- /pics1/30.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/30.jpg -------------------------------------------------------------------------------- /pics1/31.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/31.jpg -------------------------------------------------------------------------------- /pics1/32.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/32.jpg -------------------------------------------------------------------------------- /pics1/33.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/33.jpg -------------------------------------------------------------------------------- /pics1/34.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/34.jpg -------------------------------------------------------------------------------- /pics1/35.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/35.jpg -------------------------------------------------------------------------------- /pics1/36.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/36.jpg -------------------------------------------------------------------------------- /pics1/37.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/37.jpg -------------------------------------------------------------------------------- /pics1/38.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/38.jpg -------------------------------------------------------------------------------- /pics1/39.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/39.jpg -------------------------------------------------------------------------------- /pics1/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/4.jpg -------------------------------------------------------------------------------- /pics1/40.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/40.jpg -------------------------------------------------------------------------------- /pics1/41.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/41.jpg -------------------------------------------------------------------------------- /pics1/42.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/42.jpg -------------------------------------------------------------------------------- /pics1/43.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/43.jpg -------------------------------------------------------------------------------- /pics1/44.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/44.jpg -------------------------------------------------------------------------------- /pics1/45.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/45.jpg -------------------------------------------------------------------------------- /pics1/46.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/46.jpg -------------------------------------------------------------------------------- /pics1/47.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/47.jpg -------------------------------------------------------------------------------- /pics1/48.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/48.jpg -------------------------------------------------------------------------------- /pics1/49.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/49.jpg -------------------------------------------------------------------------------- /pics1/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/5.jpg -------------------------------------------------------------------------------- /pics1/50.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/50.jpg -------------------------------------------------------------------------------- /pics1/51.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/51.jpg -------------------------------------------------------------------------------- /pics1/52.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/52.jpg -------------------------------------------------------------------------------- /pics1/53.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/53.jpg -------------------------------------------------------------------------------- /pics1/54.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/54.jpg -------------------------------------------------------------------------------- /pics1/55.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/55.jpg -------------------------------------------------------------------------------- /pics1/56.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/56.jpg -------------------------------------------------------------------------------- /pics1/57.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/57.jpg -------------------------------------------------------------------------------- /pics1/58.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/58.jpg -------------------------------------------------------------------------------- /pics1/59.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/59.jpg -------------------------------------------------------------------------------- /pics1/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/6.jpg -------------------------------------------------------------------------------- /pics1/60.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/60.jpg -------------------------------------------------------------------------------- /pics1/7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/7.jpg -------------------------------------------------------------------------------- /pics1/8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/8.jpg -------------------------------------------------------------------------------- /pics1/9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aparna2071/Facelock-algorithm-using-OpenCV/HEAD/pics1/9.jpg -------------------------------------------------------------------------------- /untitled2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Thu Apr 15 17:02:51 2021 4 | 5 | @author: acer 6 | """ 7 | 8 | for a in range(0,1001): 9 | for b in range(a,1001): 10 | for c in range(b,1001): 11 | if(a+b+c==1000 and a**2+b**2==c**2): 12 | x=a*b*c 13 | print(x) -------------------------------------------------------------------------------- /untitled0.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Thu Apr 15 14:28:15 2021 4 | 5 | @author: acer 6 | """ 7 | 8 | sum=0 9 | for num in range(1, 200000): 10 | if num > 1: 11 | for i in range(2, num): 12 | if (num % i) == 0: 13 | break 14 | else: 15 | sum+=num 16 | print(sum) 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Facelock-algorithm-using-OpenCV 2 | 3 | This algorithm is used for providing security to mobile apps and websites. 4 | - For full project report:[Aparna_FacelockAlgorithmupdated (1).pdf](https://github.com/aparna2071/Facelock-algorithm-using-OpenCV/files/8366523/Aparna_FacelockAlgorithmupdated.1.pdf) 5 | 6 | - PPT: [Aparna_facelock (1)newwwww (1).pdf](https://github.com/aparna2071/Facelock-algorithm-using-OpenCV/files/8742398/Aparna_facelock.1.newwwww.1.pdf) 7 | 8 | -------------------------------------------------------------------------------- /untitled1.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sun Jul 26 17:46:21 2020 4 | 5 | @author: soura 6 | """ 7 | import cv2 8 | import numpy as np 9 | from os import listdir 10 | from os.path import isfile, join 11 | 12 | # Get the training data we previously made 13 | data_path = 'C:/su/pics/' 14 | onlyfiles = [f for f in listdir(data_path) if isfile(join(data_path, f))] 15 | 16 | # Create arrays for training data and labels 17 | Training_Data, Labels = [], [] 18 | 19 | # Open training images in our datapath 20 | # Create a numpy array for training data 21 | for i, files in enumerate(onlyfiles): 22 | image_path = data_path + onlyfiles[i] 23 | images = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) 24 | Training_Data.append(np.asarray(images, dtype=np.uint8)) 25 | Labels.append(i) 26 | 27 | # Create a numpy array for both training data and labels 28 | Labels = np.asarray(Labels, dtype=np.int32) 29 | 30 | # Initialize facial recognizer 31 | model = cv2.face.LBPHFaceRecognizer_create() 32 | 33 | # NOTE: For OpenCV 3.0 use cv2.face.createLBPHFaceRecognizer() 34 | 35 | # Let's train our model 36 | model.train(np.asarray(Training_Data), np.asarray(Labels)) 37 | print("Model trained sucessefully") 38 | -------------------------------------------------------------------------------- /Mod1.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | 4 | import cv2 5 | import numpy as np 6 | 7 | # Load HAAR face classifier 8 | face_classifier = cv2.CascadeClassifier('Haarcascades/haarcascade_frontalface_default.xml') 9 | 10 | # Load functions 11 | def face_extractor(img): 12 | # Function detects faces and returns the cropped face 13 | # If no face detected, it returns the input image 14 | 15 | gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 16 | faces = face_classifier.detectMultiScale(gray, 1.3, 5) 17 | 18 | if faces is (): 19 | return None 20 | 21 | # Crop all faces found 22 | for (x,y,w,h) in faces: 23 | cropped_face = img[y:y+h, x:x+w] 24 | 25 | return cropped_face 26 | 27 | # Initialize Webcam 28 | cap = cv2.VideoCapture(0) 29 | count = 0 30 | 31 | # Collect 1000 samples of your face from webcam input 32 | while True: 33 | 34 | ret, frame = cap.read() 35 | if face_extractor(frame) is not None: 36 | count += 1 37 | face = cv2.resize(face_extractor(frame), (200, 200)) 38 | face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY) 39 | 40 | # Save file in specified directory with unique name 41 | file_name_path = 'E:/apa/pics/' + str(count) + '.jpg' 42 | cv2.imwrite(file_name_path, face) 43 | 44 | # Put count on images and display live count 45 | cv2.putText(face, str(count), (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2) 46 | cv2.imshow('Face Cropper', face) 47 | 48 | else: 49 | print("Face not found") 50 | pass 51 | 52 | 53 | if cv2.waitKey(1) == 13 or count == 1000: #13 is the Enter Key 54 | break 55 | 56 | cap.release() 57 | cv2.destroyAllWindows() 58 | print("Collecting Samples Complete") 59 | 60 | 61 | -------------------------------------------------------------------------------- /abc.py: -------------------------------------------------------------------------------- 1 | import nltk 2 | from nltk.stem import WordNetLemmatizer 3 | lemmatizer = WordNetLemmatizer() 4 | import json 5 | import pickle 6 | 7 | import numpy as np 8 | from keras.models import Sequential 9 | from keras.layers import Dense, Activation, Dropout 10 | from keras.optimizers import SGD 11 | import random 12 | 13 | words=[] 14 | classes = [] 15 | documents = [] 16 | ignore_words = ['?', '!'] 17 | data_file = open('intents.json').read() 18 | intents = json.loads(data_file) 19 | 20 | 21 | for intent in intents['intents']: 22 | for pattern in intent['patterns']: 23 | 24 | #tokenize each word 25 | w = nltk.word_tokenize(pattern) 26 | words.extend(w) 27 | #add documents in the corpus 28 | documents.append((w, intent['tag'])) 29 | 30 | # add to our classes list 31 | if intent['tag'] not in classes: 32 | classes.append(intent['tag']) 33 | 34 | # lemmaztize and lower each word and remove duplicates 35 | words = [lemmatizer.lemmatize(w.lower()) for w in words if w not in ignore_words] 36 | words = sorted(list(set(words))) 37 | # sort classes 38 | classes = sorted(list(set(classes))) 39 | # documents = combination between patterns and intents 40 | print (len(documents), "documents") 41 | # classes = intents 42 | print (len(classes), "classes", classes) 43 | # words = all words, vocabulary 44 | print (len(words), "unique lemmatized words", words) 45 | 46 | 47 | pickle.dump(words,open('words.pkl','wb')) 48 | pickle.dump(classes,open('classes.pkl','wb')) 49 | 50 | # create our training data 51 | training = [] 52 | # create an empty array for our output 53 | output_empty = [0] * len(classes) 54 | # training set, bag of words for each sentence 55 | for doc in documents: 56 | # initialize our bag of words 57 | bag = [] 58 | # list of tokenized words for the pattern 59 | pattern_words = doc[0] 60 | # lemmatize each word - create base word, in attempt to represent related words 61 | pattern_words = [lemmatizer.lemmatize(word.lower()) for word in pattern_words] 62 | # create our bag of words array with 1, if word match found in current pattern 63 | for w in words: 64 | bag.append(1) if w in pattern_words else bag.append(0) 65 | 66 | # output is a '0' for each tag and '1' for current tag (for each pattern) 67 | output_row = list(output_empty) 68 | output_row[classes.index(doc[1])] = 1 69 | 70 | training.append([bag, output_row]) 71 | # shuffle our features and turn into np.array 72 | random.shuffle(training) 73 | training = np.array(training) 74 | # create train and test lists. X - patterns, Y - intents 75 | train_x = list(training[:,0]) 76 | train_y = list(training[:,1]) 77 | print("Training data created") 78 | 79 | 80 | # Create model - 3 layers. First layer 128 neurons, second layer 64 neurons and 3rd output layer contains number of neurons 81 | # equal to number of intents to predict output intent with softmax 82 | model = Sequential() 83 | model.add(Dense(128, input_shape=(len(train_x[0]),), activation='relu')) 84 | model.add(Dropout(0.5)) 85 | model.add(Dense(64, activation='relu')) 86 | model.add(Dropout(0.5)) 87 | model.add(Dense(len(train_y[0]), activation='softmax')) 88 | 89 | # Compile model. Stochastic gradient descent with Nesterov accelerated gradient gives good results for this model 90 | sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) 91 | model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) 92 | 93 | #fitting and saving the model 94 | hist = model.fit(np.array(train_x), np.array(train_y), epochs=200, batch_size=5, verbose=1) 95 | model.save('chatbot_model.h5', hist) 96 | 97 | print("model created") 98 | -------------------------------------------------------------------------------- /Mod2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import cv2 3 | import numpy as np 4 | from os import listdir 5 | from os.path import isfile, join 6 | 7 | # Get the training data we previously made 8 | data_path = 'E:/apa/pics/' 9 | onlyfiles = [f for f in listdir(data_path) if isfile(join(data_path, f))] 10 | 11 | # Create arrays for training data and labels 12 | Training_Data, Labels = [], [] 13 | 14 | # Open training images in our datapath 15 | # Create a numpy array for training data 16 | for i, files in enumerate(onlyfiles): 17 | image_path = data_path + onlyfiles[i] 18 | images = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) 19 | Training_Data.append(np.asarray(images, dtype=np.uint8)) 20 | Labels.append(i) 21 | 22 | # Create a numpy array for both training data and labels 23 | Labels = np.asarray(Labels, dtype=np.int32) 24 | 25 | # Initialize facial recognizer 26 | model = cv2.face.LBPHFaceRecognizer_create() 27 | 28 | # NOTE: For OpenCV 3.0 use cv2.face.createLBPHFaceRecognizer() 29 | 30 | # Let's train our model 31 | model.train(np.asarray(Training_Data), np.asarray(Labels)) 32 | print("Model trained sucessefully") 33 | 34 | face_classifier = cv2.CascadeClassifier('Haarcascades/haarcascade_frontalface_default.xml') 35 | 36 | def face_detector(img, size=0.5): 37 | 38 | # Convert image to grayscale 39 | gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 40 | faces = face_classifier.detectMultiScale(gray, 1.3, 5) 41 | if faces is (): 42 | return img, [] 43 | 44 | for (x,y,w,h) in faces: 45 | cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,255),2) 46 | roi = img[y:y+h, x:x+w] 47 | roi = cv2.resize(roi, (200, 200)) 48 | return img, roi 49 | 50 | 51 | # Open Webcam 52 | cap = cv2.VideoCapture(0) 53 | 54 | while True: 55 | 56 | ret, frame = cap.read() 57 | 58 | image, face = face_detector(frame) 59 | 60 | try: 61 | face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY) 62 | 63 | # Pass face to prediction model 64 | # "results" comprises of a tuple containing the label and the confidence value 65 | results = model.predict(face) 66 | 67 | if results[1] < 500: 68 | confidence = int( 100 * (1 - (results[1])/400) ) 69 | display_string = str(confidence) + '% Confident it is User' 70 | 71 | cv2.putText(image, display_string, (100, 120), cv2.FONT_HERSHEY_COMPLEX, 1, (255,120,150), 2) 72 | 73 | if confidence > 85: 74 | cv2.putText(image, "Unlocked", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2) 75 | cv2.imshow('Face Recognition', image ) 76 | else: 77 | cv2.putText(image, "Locked", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2) 78 | cv2.imshow('Face Recognition', image ) 79 | 80 | except: 81 | cv2.putText(image, "No Face Found", (220, 120) , cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2) 82 | cv2.putText(image, "Locked", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2) 83 | cv2.imshow('Face Recognition', image ) 84 | pass 85 | 86 | if cv2.waitKey(1) == 13: #13 is the Enter Key 87 | break 88 | 89 | cap.release() 90 | cv2.destroyAllWindows() 91 | 92 | 93 | cv2.imshow() 94 | 95 | import time 96 | 97 | # Load HAAR face classifier 98 | face_classifier = cv2.CascadeClassifier('Haarcascades/haarcascade_frontalface_default.xml') 99 | 100 | # Load functions 101 | def face_extractor(img): 102 | # Function detects faces and returns the cropped face 103 | # If no face detected, it returns the input image 104 | 105 | gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 106 | faces = face_classifier.detectMultiScale(gray, 1.3, 5) 107 | 108 | if faces is (): 109 | return None 110 | 111 | # Crop all faces found 112 | for (x,y,w,h) in faces: 113 | cropped_face = img[y:y+h, x:x+w] 114 | 115 | return cropped_face 116 | 117 | # Initialize Webcam 118 | cap = cv2.VideoCapture(0) 119 | count = 0 120 | lst=[] 121 | start_time=time.time() 122 | elapsed_time=time.time() 123 | # Collect 100 samples of your face from webcam input 124 | while True: 125 | 126 | ret, frame = cap.read() 127 | if face_extractor(frame) is not None: 128 | start_time = time.time() 129 | face = cv2.resize(face_extractor(frame), (200, 200)) 130 | face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY) 131 | 132 | # Put count on images and display live count 133 | cv2.putText(face, str(time.clock()), (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2) 134 | cv2.imshow('Face Cropper', face) 135 | 136 | else: 137 | 138 | print("Face not found") 139 | elapsed_time = time.time() - start_time 140 | lst.append(elapsed_time) 141 | seconds=0 142 | time.sleep(1) 143 | pass 144 | 145 | if cv2.waitKey(1) == 13: #13 is the Enter Key 146 | 147 | break 148 | 149 | cap.release() 150 | cv2.destroyAllWindows() 151 | print("Collecting Samples Complete") -------------------------------------------------------------------------------- /Q1.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Apr 13 14:52:02 2021 4 | 5 | @author: acer 6 | """ 7 | #Q1. If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. 8 | #Find the sum of all the multiples of 3 or 5 below 1000. 9 | 10 | sum=0 11 | for i in range(1,1000): 12 | if i%3==0 or i%5==0: 13 | sum+=i 14 | print(sum) 15 | 16 | 17 | 18 | #Q2. Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 19 | #1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... 20 | #By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. 21 | 22 | def fib(n): 23 | a=1 24 | b=2 25 | sum=0 26 | 27 | if n<0: 28 | print("Incorrect") 29 | elif n==1: 30 | sum=a 31 | return sum 32 | else: 33 | for i in range(1,n): 34 | f=a+b 35 | a=b 36 | b=f 37 | if a%2==0 and f<4000000: 38 | sum+=a 39 | return sum 40 | print(fib(4000000)) 41 | 42 | 43 | 44 | #Q10. The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. 45 | #Find the sum of all the primes below two million. 46 | 47 | sum=0 48 | for num in range(1, 20): 49 | if num > 1: 50 | for i in range(2, num): 51 | if (num % i) == 0: 52 | break 53 | else: 54 | sum+=num 55 | print(sum) 56 | 57 | 58 | #Q9. A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, 59 | # a2 + b2 = c2 60 | # For example, 32 + 42 = 9 + 16 = 25 = 52. 61 | #There exists exactly one Pythagorean triplet for which a + b + c = 1000. 62 | #Find the product abc. 63 | 64 | for a in range(0,1001): 65 | for b in range(a,1001): 66 | for c in range(b,1001): 67 | if(a+b+c==1000 and a**2+b**2==c**2): 68 | x=a*b*c 69 | print(x) 70 | 71 | 72 | 73 | #Q6. The sum of the squares of the first ten natural numbers is, 74 | #The square of the sum of the first ten natural numbers is, 75 | #Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is . 76 | #Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. 77 | 78 | 79 | n=int(input()) 80 | z=(n*n)+n 81 | x=(z*((2*n)+1))//6 82 | y= z//2 83 | print((y*y)-x) 84 | 85 | 86 | #Q7. By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. 87 | #What is the 10 001st prime number? 88 | 89 | for n in range(1,9): 90 | if n>1: 91 | for i in range(2,n): 92 | if n%i==0: 93 | break 94 | else: 95 | x=i 96 | print(x) 97 | 98 | 99 | #Q8. The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832 100 | #Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product? 101 | def split(word): 102 | return [char for char in word] 103 | 104 | s="7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450" 105 | a=split(s) 106 | for i in range(len(a)-1): 107 | for j in range(i,i+4): 108 | print(j) 109 | 110 | 111 | #Q7. By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. 112 | #What is the 10 001st prime number? 113 | def is_prime(n): 114 | for i in range(2, n): 115 | if n % i == 0: 116 | return False 117 | else: 118 | continue 119 | return True 120 | 121 | p = 0 122 | for x in range(10002): 123 | if is_prime(x): 124 | if p == 10001: 125 | print(x) 126 | break 127 | p += 1 128 | 129 | 130 | #Q5. 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. 131 | #What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? 132 | for i in range(1,11): 133 | if n%i==0: 134 | print(n) 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | --------------------------------------------------------------------------------