Upload 2 files
Browse files- facedet.py +47 -0
- facetracker.h5 +3 -0
facedet.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
+
import json
|
| 3 |
+
import numpy as np
|
| 4 |
+
from matplotlib import pyplot as plt
|
| 5 |
+
import cv2
|
| 6 |
+
from tensorflow.keras.models import load_model
|
| 7 |
+
|
| 8 |
+
#Load the model
|
| 9 |
+
facetracker = load_model('facetracker.h5')
|
| 10 |
+
|
| 11 |
+
#Load the video
|
| 12 |
+
cap = cv2.VideoCapture(0)
|
| 13 |
+
while cap.isOpened():
|
| 14 |
+
# Capture frame-by-frame
|
| 15 |
+
_ , frame = cap.read()
|
| 16 |
+
frame = frame[50:500, 50:500,:]
|
| 17 |
+
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 18 |
+
resized = tf.image.resize(rgb, (120,120))
|
| 19 |
+
|
| 20 |
+
yhat = facetracker.predict(np.expand_dims(resized/255,0))
|
| 21 |
+
sample_coords = yhat[1][0]
|
| 22 |
+
|
| 23 |
+
if yhat[0] > 0.5:
|
| 24 |
+
# Controls the main rectangle
|
| 25 |
+
cv2.rectangle(frame,
|
| 26 |
+
tuple(np.multiply(sample_coords[:2], [450,450]).astype(int)),
|
| 27 |
+
tuple(np.multiply(sample_coords[2:], [450,450]).astype(int)),
|
| 28 |
+
(255,0,0), 2)
|
| 29 |
+
# Controls the label rectangle
|
| 30 |
+
cv2.rectangle(frame,
|
| 31 |
+
tuple(np.add(np.multiply(sample_coords[:2], [450,450]).astype(int),
|
| 32 |
+
[0,-30])),
|
| 33 |
+
tuple(np.add(np.multiply(sample_coords[:2], [450,450]).astype(int),
|
| 34 |
+
[80,0])),
|
| 35 |
+
(255,0,0), -1)
|
| 36 |
+
|
| 37 |
+
# Controls the text rendered
|
| 38 |
+
cv2.putText(frame, 'Muka', tuple(np.add(np.multiply(sample_coords[:2], [450,450]).astype(int),
|
| 39 |
+
[0,-5])),
|
| 40 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2, cv2.LINE_AA)
|
| 41 |
+
# Display the resulting frame
|
| 42 |
+
cv2.imshow('Face Detection', frame)
|
| 43 |
+
# Press Q on keyboard to exit
|
| 44 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 45 |
+
break
|
| 46 |
+
cap.release()
|
| 47 |
+
cv2.destroyAllWindows()
|
facetracker.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:997421d95d0cd7c5a574872f2cc473914c4570464663a1a3650c21727358b067
|
| 3 |
+
size 67363272
|