-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidentification.py
89 lines (63 loc) · 2.34 KB
/
identification.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
import cv2
import matplotlib.pyplot as plt
import dlib
from imutils import face_utils
import sys
#CODE ONLY USED IF NEURALNET IS NOT IN SAME FOLDER AS CURRENT DIRECTORY
#sys.path.insert(1, '././githubFiles/DIP-Final/')
from NeuralNet import Mymodel
import numpy as np
import torch
from dataset_loader import dataset_loader
from PIL import Image
#MODEL PATH SHOULD BE LOCAL
#model_path='./githubFiles/DIP-Final/weights.pth'
model_path = "./weights.pth"
model=Mymodel()
#WAS NECESSARY TO ADD MAP_LOCATION COMMAND ARG TO TORCH.LOAD ON MY COMPUTER, LIKELY UNNNECESSARY IF YOU HAVE GPU
checkpoint=torch.load(model_path, map_location=torch.device('cpu'))
model.load_state_dict(checkpoint)
model.eval()
def maskIdentify(imgpath):
frame = cv2.imread(imgpath)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
dnnFaceDetector = dlib.cnn_face_detection_model_v1("./mmod_human_face_detector.dat")
#FACE DETECTOR TAKES GRAYSCALE CV IMAGE
rects = dnnFaceDetector(gray, 1)
for (i, rect) in enumerate(rects):
x1 = rect.rect.left()
y1 = rect.rect.top()
x2 = rect.rect.right()
y2 = rect.rect.bottom()
#USE OFFSET VALUE TO alter Frame size, negative values result in tighter boxes while positive value correspond with wider boxes.
offset = 5
#RESIZE IMAGE TO 60 by 60
face = cv2.resize(frame[y1-offset:y2+offset, x1-offset:x2+offset], (60,60))
#pilIMG = Image.fromarray(face)
#TURN ARRAY FROM 3D to 4D
npInput = []
npInput.append(face)
#RUN PREDICTION AND PRINT
x=np.moveaxis(npInput,-1,1)
x_t=torch.tensor(x,dtype=torch.float32)
y_pred=model.forward(x_t)
print(y_pred)
# plt.imshow(face)
# plt.show()
# print(x2-x1)
# print(y2-y1)
# Rectangle around the face
#IMAGE FOR SHOWING FACE THAT IS BEING DETECTED AND ADDING RECTANGLE AROUND FACES IN FINAL IMAGE
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 3)
plt.imshow(face)
plt.show()
plt.imshow(frame)
plt.show()
#CHANGE FILENAME TO WHATEVER LOCATION YOU HAVE FOR IMAGES
filename = "./archive/images/maksssksksss"
for i in range(3):
maskIdentify(filename+str(i)+".png")
# filename = sys.argv[1]
# print(filename)
# maskIdentify(filename)