-
Notifications
You must be signed in to change notification settings - Fork 0
/
working-1.py
93 lines (68 loc) · 3.33 KB
/
working-1.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
93
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 30 15:39:03 2022
@author: ASUS
"""
import mediapipe as mp
import cv2
import pandas as pd
import numpy as np
import pickle
import pathlib, time
base_dir = pathlib.Path(__file__).parent
# In[9]:
with open(base_dir / 'FaceRecog/data/face_model.pkl','rb') as f:
model=pickle.load(f)
#%%
print(model)
#%%
mp_drawing_utils=mp.solutions.drawing_utils
mp_holistic=mp.solutions.holistic
mp_drawing_styles = mp.solutions.drawing_styles
print('done')
#%%
cap=cv2.VideoCapture(0)
with mp_holistic.Holistic(min_detection_confidence=0.5,min_tracking_confidence=0.5) as holistic:
while cap.isOpened():
log,frame=cap.read()
image=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
result=holistic.process(image)
image=cv2.cvtColor(image,cv2.COLOR_RGB2BGR)
#mp_drawing_utils.draw_landmarks(
#image,
#result.face_landmarks,
#mp_holistic.FACEMESH_TESSELATION,
#landmark_drawing_spec=None,
#connection_drawing_spec=mp_drawing_styles.get_default_face_mesh_tesselation_style())
#mp_drawing_utils.draw_landmarks(image,result.pose_landmarks,mp_holistic.POSE_CONNECTIONS)
#mp_drawing_utils.draw_landmarks(image,result.right_hand_landmarks,mp_holistic.HAND_CONNECTIONS)
#mp_drawing_utils.draw_landmarks(image,result.left_hand_landmarks,mp_holistic.HAND_CONNECTIONS)
#image = cv2.resize(image, (1000,7000))
#cv2.imshow('raw',image)
try:
coord1=tuple(np.multiply(np.array((result.face_landmarks.landmark[mp_holistic.PoseLandmark.RIGHT_EAR].x,result.face_landmarks.landmark[mp_holistic.PoseLandmark.RIGHT_EAR].y)),[640,480]).astype('int'))
# print(coord1)
z = result.face_landmarks.landmark[mp_holistic.PoseLandmark.LEFT_EYE].z
# cv2.putText(img=image, text = str(z)[:8], org = np.array(coord1)+np.array((100,-110)), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=(255,0,0), thickness=3)
coord1=np.array(coord1)+np.array((100,-110))
coord2=tuple(np.multiply(np.array((result.face_landmarks.landmark[mp_holistic.PoseLandmark.LEFT_EAR].x,result.face_landmarks.landmark[mp_holistic.PoseLandmark.LEFT_EAR].y)),[640,480]).astype('int'))
coord2=np.array(coord2)+np.array((-60,130))
# print(coord2)
if z<-0.025 and z>-0.032:
image=cv2.rectangle(image,coord1,coord2,(0,255,0),5)
face=np.array(result.face_landmarks.landmark)
face_row=list(np.array([[landmark.x,landmark.y,landmark.z,landmark.visibility] for landmark in face]).flatten())
added=pd.DataFrame([face_row])
prediction=model.predict(added)[0]
prediction_prob=model.predict_proba(added)[0][0]
print(prediction,prediction_prob)
else:
image=cv2.rectangle(image,coord1,coord2,(0,0,255),5)
cv2.imshow('raw',image)
time.sleep(1)
except:
cv2.imshow('raw',image)
if cv2.waitKey(10) & 0xFF == ord('Q') :
break
cap.release()
cv2.destroyAllWindows()