0% found this document useful (0 votes)
41 views7 pages

Coding Detection Dan Training

The document describes a facial recognition system that detects faces from a webcam video stream, collects training images into labeled folders, trains a recognizer model on the images, and predicts recognized faces. The system uses OpenCV for face detection, collects 30 images per person into labeled folders, trains a Local Binary Patterns Histograms (LBPH) model, and displays prediction results. It includes buttons to launch the dataset collection and model training processes.

Uploaded by

NURHIKMA ARIFIN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views7 pages

Coding Detection Dan Training

The document describes a facial recognition system that detects faces from a webcam video stream, collects training images into labeled folders, trains a recognizer model on the images, and predicts recognized faces. The system uses OpenCV for face detection, collects 30 images per person into labeled folders, trains a Local Binary Patterns Histograms (LBPH) model, and displays prediction results. It includes buttons to launch the dataset collection and model training processes.

Uploaded by

NURHIKMA ARIFIN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Detection

import os,cv2,numpy as np

if not os.path.exists('dataset'):

os.makedirs('dataset')

face = os.listdir('dataset')

facedetect=cv2.CascadeClassifier(cv2.data.haarcascades +

'haarcascade_frontalface_default.xml')

recognizer = cv2.face.LBPHFaceRecognizer_create()

def dataset():

nama = simpledialog.askstring("Input Nama", "Masukkan Nama Anda!")

if not nama:

messagebox.showinfo("Informasi", "Isikan Nama Anda!")

return

cam = cv2.VideoCapture(1)

cam.set(3, 360) # set lebar video

cam.set(4, 270) # set tinggi video

if not os.path.exists('dataset/'+nama):

os.makedirs('dataset/'+nama.lower())

count = 0

while(True):

ret, img = cam.read()

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = facedetect.detectMultiScale(gray, 1.3, 5)

for (x,y,w,h) in faces:

cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)


count += 1

cv2.imwrite("dataset/" + str(nama) + '/' + str(count) + ".jpg", gray[y:y+h,x:x+w])

cv2.putText(img, str(count), (x + 5, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 1,

(255, 255, 255), 2)

cv2.imshow('img',img)

k = cv2.waitKey(100) & 0xff # Tekan 'ESC' untuk keluar dari Video

if k == 27:

break

if count == 30: # Mengambil foto wajah sebanyak 30 dan menghentikan Video

break

cam.release()

cv2.destroyAllWindows()

messagebox.showinfo("Informasi", "Dataset "+nama+" telah berhasil

ditambahkan!")

def training():

face = os.listdir('dataset')

facemodels=[]

ids = []

cnt=0

for v in face:

for f in os.listdir(os.path.join('dataset/', v)):

PIL_img = Image.open(os.path.join('dataset/', v, f)).convert('L') # Mengubah ke

Grayscale

img_numpy = np.array(PIL_img, 'uint8')


faces = facedetect.detectMultiScale(img_numpy)

for (x, y, w, h) in faces:

facemodels.append(img_numpy[y:y + h, x:x + w])

ids.append(cnt)

cnt+=1

recognizer.train(facemodels,np.array(ids))

recognizer.write('models.yml')

messagebox.showinfo("Informasi", "Training telah berhasil!")

root = Tk()

root.title("Pengambilan Data")

root.geometry('480x360')

root.eval('tk::PlaceWindow . center')

picPanel = Label(root, text='Preprocessed',borderwidth=2, relief='groove')

picPanel.place(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")

keterangan.place(x=100, y=300, width=50, height=20)

person = Label(root,borderwidth=2, relief='groove')

person.place(x=150, y=300, width=100, height=20)

root.mainloop()
2. Training Data

from tkinter import *

from tkinter import simpledialog,messagebox

from PIL import ImageTk, Image

import os,cv2,numpy as np

if not os.path.exists('dataset'):

os.makedirs('dataset')

face = os.listdir('dataset')

facedetect=cv2.CascadeClassifier(cv2.data.haarcascades +

'haarcascade_frontalface_default.xml')

recognizer = cv2.face.LBPHFaceRecognizer_create()

def dataset():

nama = simpledialog.askstring("Input Nama", "Masukkan Nama Anda!")

if not nama:

messagebox.showinfo("Informasi", "Isikan Nama Anda!")

return

cam = cv2.VideoCapture(1)

cam.set(3, 360) # set lebar video

cam.set(4, 270) # set tinggi video

if not os.path.exists('dataset/'+nama):

os.makedirs('dataset/'+nama.lower())

count = 0

while(True):

ret, img = cam.read()

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


faces = facedetect.detectMultiScale(gray, 1.3, 5)

for (x,y,w,h) in faces:

cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)

count += 1

cv2.imwrite("dataset/" + str(nama) + '/' + str(count) + ".jpg", gray[y:y+h,x:x+w])

cv2.putText(img, str(count), (x + 5, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 1,

(255, 255, 255), 2)

cv2.imshow('img',img)

k = cv2.waitKey(100) & 0xff # Tekan 'ESC' untuk keluar dari Video

if k == 27:

break

if count == 30: # Mengambil foto wajah sebanyak 30 dan menghentikan Video

break

cam.release()

cv2.destroyAllWindows()

messagebox.showinfo("Informasi", "Dataset "+nama+" telah berhasil

ditambahkan!")

def training():

face = os.listdir('dataset')

facemodels=[]

ids = []

cnt=0

for v in face:

for f in os.listdir(os.path.join('dataset/', v)):


PIL_img = Image.open(os.path.join('dataset/', v, f)).convert('L') # Mengubah ke

Grayscale

img_numpy = np.array(PIL_img, 'uint8')

faces = facedetect.detectMultiScale(img_numpy)

for (x, y, w, h) in faces:

facemodels.append(img_numpy[y:y + h, x:x + w])

ids.append(cnt)

cnt+=1

recognizer.train(facemodels,np.array(ids))

recognizer.write('models.yml')

messagebox.showinfo("Informasi", "Training telah berhasil!")

root = Tk()

root.title("Pengambilan Data")

root.geometry('480x360')

root.eval('tk::PlaceWindow . center')

picPanel = Label(root, text='Preprocessed',borderwidth=2, relief='groove')

picPanel.place(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")

keterangan.place(x=100, y=300, width=50, height=20)

person = Label(root,borderwidth=2, relief='groove')


person.place(x=150, y=300, width=100, height=20)

root.mainloop()

You might also like