File size: 3,134 Bytes
f13361b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""

@author: Santhosh R

"""
import cv2, os
import shutil
import csv
import numpy as np
from PIL import Image, ImageTk
import pandas as pd


name , Id = '',''
dic = {
    'Name' : name,
    'Ids' : Id
}
def store_data():
    global name,Id,dic
    name = str(input("Enter Name  "))
   
    Id  = str(input("Enter Id   "))
   
    dic = {
        'Ids' : Id,
        'Name': name
    }
    c = dic
    return  c

#Fucntion to check if entered ID is number or not
def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        pass
 
    try:
        import unicodedata
        unicodedata.numeric(s)
        return True
    except (TypeError, ValueError):
        pass
 
    return False

def TakeImages():
    dict1 = store_data()
    
    #print(dict1)
    #name = "Santhu"
    #Id = '1'
    if (name.isalpha() and is_number(Id)):
        #Checking Id if it is 1 we are rewring the profile else just updating csv
        if Id == '1':
            fieldnames = ['Name','Ids']
            with open('Profile.csv','w') as f:
                writer = csv.DictWriter(f, fieldnames =fieldnames)
                writer.writeheader()
                writer.writerow(dict1)
        else:
            fieldnames = ['Name','Ids']
            with open('Profile.csv','a+') as f:
                writer = csv.DictWriter(f, fieldnames =fieldnames)
                #writer.writeheader()
                writer.writerow(dict1)
        cam = cv2.VideoCapture(0)

        #Haarcascade file for detctionof face
        harcascadePath = "hh.xml"
        detector = cv2.CascadeClassifier(harcascadePath)
        sampleNum = 0
        while (True):
            ret, img = cam.read()
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            faces = detector.detectMultiScale(gray, 1.3, 5)
            for (x, y, w, h) in faces:
                cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)
                # Incrementing sample number
                sampleNum = sampleNum + 1
                # Saving the captured face in the dataset folder TrainingImage
                cv2.imwrite("TrainingImage\ " + name + "." + Id + '.' + str(sampleNum) + ".jpg", gray[y:y + h, x:x + w])
                # display the frame
            cv2.imshow('Cpaturing Face for Login ', img)
        
            # wait for 100 miliseconds
            if cv2.waitKey(100) & 0xFF == ord('q'):
                break
            # break if the sample number is morethan 60
            elif sampleNum > 60:
                break
            
        
        cam.release()
        cv2.destroyAllWindows()
        res = "Images Saved for Name : " + name + " with ID  " + Id
        print(res)
        print(' Images save location is TrainingImage\ ')
      
        
    else:
        if(name.isalpha()):
            print('Enter Proper Id')
        elif(is_number(Id)):
            print('Enter Proper name')
        else:
            print('Enter Proper Id and Name')
                    
        

TakeImages()