-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
98 lines (65 loc) · 2.4 KB
/
main.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
94
95
96
97
98
from flask import (
Flask, render_template, Response
)
import torch
import cv2
import mediapipe as mp
from arduino import ArduinoInput, get_usb_device
from webcam import Webcam
app = Flask(__name__)
WIDTH, HEIGHT = (1080, 760)
cam = Webcam(0, WIDTH, HEIGHT)
sensor = ArduinoInput(get_usb_device()[-1])
mp_face_mesh = mp.solutions.face_mesh
face_detector = mp_face_mesh.FaceMesh(static_image_mode=True, max_num_faces=1, refine_landmarks=True, min_detection_confidence=0.4)
model = torch.load("./weights/model_v1-2.pt")
def prediction(x1, x2):
pred = model(x1, x2)
return (pred >= torch.FloatTensor([0.5]))[0]
## Generate Frame And Processing Frame when Webcam source IN.
def generate_frames():
while True:
## read the camera frame
frame = cam.get_image()
frame = cv2.flip(frame, 1)
lie = False
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
temp_landmark = face_detector.process(image)
try:
tensor_landmark = torch.tensor([[landmark.x, landmark.y, landmark.z] for landmark in list(temp_landmark.multi_face_landmarks[0].landmark)], dtype=torch.float32)
except:
tensor_landmark = torch.randn((478, 3))
x1 = tensor_landmark.unsqueeze(0)
# TODO: Sensor Value Expectation
# sensor_values = []
# for _ in range(10):
# v = sensor.get_data()
# sensor_values.append(int(v))
# time.sleep(0.001)
# x2 = torch.tensor([sensor_values], dtype=torch.float32)
x2 = torch.randn((1, 10), dtype=torch.float32)
pred = prediction(x1, x2)
# print(f"LIE {pred}")
if pred:
lie = True
if lie:
text = "You're lying!!"
else:
text = "..."
org=(50,100)
font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame,text,org,font,4,(255,0,0),8)
ret,buffer=cv2.imencode('.jpg',frame)
assert ret, "Encoding A Frame Fail"
frame=buffer.tobytes()
yield(b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route('/', methods=["GET"])
def index():
return render_template('index.html')
# Video Wrapper API
@app.route("/video")
def video():
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == "__main__":
app.run(host = '127.0.0.1', port=8080)