forked from rizkydermawan1992/Face-Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
facedetection.py
37 lines (26 loc) · 1.06 KB
/
facedetection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import cv2
import numpy as np
import time
pengklasifikasiWajah = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
videoCam = cv2.VideoCapture(0)
if not videoCam.isOpened():
print("Kamera tidak dapat diakses")
exit()
tombolQditekan = False
while (tombolQditekan == False):
ret, kerangka = videoCam.read()
if ret == True:
abuAbu = cv2.cvtColor(kerangka, cv2.COLOR_BGR2GRAY)
dafWajah = pengklasifikasiWajah.detectMultiScale(abuAbu, scaleFactor = 1.3, minNeighbors = 2)
for (x, y, w, h) in dafWajah:
cv2.rectangle(kerangka, (x, y), (x + w, y + h), (0, 255, 0), 2)
#print("Jumlah Wajah terdeksi: ", len(dafWajah))
teks = "Jumlah Wajah Terdeteksi = " + str(len(dafWajah))
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(kerangka, teks, (0, 30), font, 1, (255, 0, 0), 1)
cv2.imshow("Hasil", kerangka)
if cv2.waitKey(1) & 0xFF == ord('q'):
tombolQditekan = True
break
videoCam.release()
cv2.destroyAllWindows()