简体   繁体   English

使用 Twilio 通过 SMS 进行面部识别考勤

[英]Facial Recognition Attendance with SMS using Twilio

How do I manipulate the number that the message will be sent based on the designated number of the enrolled person.如何根据注册人的指定号码来操作发送消息的号码。 I don't know how to register a number to the designated person who will enroll using Twilio.我不知道如何向将使用 Twilio 注册的指定人员注册号码。 This capstone project is for our school.这个顶点项目是为我们学校准备的。 I want to integrate Twilio with it so that I can have my desired capstone project.我想将 Twilio 与它集成,这样我就可以拥有我想要的顶点项目。

This is the code that I'm using right now, I manipulated some details, trying to meet my desired project.这是我现在正在使用的代码,我操纵了一些细节,试图满足我想要的项目。

MAIN.PY主文件

    import enroll,spreadsheet,emailing,recognition

from twilio.rest import Client

recognition.load_facial_encodings_and_names_from_memory()

spreadsheet.mark_all_absent()

recognition.run_recognition()

client = Client(enroll.account_sid, enroll.auth_token)


student = enroll.enroll_via_camera('hups')

message = client.messages.create(
        body = "Thanks for enrolling " + ["name"], 
        from_ = enroll.twilio_number,
        to= ["mobile"]
    )
print(message.body) 

ENROLL.PY注册.PY

    import face_recognition,cv2,pickle
import spreadsheet

photo_folder = 'C:/Users/calab/Desktop/Daniel/Face-recognition-based-attendance-system-master/known face photos/'
facial_encodings_folder='C:/Users/calab/Desktop/Daniel/Face-recognition-based-attendance-system-master/known face encodings/'

cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)

account_sid = 'ACae108c0b6468e1451db3312b237bd599'
auth_token = 'a87d9f333d3a8e242b2ea972bb6ada83'
twilio_number = '+19377125829'

def encoding_of_enrolled_person(name,image):
    enroll_encoding=[]

    enroll_encoding.append(face_recognition.face_encodings(face_recognition.load_image_file(image))[0])
    f=open(facial_encodings_folder+name+'.txt','w+')
    
    with open(facial_encodings_folder+name+'.txt','wb') as fp:
        pickle.dump(enroll_encoding,fp)
    f.close
    
    

def enroll_via_camera(name):
    while True:
        ret,frame=cap.read()
        cv2.imshow('Enrolling new attendee',frame)
        k=cv2.waitKey(1)
        if k & 0xFF==ord('y'):
            cv2.imwrite(photo_folder+name+'.jpg',frame)
            encoding_of_enrolled_person(name,photo_folder+name+'.jpg')
            cv2.destroyAllWindows()
            break
        if k& 0xFF==ord('q'):
            print('quitting')
            cv2.destroyAllWindows()
            break
    cap.release()
    email=input("Enter email address: ")
    mobile =input("Enter mobile number: ")
    spreadsheet.enroll_person_to_sheet(name,email,mobile)
    return {
      "name": name,
      "email": email,
      "mobile": mobile
    }
    

EMAILING.PY电子邮件.PY

    import smtplib, ssl, datetime


sender= "facerecogappcapstone@gmail.com"     #senders email id
password="Face12345"                #password



def email_pin(email,pin):
    port = 465
    now=datetime.datetime.now()
    date=now.strftime('%m/%d/%Y').replace('/0','/')
    if(date[0]=='0'):
        date=date[1:]
    subject="Pin for your attendance app "
    text="\nYour pin for attendance app is "+str(pin)

    message ='Subject: {}\n\n{}'.format(subject, text)
    

    context = ssl.create_default_context()

    print("Starting to send")
    with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
        server.login(sender, password)
        server.sendmail(sender, email, message)

    print("sent email!")



def send_email(receiver_mail,attendance):
    port = 46532
    now=datetime.datetime.now()
    date=now.strftime('%m/%d/%Y').replace('/0','/')
    subject="Attendance on "+str(date)
    text="\nYour attendance is marked "+attendance

    message ='Subject: {}\n\n{}'.format(subject, text)
    

    context = ssl.create_default_context()

    print("Starting to send")
    with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
        server.login(sender, password)
        server.sendmail(sender, receiver_mail , message)

    print("sent email!")

RECOGNITION.PY识别.PY

  # -*- coding: utf-8 -*-
    
    
    import face_recognition,cv2,os,pickle
    import numpy as np
    import spreadsheet 
    
    known_face_encodings=[]
    known_face_names=[]
    cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
    
    photo_folder = 'C:/Users/calab/Desktop/Daniel/Face-recognition-based-attendance-system-master/known face photos/'
    facial_encodings_folder='C:/Users/calab/Desktop/Daniel/Face-recognition-based-attendance-system-master/known face encodings/'
    
    def load_facial_encodings_and_names_from_memory():
        for filename in os.listdir(facial_encodings_folder):
            known_face_names.append(filename[:-4])
            with open (facial_encodings_folder+filename, 'rb') as fp:
                known_face_encodings.append(pickle.load(fp)[0])
                
                
    def run_recognition():
    
    
         video_capture = cv2.VideoCapture(0,cv2.CAP_DSHOW)
    
    
         face_locations = []
         face_encodings = []
         face_names = []
         process_this_frame = True
    
         while True:
        # Grab a single frame of video
             ret, frame = video_capture.read()
    
        # Resize frame of video to 1/4 size for faster face recognition processing
             small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
    
        # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
             rgb_small_frame = small_frame[:, :, ::-1]
    
        # Only process every other frame of video to save time
             if process_this_frame:
            # Find all the faces and face encodings in the current frame of video
                 face_locations = face_recognition.face_locations(rgb_small_frame)
                 face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
    
                 face_names = []
                 for face_encoding in face_encodings:
                # See if the face is a match for the known face(s)
                     matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
                     name = "Unknown"
    
                # # If a match was found in known_face_encodings, just use the first one.
                # if True in matches:
                #     first_match_index = matches.index(True)
                #     name = known_face_names[first_match_index]
    
                # Or instead, use the known face with the smallest distance to the new face
                     face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
                     best_match_index = np.argmin(face_distances)
                     if matches[best_match_index]:
                         name = known_face_names[best_match_index]
    
                     face_names.append(name)
    
             process_this_frame = not process_this_frame
    
    
        # Display the results
             for (top, right, bottom, left), name in zip(face_locations, face_names):
            # Scale back up face locations since the frame we detected in was scaled to 1/4 size
                 top *= 4
                 right *= 4
                 bottom *= 4
                 left *= 4
    
            # Draw a box around the face
                 cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
    
            # Draw a label with a name below the face
                 cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
                 font = cv2.FONT_HERSHEY_DUPLEX
                 cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
    
        # Display the resulting image
             cv2.imshow('Video', frame)
             flag=-1
             if(len(face_names)!=0):
                count=0
                for person in face_names:
                    if(person=='Unknown'):
                        count+=1
                if(count==len(face_names)):
                    flag=1
                else:
                    flag=0
    
    
        # Hit 'q' on the keyboard to quit!
    
        
             if cv2.waitKey(1) & 0xFF==ord('q') or flag==0:
                spreadsheet.write_to_sheet(face_names[0])
                break
            
                
    
    # Release handle to the webcam
         video_capture.release()
         cv2.destroyAllWindows()
    

SPREADSHEET.PY电子表格.PY

 import datetime,gspread,random
from oauth2client.service_account import ServiceAccountCredentials
import emailing as em 

scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]

creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)

client = gspread.authorize(creds)

sheet = client.open("Face recognition").sheet1

max_intime='16:00:00'

def enroll_person_to_sheet(name,email,mobile):
    nrows = len(sheet.col_values(1))
    pin=random.randint(999,9999)
    sheet.update_cell(nrows+1,1,name)
    sheet.update_cell(nrows+1,2,email)
    sheet.update_cell(nrows+1,3,pin)
    sheet.update_cell(nrows+1,4,mobile)
    em.email_pin(email,pin)
    
    
def mark_all_absent():
    now=datetime.datetime.now()
    date=now.strftime('%m/%d/%Y').replace('/0','/')
    if(date[0]=='0'):
        date=date[1:]
    datecell=sheet.find(date)
    nrows = len(sheet.col_values(1))
    for row in range(2,nrows+1):
        sheet.update_cell(row,datecell.col,'absent') 
        
        
        
def write_to_sheet(name):
    now=datetime.datetime.now()
    date=now.strftime('%m/%d/%Y').replace('/0','/')
    if(date[0]=='0'):
            date=date[1:]
    time=now.strftime('%H:%M:%S')
    namecell=sheet.find(name)
    datecell=sheet.find(date)

    if(sheet.cell(namecell.row,datecell.col).value =='absent' ):
        if(time<max_intime):
            sheet.update_cell(namecell.row,datecell.col,'present') 
            print('recorded')
            em.send_email(sheet.cell(namecell.row,2).value,"present")
         
        else:
            # sheet.update_cell(namecell.row,datecell.col,'late')
            print('late')
            em.send_email(sheet.cell(namecell.row,2).value,"absent")

