-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect.py
More file actions
42 lines (32 loc) · 1.39 KB
/
Copy pathdetect.py
File metadata and controls
42 lines (32 loc) · 1.39 KB
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
38
39
40
41
42
import cv2
import numpy as np
from PIL import Image
import os
def draw_boundary(img, classifier, scaleFactor, minNeighbors, color, text, clf):
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
features = classifier.detectMultiScale(gray_img, scaleFactor, minNeighbors)
for (x,y,w,h) in features:
cv2.rectangle(img, (x,y), (x+w,y+h), color, 2 )
id, pred = clf.predict(gray_img[y:y+h,x:x+w])
confidence = int(100*(1-pred/300))
if confidence>70:
if id==1:
cv2.putText(img, "Krish", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 1, cv2.LINE_AA)
if id==2:
cv2.putText(img, "aditya", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 1, cv2.LINE_AA)
else:
cv2.putText(img, "UNKNOWN", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,0,255), 1, cv2.LINE_AA)
return img
# loading classifier
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
clf = cv2.face.LBPHFaceRecognizer_create()
clf.read("classifier.xml")
video_capture = cv2.VideoCapture(0)
while True:
ret, img = video_capture.read()
img = draw_boundary(img, faceCascade, 1.3, 6, (255,255,255), "Face", clf)
cv2.imshow("face Detection", img)
if cv2.waitKey(1)==13:
break
video_capture.release()
cv2.destroyAllWindows()