Created
August 2, 2025 14:52
-
-
Save ajangrahmat/fc8b018107a5c5930a9641f89058959b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import face_recognition | |
import time | |
from cvzone.FaceDetectionModule import FaceDetector | |
# Font dan FPS | |
font = cv2.FONT_HERSHEY_SIMPLEX | |
jumlahFrame = 0 | |
waktuMulai = time.time() | |
nilai_fps = 0 | |
def hitungFps(): | |
global jumlahFrame, waktuMulai, nilai_fps | |
jumlahFrame += 1 | |
waktuSekarang = time.time() | |
waktuSebelumnya = waktuSekarang - waktuMulai | |
if waktuSebelumnya > 1.0: | |
nilai_fps = jumlahFrame / waktuSebelumnya | |
jumlahFrame = 0 | |
waktuMulai = time.time() | |
# Data wajah yang dikenali | |
wajah_known = face_recognition.load_image_file("ajang.jpg") | |
encoding_known = face_recognition.face_encodings(wajah_known)[0] | |
nama_known = "Ajang" | |
# Variabel status | |
lock = False | |
namaTerakhir = "Tidak dikenal" | |
# Webcam dan detector | |
video = cv2.VideoCapture(1) | |
detektor = FaceDetector(minDetectionCon=0.6) | |
while True: | |
ret, frame = video.read() | |
if not ret: | |
break | |
frame, wajah = detektor.findFaces(frame) | |
if wajah: | |
if not lock: | |
# Ubah ke RGB dan proses pengenalan wajah | |
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | |
lokasiWajah = face_recognition.face_locations(rgb) | |
encodingWajah = face_recognition.face_encodings(rgb, lokasiWajah) | |
for encode, loc in zip(encodingWajah, lokasiWajah): | |
hasil = face_recognition.compare_faces([encoding_known], encode) | |
if hasil[0]: | |
namaTerakhir = nama_known | |
else: | |
namaTerakhir = "Tidak dikenal" | |
lock = True | |
break # satu wajah cukup | |
# Gambar nama di bawah kotak | |
for kotak in wajah: | |
x, y, w, h = kotak['bbox'] | |
cv2.putText(frame, namaTerakhir, (x, y + h + 25), font, 0.8, (0, 255, 0), 2) | |
else: | |
# Tidak ada wajah → reset lock dan nama | |
lock = False | |
namaTerakhir = "Tidak dikenal" | |
# FPS dan tampil | |
hitungFps() | |
cv2.putText(frame, f'FPS: {round(nilai_fps)}', (10, 30), font, 0.8, (255, 0, 0), 2) | |
cv2.imshow('Pengenalan Wajah', frame) | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
video.release() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment