TIF_E41201452/Python/tectcrime/documentation.py

43 lines
1.6 KiB
Python

import cv2
# Membaca gambar asli
original_image = cv2.imread('images/backup/testface/Akshay Kumar/Akshay Kumar_40.jpg')
cv2.imshow('Original Image', original_image)
# Mengonversi gambar ke grayscale
gray_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
cv2.imshow('Grayscale Image', gray_image)
# Meningkatkan kontras gambar grayscale
contrastpic = cv2.equalizeHist(gray_image)
cv2.imshow('Contrast Enhanced Image', contrastpic)
# Memuat classifier Haar Cascade untuk deteksi wajah
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# Mendeteksi wajah dalam gambar grayscale
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.2, minNeighbors=10, minSize=(50, 50))
# Inisialisasi variabel cropped_face dengan nilai kosong
cropped_face = None
for i, (x, y, w, h) in enumerate(faces, 1):
# Memotong wajah dari gambar asli
cropped_face = original_image[y:y + h, x:x + w]
cv2.imshow(f'Cropped Face {i}', cropped_face)
# Mengubah ukuran wajah terpotong
if cropped_face is not None:
resized_face = cv2.resize(cropped_face, (300, 300))
cv2.imshow('Resized Face', resized_face)
# Mengonversi wajah yang diubah ukurannya ke grayscale
gray_resized_face = cv2.cvtColor(resized_face, cv2.COLOR_BGR2GRAY)
cv2.imshow('Grayscale Resized Face', gray_resized_face)
# Melakukan normalisasi pada wajah grayscale yang diubah ukurannya
normalized_face = cv2.normalize(gray_resized_face, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
cv2.imshow('Normalized Face', normalized_face)
cv2.waitKey(0)
cv2.destroyAllWindows()