├── templates └── index.html ├── FaceSamples.py ├── FaceRecognition.py ├── static └── style.css ├── setting.py └── Recognition.py /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Artificial Intelligence 5 | 6 | 7 | 8 | 9 |
10 |
Artificial Intelligence
11 |
12 |
13 | 14 | 15 |
16 | 17 | -------------------------------------------------------------------------------- /FaceSamples.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | face_classifier = cv2.CascadeClassifier('C:/Users/Mayank Mittal/AppData/Local/Programs/Python/Python36-32/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml') 5 | 6 | 7 | def face_extractor(img): 8 | 9 | gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 10 | faces = face_classifier.detectMultiScale(gray,1.3,5) 11 | 12 | if faces is(): 13 | return None 14 | 15 | for(x,y,w,h) in faces: 16 | cropped_face = img[y:y+h, x:x+w] 17 | 18 | return cropped_face 19 | 20 | 21 | cap = cv2.VideoCapture(0) 22 | count = 0 23 | 24 | while True: 25 | ret, frame = cap.read() 26 | if face_extractor(frame) is not None: 27 | count+=1 28 | face = cv2.resize(face_extractor(frame),(200,200)) 29 | face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY) 30 | 31 | file_name_path = 'C:/Users/Mayank Mittal/Downloads/OpenCV/Faces/user'+str(count)+'.jpg' 32 | cv2.imwrite(file_name_path,face) 33 | 34 | cv2.putText(face,str(count),(50,50),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2) 35 | cv2.imshow('Face Cropper',face) 36 | else: 37 | print("Face not Found") 38 | pass 39 | 40 | if cv2.waitKey(1)==13 or count==100: 41 | break 42 | 43 | cap.release() 44 | cv2.destroyAllWindows() 45 | print('Colleting Samples Complete!!!') 46 | -------------------------------------------------------------------------------- /FaceRecognition.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | from os import listdir 4 | from os.path import isfile, join 5 | 6 | data_path = 'C:/Users/Mayank Mittal/Downloads/OpenCV/Faces/' 7 | onlyfiles = [f for f in listdir(data_path) if isfile(join(data_path,f))] 8 | 9 | Training_Data, Labels = [], [] 10 | 11 | for i, files in enumerate(onlyfiles): 12 | image_path = data_path + onlyfiles[i] 13 | images = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) 14 | Training_Data.append(np.asarray(images, dtype=np.uint8)) 15 | Labels.append(i) 16 | 17 | Labels = np.asarray(Labels, dtype=np.int32) 18 | 19 | model = cv2.face.LBPHFaceRecognizer_create() 20 | 21 | model.train(np.asarray(Training_Data), np.asarray(Labels)) 22 | 23 | print("Model Training Complete!!!!!") 24 | 25 | face_classifier = cv2.CascadeClassifier('C:/Users/Mayank Mittal/AppData/Local/Programs/Python/Python36-32/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml') 26 | 27 | def face_detector(img, size = 0.5): 28 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 29 | faces = face_classifier.detectMultiScale(gray,1.3,5) 30 | 31 | if faces is(): 32 | return img,[] 33 | 34 | for(x,y,w,h) in faces: 35 | cv2.rectangle(img, (x,y),(x+w,y+h),(0,255,255),2) 36 | roi = img[y:y+h, x:x+w] 37 | roi = cv2.resize(roi, (200,200)) 38 | 39 | return img,roi 40 | 41 | cap = cv2.VideoCapture(0) 42 | while True: 43 | 44 | ret, frame = cap.read() 45 | 46 | image, face = face_detector(frame) 47 | 48 | try: 49 | face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY) 50 | result = model.predict(face) 51 | 52 | if result[1] < 500: 53 | confidence = int(100*(1-(result[1])/300)) 54 | display_string = str(confidence)+'% Confidence it is user' 55 | cv2.putText(image,display_string,(100,120), cv2.FONT_HERSHEY_COMPLEX,1,(250,120,255),2) 56 | 57 | 58 | if confidence > 75: 59 | cv2.putText(image, "Unlocked", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2) 60 | cv2.imshow('Face Cropper', image) 61 | 62 | else: 63 | cv2.putText(image, "Locked", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2) 64 | cv2.imshow('Face Cropper', image) 65 | 66 | 67 | except: 68 | cv2.putText(image, "Face Not Found", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0), 2) 69 | cv2.imshow('Face Cropper', image) 70 | pass 71 | 72 | if cv2.waitKey(1)==13: 73 | break 74 | 75 | 76 | cap.release() 77 | cv2.destroyAllWindows() 78 | -------------------------------------------------------------------------------- /static/style.css: -------------------------------------------------------------------------------- 1 | .container{ 2 | width: 90%; 3 | margin: auto; 4 | } 5 | #mainh{ 6 | text-align: center; 7 | } 8 | #datasetTable{ 9 | text-align: center; 10 | width: 50%; 11 | margin: auto; 12 | } 13 | .dependentTable{ 14 | width: 33%; 15 | text-align: center; 16 | margin: 20px 0; 17 | float: left; 18 | } 19 | .dependentTablee{ 20 | width: 50%; 21 | text-align: center; 22 | margin: 20px 0; 23 | float: left; 24 | } 25 | 26 | /* Home Page Css */ 27 | @font-face { 28 | font-family: 'Open Sans'; 29 | font-style: normal; 30 | font-weight: 300; 31 | src: url('../fonts/open-sans-v15-latin-300.eot'); /* IE9 Compat Modes */ 32 | src: local('Open Sans Light'), local('OpenSans-Light'), 33 | url('../fonts/open-sans-v15-latin-300.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */ 34 | url('../fonts/open-sans-v15-latin-300.woff2') format('woff2'), /* Super Modern Browsers */ 35 | url('../fonts/open-sans-v15-latin-300.woff') format('woff'), /* Modern Browsers */ 36 | url('../fonts/open-sans-v15-latin-300.ttf') format('truetype'), /* Safari, Android, iOS */ 37 | url('../fonts/open-sans-v15-latin-300.svg#OpenSans') format('svg'); /* Legacy iOS */ 38 | } 39 | *,body,span,div,h1,h2,h3,h4,h5,h6,p,input{ 40 | margin: 0;padding: 0; 41 | } 42 | .header{ 43 | text-align: center; 44 | } 45 | .p-t-10{ 46 | padding-top: 10px; 47 | } 48 | .p-t-20{ 49 | padding-top: 20px; 50 | } 51 | .hi{ 52 | font-family: 'Open Sans', 'Calibri Light'; 53 | font-weight: 300; 54 | font-size: 10vw; 55 | color: #2d2d2d; 56 | } 57 | .hi-1{ 58 | font-family: 'Open Sans', 'Calibri Light'; 59 | font-weight: 300; 60 | font-size: 8vw; 61 | color: #2d2d2d; 62 | } 63 | .text-center{ 64 | text-align: center; 65 | } 66 | .input-1{ 67 | font-family: 'Open Sans', 'Calibri Light'; 68 | margin: 6.5px; 69 | width: 25%; 70 | padding: 0 0 0px 8px; 71 | border: 1px solid black; 72 | box-shadow: 1px 1px #888888; 73 | height: 2vw; 74 | font-size: 1vw; 75 | } 76 | .input-2{ 77 | font-family: 'Open Sans', 'Calibri Light'; 78 | color: white; 79 | background-color: #0F9E00; 80 | margin-top: 10px; 81 | width: 25.85%; 82 | padding: 0 0 0px 8px; 83 | border: none; 84 | border-radius: 2px; 85 | height: 2.2vw; 86 | font-size: 1vw; 87 | cursor: pointer; 88 | } 89 | .input-3{ 90 | font-family: 'Open Sans', 'Calibri Light'; 91 | color: white; 92 | background-color: #2B78E4; 93 | margin-top: 10px; 94 | width: 25.85%; 95 | padding: 0 0 0px 8px; 96 | border: none; 97 | border-radius: 2px; 98 | height: 2.2vw; 99 | font-size: 1vw; 100 | cursor: pointer; 101 | } 102 | .div-2{ 103 | text-align: center; 104 | } 105 | 106 | .msg{ 107 | font-family: 'Open Sans', 'Calibri Light'; 108 | font-weight: 600; 109 | font-size: 1.75vw; 110 | font-style: italic; 111 | color: #2d2d2d; 112 | } 113 | .hi-2{ 114 | font-family: 'Open Sans', 'Calibri Light'; 115 | font-weight: 300; 116 | font-size: 5.5vw; 117 | color: #2d2d2d; 118 | text-align: center; 119 | } 120 | .hi-1{ 121 | font-family: 'Open Sans', 'Calibri Light'; 122 | font-weight: 300; 123 | font-size: 4vw; 124 | color: #2d2d2d; 125 | text-align: center; 126 | } 127 | .containerh{ 128 | margin:4.5% 16%; 129 | } 130 | .mid-page1{ 131 | margin: auto; 132 | width: 50%; 133 | 134 | } 135 | .btn-4{ 136 | font-family: 'Open Sans', 'Calibri Light'; 137 | color: white; 138 | background-color: #2B78E4; 139 | margin: 0 4.8%; 140 | width: 40%; 141 | border: none; 142 | border-radius: 2px; 143 | height: 7vw; 144 | font-size: 2vw; 145 | cursor: pointer; 146 | } -------------------------------------------------------------------------------- /setting.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template, redirect, url_for, request, g, session 2 | import sqlite3 as sql,os 3 | import numpy as np 4 | import cv2,math 5 | 6 | app=Flask(__name__) 7 | app.config["CACHE_TYPE"] = "null" 8 | app.secret_key = os.urandom(24) 9 | 10 | 11 | @app.route("/") 12 | def index(): 13 | """if 'user' in session: 14 | print(session['user']) 15 | return render_template("index1.html",UserName = session['user'])""" 16 | return render_template("index.html") 17 | 18 | @app.route("/hr") 19 | def hr(): 20 | """if 'user' in session: 21 | print(session['user']) 22 | return render_template("index1.html",UserName = session['user'])""" 23 | capture = cv2.VideoCapture(0) 24 | 25 | while capture.isOpened(): 26 | 27 | # Capture frames from the camera 28 | ret, frame = capture.read() 29 | 30 | # Get hand data from the rectangle sub window 31 | cv2.rectangle(frame, (100, 100), (300, 300), (0, 255, 0), 0) 32 | crop_image = frame[100:300, 100:300] 33 | 34 | # Apply Gaussian blur 35 | blur = cv2.GaussianBlur(crop_image, (3, 3), 0) 36 | 37 | # Change color-space from BGR -> HSV 38 | hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV) 39 | 40 | # Create a binary image with where white will be skin colors and rest is black 41 | mask2 = cv2.inRange(hsv, np.array([2, 0, 0]), np.array([20, 255, 255])) 42 | 43 | # Kernel for morphological transformation 44 | kernel = np.ones((5, 5)) 45 | 46 | # Apply morphological transformations to filter out the background noise 47 | dilation = cv2.dilate(mask2, kernel, iterations=1) 48 | erosion = cv2.erode(dilation, kernel, iterations=1) 49 | 50 | # Apply Gaussian Blur and Threshold 51 | filtered = cv2.GaussianBlur(erosion, (3, 3), 0) 52 | ret, thresh = cv2.threshold(filtered, 127, 255, 0) 53 | 54 | # Show threshold image 55 | cv2.imshow("Thresholded", thresh) 56 | 57 | # Find contours 58 | image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) 59 | 60 | try: 61 | # Find contour with maximum area 62 | contour = max(contours, key=lambda x: cv2.contourArea(x)) 63 | 64 | # Create bounding rectangle around the contour 65 | x, y, w, h = cv2.boundingRect(contour) 66 | cv2.rectangle(crop_image, (x, y), (x + w, y + h), (0, 0, 255), 0) 67 | 68 | # Find convex hull 69 | hull = cv2.convexHull(contour) 70 | 71 | # Draw contour 72 | drawing = np.zeros(crop_image.shape, np.uint8) 73 | cv2.drawContours(drawing, [contour], -1, (0, 255, 0), 0) 74 | cv2.drawContours(drawing, [hull], -1, (0, 0, 255), 0) 75 | 76 | # Find convexity defects 77 | hull = cv2.convexHull(contour, returnPoints=False) 78 | defects = cv2.convexityDefects(contour, hull) 79 | 80 | # Use cosine rule to find angle of the far point from the start and end point i.e. the convex points (the finger 81 | # tips) for all defects 82 | count_defects = 0 83 | 84 | for i in range(defects.shape[0]): 85 | s, e, f, d = defects[i, 0] 86 | start = tuple(contour[s][0]) 87 | end = tuple(contour[e][0]) 88 | far = tuple(contour[f][0]) 89 | 90 | a = math.sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2) 91 | b = math.sqrt((far[0] - start[0]) ** 2 + (far[1] - start[1]) ** 2) 92 | c = math.sqrt((end[0] - far[0]) ** 2 + (end[1] - far[1]) ** 2) 93 | angle = (math.acos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)) * 180) / 3.14 94 | 95 | # if angle > 90 draw a circle at the far point 96 | if angle <= 90: 97 | count_defects += 1 98 | cv2.circle(crop_image, far, 1, [0, 0, 255], -1) 99 | 100 | cv2.line(crop_image, start, end, [0, 255, 0], 2) 101 | 102 | # Print number of fingers 103 | if count_defects == 0: 104 | cv2.putText(frame, "ONE", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,0,255),2) 105 | elif count_defects == 1: 106 | cv2.putText(frame, "TWO", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,0,255), 2) 107 | elif count_defects == 2: 108 | cv2.putText(frame, "THREE", (5, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,0,255), 2) 109 | elif count_defects == 3: 110 | cv2.putText(frame, "FOUR", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,0,255), 2) 111 | elif count_defects == 4: 112 | cv2.putText(frame, "FIVE", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,0,255), 2) 113 | else: 114 | pass 115 | except: 116 | pass 117 | 118 | # Show required images 119 | cv2.imshow("Gesture", frame) 120 | all_image = np.hstack((drawing, crop_image)) 121 | cv2.imshow('Contours', all_image) 122 | 123 | # Close the camera if 'q' is pressed 124 | if cv2.waitKey(1) == ord('q'): 125 | break 126 | 127 | capture.release() 128 | cv2.destroyAllWindows() 129 | 130 | return redirect("/") 131 | 132 | if __name__=="__main__": 133 | app.run(debug=True,port=8000) 134 | -------------------------------------------------------------------------------- /Recognition.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk # python 3 2 | from tkinter import font as tkfont,Canvas, Label, Tk, StringVar, Button, LEFT,messagebox 3 | import cv2, math 4 | import numpy as np 5 | from os import listdir 6 | from os.path import isfile, join 7 | 8 | 9 | def regfacetraining(): 10 | 11 | data_path = 'C:/Users/Ranajoy/Downloads/OpenCV/Faces/' 12 | onlyfiles = [f for f in listdir(data_path) if isfile(join(data_path,f))] 13 | 14 | Training_Data, Labels = [], [] 15 | 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 | Labels = np.asarray(Labels, dtype=np.int32) 23 | 24 | model = cv2.face.LBPHFaceRecognizer_create() 25 | 26 | model.train(np.asarray(Training_Data), np.asarray(Labels)) 27 | 28 | print("Model Training Complete!!!!!") 29 | 30 | face_classifier = cv2.CascadeClassifier('C:/Users/Ranajoy/AppData/Local/Programs/Python/Python36-32/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml') 31 | 32 | def face_detector(img, size = 0.5): 33 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 34 | faces = face_classifier.detectMultiScale(gray,1.3,5) 35 | 36 | if faces is(): 37 | return img,[] 38 | 39 | for(x,y,w,h) in faces: 40 | cv2.rectangle(img, (x,y),(x+w,y+h),(0,255,255),2) 41 | roi = img[y:y+h, x:x+w] 42 | roi = cv2.resize(roi, (200,200)) 43 | 44 | return img,roi 45 | 46 | cap = cv2.VideoCapture(0) 47 | while True: 48 | 49 | ret, frame = cap.read() 50 | 51 | image, face = face_detector(frame) 52 | 53 | try: 54 | face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY) 55 | result = model.predict(face) 56 | 57 | if result[1] < 500: 58 | confidence = int(100*(1-(result[1])/300)) 59 | display_string = str(confidence)+'% Confidence it is user' 60 | cv2.putText(image,display_string,(100,120), cv2.FONT_HERSHEY_COMPLEX,1,(250,120,255),2) 61 | 62 | 63 | if confidence > 75: 64 | cv2.putText(image, "Unlocked", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2) 65 | cv2.imshow('Face Cropper', image) 66 | 67 | else: 68 | cv2.putText(image, "Locked", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2) 69 | cv2.imshow('Face Cropper', image) 70 | 71 | 72 | except: 73 | cv2.putText(image, "Face Not Found", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0), 2) 74 | cv2.imshow('Face Cropper', image) 75 | pass 76 | 77 | if cv2.waitKey(1)==13: 78 | break 79 | 80 | cap.release() 81 | return cv2.destroyAllWindows() 82 | 83 | def regface(): 84 | 85 | face_classifier = cv2.CascadeClassifier('C:/Users/Ranajoy/AppData/Local/Programs/Python/Python36-32/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml') 86 | 87 | 88 | def face_extractor(img): 89 | 90 | gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 91 | faces = face_classifier.detectMultiScale(gray,1.3,5) 92 | 93 | if faces is(): 94 | return None 95 | 96 | for(x,y,w,h) in faces: 97 | cropped_face = img[y:y+h, x:x+w] 98 | 99 | return cropped_face 100 | 101 | 102 | cap = cv2.VideoCapture(0) 103 | count = 0 104 | 105 | while True: 106 | ret, frame = cap.read() 107 | if face_extractor(frame) is not None: 108 | count+=1 109 | face = cv2.resize(face_extractor(frame),(200,200)) 110 | face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY) 111 | 112 | file_name_path = 'C:/Users/Ranajoy/Downloads/OpenCV/Faces/user'+str(count)+'.jpg' 113 | cv2.imwrite(file_name_path,face) 114 | 115 | cv2.putText(face,str(count),(50,50),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2) 116 | cv2.imshow('Face Cropper',face) 117 | else: 118 | print("Face not Found") 119 | pass 120 | 121 | if cv2.waitKey(1)==13 or count==100: 122 | break 123 | 124 | cap.release() 125 | print('Colleting Samples Complete!!!') 126 | 127 | return cv2.destroyAllWindows() 128 | 129 | 130 | def reg(): 131 | # Open Camera 132 | capture = cv2.VideoCapture(0) 133 | 134 | while capture.isOpened(): 135 | 136 | # Capture frames from the camera 137 | ret, frame = capture.read() 138 | 139 | # Get hand data from the rectangle sub window 140 | cv2.rectangle(frame, (100, 100), (300, 300), (0, 255, 0), 0) 141 | crop_image = frame[100:300, 100:300] 142 | 143 | # Apply Gaussian blur 144 | blur = cv2.GaussianBlur(crop_image, (3, 3), 0) 145 | 146 | # Change color-space from BGR -> HSV 147 | hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV) 148 | 149 | # Create a binary image with where white will be skin colors and rest is black 150 | mask2 = cv2.inRange(hsv, np.array([2, 0, 0]), np.array([20, 255, 255])) 151 | 152 | # Kernel for morphological transformation 153 | kernel = np.ones((5, 5)) 154 | 155 | # Apply morphological transformations to filter out the background noise 156 | dilation = cv2.dilate(mask2, kernel, iterations=1) 157 | erosion = cv2.erode(dilation, kernel, iterations=1) 158 | 159 | # Apply Gaussian Blur and Threshold 160 | filtered = cv2.GaussianBlur(erosion, (3, 3), 0) 161 | ret, thresh = cv2.threshold(filtered, 127, 255, 0) 162 | 163 | # Show threshold image 164 | cv2.imshow("Thresholded", thresh) 165 | 166 | # Find contours 167 | image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) 168 | 169 | try: 170 | # Find contour with maximum area 171 | contour = max(contours, key=lambda x: cv2.contourArea(x)) 172 | 173 | # Create bounding rectangle around the contour 174 | x, y, w, h = cv2.boundingRect(contour) 175 | cv2.rectangle(crop_image, (x, y), (x + w, y + h), (0, 0, 255), 0) 176 | 177 | # Find convex hull 178 | hull = cv2.convexHull(contour) 179 | 180 | # Draw contour 181 | drawing = np.zeros(crop_image.shape, np.uint8) 182 | cv2.drawContours(drawing, [contour], -1, (0, 255, 0), 0) 183 | cv2.drawContours(drawing, [hull], -1, (0, 0, 255), 0) 184 | 185 | # Find convexity defects 186 | hull = cv2.convexHull(contour, returnPoints=False) 187 | defects = cv2.convexityDefects(contour, hull) 188 | 189 | # Use cosine rule to find angle of the far point from the start and end point i.e. the convex points (the finger 190 | # tips) for all defects 191 | count_defects = 0 192 | 193 | for i in range(defects.shape[0]): 194 | s, e, f, d = defects[i, 0] 195 | start = tuple(contour[s][0]) 196 | end = tuple(contour[e][0]) 197 | far = tuple(contour[f][0]) 198 | 199 | a = math.sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2) 200 | b = math.sqrt((far[0] - start[0]) ** 2 + (far[1] - start[1]) ** 2) 201 | c = math.sqrt((end[0] - far[0]) ** 2 + (end[1] - far[1]) ** 2) 202 | angle = (math.acos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)) * 180) / 3.14 203 | 204 | # if angle > 90 draw a circle at the far point 205 | if angle <= 90: 206 | count_defects += 1 207 | cv2.circle(crop_image, far, 1, [0, 0, 255], -1) 208 | 209 | cv2.line(crop_image, start, end, [0, 255, 0], 2) 210 | 211 | # Print number of fingers 212 | if count_defects == 0: 213 | cv2.putText(frame, "ONE", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,0,255),2) 214 | elif count_defects == 1: 215 | cv2.putText(frame, "TWO", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,0,255), 2) 216 | elif count_defects == 2: 217 | cv2.putText(frame, "THREE", (5, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,0,255), 2) 218 | elif count_defects == 3: 219 | cv2.putText(frame, "FOUR", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,0,255), 2) 220 | elif count_defects == 4: 221 | cv2.putText(frame, "FIVE", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,0,255), 2) 222 | else: 223 | pass 224 | except: 225 | pass 226 | 227 | # Show required images 228 | cv2.imshow("Gesture", frame) 229 | all_image = np.hstack((drawing, crop_image)) 230 | cv2.imshow('Contours', all_image) 231 | 232 | # Close the camera if 'q' is pressed 233 | if cv2.waitKey(1) == ord('q'): 234 | break 235 | 236 | capture.release() 237 | return cv2.destroyAllWindows() 238 | 239 | 240 | class SampleApp(tk.Tk): 241 | 242 | def __init__(self, *args, **kwargs): 243 | tk.Tk.__init__(self, *args, **kwargs) 244 | 245 | self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic") 246 | 247 | # the container is where we'll stack a bunch of frames 248 | # on top of each other, then the one we want visible 249 | # will be raised above the others 250 | container = tk.Frame(self) 251 | container.pack(side="top", fill="both", expand=True) 252 | container.grid_rowconfigure(0, weight=1) 253 | container.grid_columnconfigure(0, weight=1) 254 | 255 | self.frames = {} 256 | for F in (StartPage, PageOne): 257 | page_name = F.__name__ 258 | frame = F(parent=container, controller=self) 259 | self.frames[page_name] = frame 260 | 261 | # put all of the pages in the same location; 262 | # the one on the top of the stacking order 263 | # will be the one that is visible. 264 | frame.grid(row=0, column=0, sticky="nsew") 265 | 266 | self.show_frame("StartPage") 267 | 268 | def show_frame(self, page_name): 269 | '''Show a frame for the given page name''' 270 | frame = self.frames[page_name] 271 | frame.tkraise() 272 | 273 | 274 | class StartPage(tk.Frame): 275 | 276 | def __init__(self, parent, controller): 277 | tk.Frame.__init__(self, parent) 278 | self.controller = controller 279 | label = tk.Label(self, text=" Fun With Cam :) ", font=controller.title_font) 280 | label.pack(side="top", fill="x", pady=10) 281 | 282 | button1 = tk.Button(self, text="reg", command=reg) 283 | button2 = tk.Button(self, text="regface", command=regface) 284 | button3 = tk.Button(self, text="regfacetraining", command=regfacetraining) 285 | 286 | button1.pack() 287 | button2.pack() 288 | button3.pack() 289 | label = tk.Label(self, text="", font=controller.title_font) 290 | label.pack(side="top", fill="x", pady=2) 291 | 292 | 293 | class PageOne(tk.Frame): 294 | 295 | def __init__(self, parent, controller): 296 | tk.Frame.__init__(self, parent) 297 | 298 | 299 | if __name__ == "__main__": 300 | app = SampleApp() 301 | app.mainloop() 302 | --------------------------------------------------------------------------------