-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
68 lines (54 loc) · 2.08 KB
/
test.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import cv2
import numpy as np
from tensorflow.keras.models import load_model
facedetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Copyright 2023 Pabitra Banerjee
cap = cv2.VideoCapture(0)
cap.set(3, 480)
cap.set(4, 360)
font = cv2.FONT_HERSHEY_COMPLEX
model = load_model('keras_model.h5')
def get_className(classNo):
if classNo == 0:
return "Arnab De"
elif classNo == 1:
return "Pabitra Banerjee"
elif classNo == 2:
return "Elon Musk"
elif classNo == 3:
return "Sundar Pichai"
elif classNo == 4:
return "Bill Gates"
elif classNo == 5:
return "Sam Altman"
elif classNo == 6:
return "Murk Zuckerberg"
elif classNo == 7:
return "Tony Stark"
# Copyright 2023 Pabitra Banerjee
while True:
success, imgOrignal = cap.read()
faces = facedetect.detectMultiScale(imgOrignal, 1.3, 5)
for x, y, w, h in faces:
crop_img = imgOrignal[y:y+h, x:x+w]
img = cv2.resize(crop_img, (224, 224))
img = img.reshape(1, 224, 224, 3)
prediction = model.predict(img)
classIndex = np.argmax(prediction)
probabilityValue = np.amax(prediction)
if classIndex == 0:
cv2.rectangle(imgOrignal, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.rectangle(imgOrignal, (x, y-40), (x+w, y), (0, 255, 0), -2)
cv2.putText(imgOrignal, str(get_className(classIndex)), (x, y-10), font, 0.75, (255, 255, 255), 1, cv2.LINE_AA)
elif classIndex == 1:
cv2.rectangle(imgOrignal, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.rectangle(imgOrignal, (x, y-40), (x+w, y), (0, 255, 0), -2)
cv2.putText(imgOrignal, str(get_className(classIndex)), (x, y-10), font, 0.75, (255, 255, 255), 1, cv2.LINE_AA)
cv2.putText(imgOrignal, str(round(probabilityValue*100, 2))+"%", (180, 75), font, 0.75, (255, 0, 0), 2, cv2.LINE_AA)
cv2.imshow("Result", imgOrignal)
k = cv2.waitKey(1)
if k == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# Copyright 2023 Pabitra Banerjee