ERROR:错误:

AttributeError: module 'enroll' has no attribute 'mobile'

  File "C:\Users\calab\Desktop\Daniel\Face-recognition-based-attendance-system-master\face recognition source code\main.py", line 10, in <module>
    recognition.run_recognition()

  File "C:\Users\calab\Desktop\Daniel\Face-recognition-based-attendance-system-master\face recognition source code\recognition.py", line 105, in run_recognition
    spreadsheet.write_to_sheet(face_names[0])

  File "C:\Users\calab\Desktop\Daniel\Face-recognition-based-attendance-system-master\face recognition source code\spreadsheet.py", line 53, in write_to_sheet
    em.send_email(sheet.cell(namecell.row,2).value,"present")

  File "C:\Users\calab\Desktop\Daniel\Face-recognition-based-attendance-system-master\face recognition source code\emailing.py", line 48, in send_email
    with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:

  File "C:\Users\calab\anaconda3\envs\Daniel\lib\smtplib.py", line 1048, in __init__
    SMTP.__init__(self, host, port, local_hostname, timeout,

  File "C:\Users\calab\anaconda3\envs\Daniel\lib\smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)

  File "C:\Users\calab\anaconda3\envs\Daniel\lib\smtplib.py", line 339, in connect
    self.sock = self._get_socket(host, port, self.timeout)

  File "C:\Users\calab\anaconda3\envs\Daniel\lib\smtplib.py", line 1054, in _get_socket
    new_socket = socket.create_connection((host, port), timeout,

  File "C:\Users\calab\anaconda3\envs\Daniel\lib\socket.py", line 808, in create_connection
    raise err

  File "C:\Users\calab\anaconda3\envs\Daniel\lib\socket.py", line 796, in create_connection
    sock.connect(sa)

TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

enroll.mobile注册.mobile

message = client.messages.create(
    body = "check123",
    from_ = enroll.twilio_number,
    to= int(enroll.mobile) # <<<---- HERE
    )

It does not have mobile maybe you should consider upgrading the API version or to check if you need to define mobile它没有移动可能你应该考虑升级API版本或检查你是否需要定义移动

It looks as though this is a command line application?看起来好像这是一个命令行应用程序? And enrolling someone asks for their email address and mobile phone number and stores it in a spreadsheet.注册某人会询问他们的电子邮件地址和手机号码,并将其存储在电子表格中。

In your main.py you seem to be enrolling just one person, Daniel, right now.在您的 main.py 中,您现在似乎只注册了一个人 Daniel。 What you could do is return the mobile number from the function enroll_via_camera and then send them a message once the enrolment is complete.您可以做的是从函数enroll_via_camera返回手机号码,然后在注册完成后向他们发送消息。

To do this you would need to update the enroll_via_camera method to return the user details.为此,您需要更新enroll_via_camera方法以返回用户详细信息。

def enroll_via_camera(name):
    while True:
        ret,frame=cap.read()
        cv2.imshow('Enrolling new attendee',frame)
        k=cv2.waitKey(1)
        if k & 0xFF==ord('y'):
            cv2.imwrite(photo_folder+name+'.jpg',frame)
            encoding_of_enrolled_person(name,photo_folder+name+'.jpg')
            cv2.destroyAllWindows()
            break
        if k& 0xFF==ord('q'):
            print('quitting')
            cv2.destroyAllWindows()
            break
    cap.release()
    email=input("Enter email address: ")
    mobile =input("Enter mobile number: ")
    spreadsheet.enroll_person_to_sheet(name,email,mobile)
    return {
      "name": name,
      "email": email,
      "mobile": mobile
    }

Then, in main.py you can use that result to send a message.然后,在 main.py 中,您可以使用该结果发送消息。

import enroll,spreadsheet,emailing,recognition

from twilio.rest import Client

recognition.load_facial_encodings_and_names_from_memory()

spreadsheet.mark_all_absent()

recognition.run_recognition()

client = Client(enroll.account_sid, enroll.auth_token)


student = enroll.enroll_via_camera('Daniel')

message = client.messages.create(
        body = "Thanks for enrolling " + student["name"], 
        from_ = enroll.twilio_number,
        to= student["mobile"]
    )
print(message.body) 

One extra note, you seem to have included your Account Sid and Auth Token in this question.一个额外的说明,你似乎在这个问题中包含了你的 Account Sid 和 Auth Token。 A malicious person could steal those credentials and abuse your account, so I recommend you change your auth token as soon as possible.恶意的人可能会窃取这些凭据并滥用您的帐户,因此我建议您尽快更改您的身份验证令牌

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM