-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
92 lines (75 loc) · 2.58 KB
/
run.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 5 04:39:40 2022
@author: DELL
"""
import numpy as np
import cv2
import tensorflow
from tensorflow import keras
from tensorflow.keras.models import load_model
# load the model from disk
filename = 'vgg16.hdf5'
vgg16 = load_model(filename)
celeb_names=['Angelina Jolie', 'Bhuvan Bam', 'Brad Pitt', 'Denzel Washington', 'Hrithik Roshan',
'Jennifer Lawrence', 'Kate Winslet', 'Leonardo DiCaprio', 'Megan Fox', 'Natalie Portman',
'Robert Downey Jr', 'Sandra Bullock', 'Scarlett Johansson', 'Tom Cruise', 'Tom Hanks']
font = cv2.FONT_HERSHEY_SIMPLEX
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
cap = cv2.VideoCapture(0)
cap.set(3,640) # set Width
cap.set(4,480) # set Height'''
while True:
ret, img = cap.read()
faces = faceCascade.detectMultiScale(
img,
scaleFactor=1.2,
minNeighbors=5,
minSize=(20, 20)
)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cropped_image = img[y:y+h, x:x+w]
IMG_SIZE=256
color_img = cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB)
new_array = cv2.resize(color_img, (IMG_SIZE, IMG_SIZE))
X=new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 3)
pred = vgg16.predict(X)
prediction = np.argmax(pred, axis = -1)
confidence = np.max(pred)
if (confidence*100)>60:
cv2.putText(
img,
celeb_names[prediction[0]],
(x+5,y-5),
font,
1,
(255,255,255),
2
)
cv2.putText(
img,
str(confidence*100)+"%",
(x+5,y+h-5),
font,
1,
(255,255,0),
1
)
else:
cv2.putText(
img,
"Not Recognised",
(x+5,y-5),
font,
1,
(255,255,255),
2
)
cv2.imshow('video',img)
k = cv2.waitKey(30) & 0xff
if k == 27: # press 'ESC' to quit
break
cap.release()
cv2.destroyAllWindows()