Vous êtes sur la page 1sur 4

import cv2

import os
import sys
import numpy as np

f= open("prediksi.txt","w+")

subjects = [""]
testing_data= sys.argv[1]
path = 'training-data-cat/'
dirList=os.listdir(path)
dirList.sort()
status_ketemu = False
datapemilik = False
min_val = 999999
jenis_kucing = ''

# TEMPLATE MATCHING
face_cascade = cv2.CascadeClassifier('opencv-files/catface.xml')
for dname in dirList:
path2 = path+"/"+dname
dirList2=os.listdir(path2)
no = 0
for fname in dirList2:
if '.txt' not in fname:
img_gray = cv2.imread(path2 + "/" + fname, 0)
img_gray = cv2.resize(img_gray, (100, 100))
template = cv2.imread(testing_data,0)
template = cv2.resize(template, (100, 100))

res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold_min = 0.7
threshold_max = 1
if min_val >= np.sum(res) and (np.sum(res) >= threshold_min and np.sum(res) <=
threshold_max):
min_val = np.sum(res)
jenis_kucing = dname
status_ketemu = True
datapemilik = True
subjects.append(dname)
##print jenis_kucing
##if status_ketemu == True :
## print 'Muka kucing cocok dengan ' , jenis_kucing
##else :
## print 'Tidak ada jenis kucing yang cocok'
##
##if datapemilik == True :
## print 'Informasi detail '
## file = open("training-data-cat/" +jenis_kucing+ "/data.txt", "r")
## print file.read()
##else :
## print 'Pemilik tidak terdaftar'

# DETEKSI MUKA KUCING YANG DI UPLOAD


def detect_face(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

face_cascade = cv2.CascadeClassifier('opencv-files/catface.xml')

faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5);

if (len(faces) == 0):
return None, None

(x, y, w, h) = faces[0]

return gray[y:y+w, x:x+h], faces[0]

# MEMPERSIAPKAN DATA LATIH HASIL TEMPLATE MATCHING


def prepare_training_data(data_folder_path):

dirs = os.listdir(data_folder_path)

faces = []
labels = []

for dir_name in dirs:

label = int(dir_name.replace("kucing ", ""))

subject_dir_path = data_folder_path + "/" + dir_name

subject_images_names = os.listdir(subject_dir_path)
if dir_name in subjects:
for image_name in subject_images_names:

if image_name.startswith("."):
continue;
if '.txt' not in image_name:
image_path = subject_dir_path + "/" + image_name
image = cv2.imread(image_path)
image = cv2.resize(image, (150, 150))

#cv2.imshow("Training on image...", image)


face, rect = detect_face(image)

if face is not None:


faces.append(face)
labels.append(label)

return faces, labels

# print("Preparing data...")
faces, labels = prepare_training_data("training-data-cat")
#print("Data prepared")
f.write("Deteksi berhasil\n")
# print("Total faces: ", len(faces))
# print("Total labels: ", len(labels))
# MENDETEKSI MUKA KUCING DENGAN LBPH
face_recognizer = cv2.face.createLBPHFaceRecognizer()
# MELAKUKAN PELATIHAN
face_recognizer.train(faces, np.array(labels))

def draw_rectangle(img, rect):


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

def draw_text(img, text, x, y):


cv2.putText(img, text, (x, y), cv2.FONT_HERSHEY_PLAIN, 5, (0, 255, 0), 5)
# MELAKUKAN PREDIKSI DARI DATA LATIH
def predict(test_img):

img = test_img.copy()
face, rect = detect_face(img)
label, confidence = face_recognizer.predict(face)
label_text = "Kucing " + str(label)
#print("Hasil Deteksi", label_text)

draw_text(img, label_text, rect[0], rect[1]-5)

return img

#print("Predicting images...")
#print(subjects)
test_img1 = cv2.imread(testing_data)

#perform a prediction
predicted_img1 = predict(test_img1)
f.write("Prediction complete\n")
f.write(subjects[1])

f.close()
#display both images
cv2.imshow("Result", cv2.resize(predicted_img1, (400, 500)))
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)
cv2.destroyAllWindows()

Vous aimerez peut-être aussi