Shell
x
1
1
pip install opencv-python
code.py
Python
1
78
78
1
# OpenCV program to detect face in real time
2
# import libraries of python OpenCV
3
# where its functionality resides
4
5
from cv2 import cv2
6
7
import clx.xms
8
import requests
9
# By creating an account in sinch sms You can get your code.
10
# code for sms starts here
11
#client is a object that carries your unique token.
12
client = clx.xms.Client(service_plan_id='your_service id',
13
token='token_id')
14
15
create = clx.xms.api.MtBatchTextSmsCreate()
16
create.sender = 'sender no.'
17
create.recipients = {'recipients no.'}
18
create.body = 'This is a test message from your Sinch account'
19
# code for sms ends here
20
# Face Recognition starts from here.
21
# load the required trained XML classifiers
22
#https://github.com/opencv/opencv/blob/master
23
#/data/haarcascades/haarcascade_frontalface_default.xml
24
# Trained XML classifiers describes some features of some
25
# object we want to detect a cascade function is trained
26
# from a lot of positive(faces) and negative(non-faces)
27
# images.
28
29
detector = cv2.CascadeClassifier(
30
"path")
31
32
# capture frames from a camera
33
34
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
35
#We want to send sms only once not until the face is there and for that we are
36
#initializing the counter
37
counter = 0
38
# loop runs if capturing has been initialized.
39
40
while True:
41
# reads frames from a camera
42
43
ret, img = cap.read()
44
45
if ret:
46
# convert to gray scale of each frames
47
48
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
49
# Detects faces of different sizes in the input image
50
51
faces = detector.detectMultiScale(gray, 1.1, 4)
52
53
for face in faces:
54
55
x, y, w, h = face
56
# if there is any face and counter is zero then only it will send notification to the sender
57
if(face.any() and counter ==0):
58
try:
59
batch = client.create_batch(create)
60
except (requests.exceptions.RequestException, clx.xms.exceptions.ApiException) as ex:
61
print('Failed to communicate with XMS: %s' % str(ex))
62
#sms ends here
63
# To draw a rectangle in a face
64
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
65
66
67
cv2.imshow("Face", img)
68
counter = 1
69
# Wait for 'q' key to stop
70
71
key = cv2.waitKey(1)
72
if key == ord("q"):
73
break
74
# Close the window
75
cap.release()
76
# De-allocate any associated memory usage
77
78
cv2.destroyAllWindows()