-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImplementation.py
71 lines (49 loc) · 1.6 KB
/
Implementation.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
import cv2
import numpy as np
import torch
import torchvision.transforms as T
import os
from GenderModel import ConvNetwork
import cvlib as cv
def prediction(image):
imgArray = image
transfrom = T.Compose([T.ToPILImage(),T.Resize((100,100)),T.ToTensor()])
img_t = transfrom(imgArray).unsqueeze(0)
model = ConvNetwork()
model.load_state_dict(torch.load("modelM.pth"))
model.eval()
with torch.no_grad():
predict = model(img_t)
probability = torch.nn.functional.softmax(predict, dim=1)
conf, classes = torch.max(probability, 1)
return (conf, classes)
cap = cv2.VideoCapture(0)
# detects Face
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
while cap.isOpened():
# reads from webcam
status, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect the faces
faces = face_cascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=7,
minSize=(100, 100),
flags=cv2.CASCADE_SCALE_IMAGE
)
classess = ["Man", "Woman"]
# Draw the rectangle around each face
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 255), 2)
faceCrop = np.copy(img[y:(y+h),x:(x+w)])
#Making and Displaying the Label
confidence, gender = prediction(faceCrop)
label = classess[gender.item()]
print(confidence)
cv2.putText(img, label, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.7,(0, 255, 0), 2 )
# Display
cv2.imshow('img', img)
key = cv2.waitKey(1)
if key == 27:
break