Académique Documents
Professionnel Documents
Culture Documents
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'
face_cascade = cv2.CascadeClassifier('opencv-files/catface.xml')
if (len(faces) == 0):
return None, None
(x, y, w, h) = faces[0]
dirs = os.listdir(data_folder_path)
faces = []
labels = []
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))
# 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))
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)
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()