1.
Detection
import os,cv2,numpy as np
if not [Link]('dataset'):
[Link]('dataset')
face = [Link]('dataset')
facedetect=[Link]([Link] +
'haarcascade_frontalface_default.xml')
recognizer = [Link].LBPHFaceRecognizer_create()
def dataset():
nama = [Link]("Input Nama", "Masukkan Nama Anda!")
if not nama:
[Link]("Informasi", "Isikan Nama Anda!")
return
cam = [Link](1)
[Link](3, 360) # set lebar video
[Link](4, 270) # set tinggi video
if not [Link]('dataset/'+nama):
[Link]('dataset/'+[Link]())
count = 0
while(True):
ret, img = [Link]()
gray = [Link](img, cv2.COLOR_BGR2GRAY)
faces = [Link](gray, 1.3, 5)
for (x,y,w,h) in faces:
[Link](img, (x, y), (x + w, y + h), (0, 0, 255), 2)
count += 1
[Link]("dataset/" + str(nama) + '/' + str(count) + ".jpg", gray[y:y+h,x:x+w])
[Link](img, str(count), (x + 5, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 1,
(255, 255, 255), 2)
[Link]('img',img)
k = [Link](100) & 0xff # Tekan 'ESC' untuk keluar dari Video
if k == 27:
break
if count == 30: # Mengambil foto wajah sebanyak 30 dan menghentikan Video
break
[Link]()
[Link]()
[Link]("Informasi", "Dataset "+nama+" telah berhasil
ditambahkan!")
def training():
face = [Link]('dataset')
facemodels=[]
ids = []
cnt=0
for v in face:
for f in [Link]([Link]('dataset/', v)):
PIL_img = [Link]([Link]('dataset/', v, f)).convert('L') # Mengubah ke
Grayscale
img_numpy = [Link](PIL_img, 'uint8')
faces = [Link](img_numpy)
for (x, y, w, h) in faces:
[Link](img_numpy[y:y + h, x:x + w])
[Link](cnt)
cnt+=1
[Link](facemodels,[Link](ids))
[Link]('[Link]')
[Link]("Informasi", "Training telah berhasil!")
root = Tk()
[Link]("Pengambilan Data")
[Link]('480x360')
[Link]('tk::PlaceWindow . center')
picPanel = Label(root, text='Preprocessed',borderwidth=2, relief='groove')
[Link](x=100, y=25, width=360, height=270)
btn_dataset = Button(root, text="Dataset", command=dataset)
btn_dataset.place(x=15, y=25, width=75, height=20)
btn_training = Button(root, text="Training", command=training)
btn_training.place(x=15, y=50, width=75, height=20)
keterangan = Label(root,text="Prediksi")
[Link](x=100, y=300, width=50, height=20)
person = Label(root,borderwidth=2, relief='groove')
[Link](x=150, y=300, width=100, height=20)
[Link]()
2. Training Data
from tkinter import *
from tkinter import simpledialog,messagebox
from PIL import ImageTk, Image
import os,cv2,numpy as np
if not [Link]('dataset'):
[Link]('dataset')
face = [Link]('dataset')
facedetect=[Link]([Link] +
'haarcascade_frontalface_default.xml')
recognizer = [Link].LBPHFaceRecognizer_create()
def dataset():
nama = [Link]("Input Nama", "Masukkan Nama Anda!")
if not nama:
[Link]("Informasi", "Isikan Nama Anda!")
return
cam = [Link](1)
[Link](3, 360) # set lebar video
[Link](4, 270) # set tinggi video
if not [Link]('dataset/'+nama):
[Link]('dataset/'+[Link]())
count = 0
while(True):
ret, img = [Link]()
gray = [Link](img, cv2.COLOR_BGR2GRAY)
faces = [Link](gray, 1.3, 5)
for (x,y,w,h) in faces:
[Link](img, (x, y), (x + w, y + h), (0, 0, 255), 2)
count += 1
[Link]("dataset/" + str(nama) + '/' + str(count) + ".jpg", gray[y:y+h,x:x+w])
[Link](img, str(count), (x + 5, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 1,
(255, 255, 255), 2)
[Link]('img',img)
k = [Link](100) & 0xff # Tekan 'ESC' untuk keluar dari Video
if k == 27:
break
if count == 30: # Mengambil foto wajah sebanyak 30 dan menghentikan Video
break
[Link]()
[Link]()
[Link]("Informasi", "Dataset "+nama+" telah berhasil
ditambahkan!")
def training():
face = [Link]('dataset')
facemodels=[]
ids = []
cnt=0
for v in face:
for f in [Link]([Link]('dataset/', v)):
PIL_img = [Link]([Link]('dataset/', v, f)).convert('L') # Mengubah ke
Grayscale
img_numpy = [Link](PIL_img, 'uint8')
faces = [Link](img_numpy)
for (x, y, w, h) in faces:
[Link](img_numpy[y:y + h, x:x + w])
[Link](cnt)
cnt+=1
[Link](facemodels,[Link](ids))
[Link]('[Link]')
[Link]("Informasi", "Training telah berhasil!")
root = Tk()
[Link]("Pengambilan Data")
[Link]('480x360')
[Link]('tk::PlaceWindow . center')
picPanel = Label(root, text='Preprocessed',borderwidth=2, relief='groove')
[Link](x=100, y=25, width=360, height=270)
btn_dataset = Button(root, text="Dataset", command=dataset)
btn_dataset.place(x=15, y=25, width=75, height=20)
btn_training = Button(root, text="Training", command=training)
btn_training.place(x=15, y=50, width=75, height=20)
keterangan = Label(root,text="Prediksi")
[Link](x=100, y=300, width=50, height=20)
person = Label(root,borderwidth=2, relief='groove')
[Link](x=150, y=300, width=100, height=20)
[Link]()