Lanyu XuMohit KumarChangxin Bai
Created March 22, 2016

Adolescent Monitoring System

Want to know when are your kids coming back home? Keep track of your family with this cool Intel Edison project!

AdvancedShowcase (no instructions)8 hours216
Adolescent Monitoring System

Things used in this project

Story

Read more

Code

processing.py

Python
Preprocess face pictures in the face database
path = "/home/root/face"
import os
import numpy as np
import cv2
picturelist = os.listdir(path)
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
for pic in picturelist:
  img = cv2.imread(path+'/'+ pic)
  gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  faces = faceCascade.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5, minSize=(30, 30), flags = cv2.cv.CV_HAAR_SCALE_IMAGE)
  for (x,y,w,h) in faces:
    crop_img = img[y:y+h,x:x+w]
  crop_img = cv2.resize(crop_img, (115, 115)) 
  temp = pic.split('.')
  os.chdir(path)
  cv2.imwrite(temp[0]+ '.bmp', crop_img)
  with file(temp[0]+'.txt','w')as outfile:
    for data_slice in crop_img:
      np.savetxt(outfile,data_slice,fmt='%-7.2f') 

rotate.py

Python
Edison board triggers the webcam as soon as it detects the movement of rotary angle sensors and then it sends the message to parents by email and SMS. This file calls ProcessNew.py to process the snapshot, and matching.py to find the matching face in database.
from time import sleep
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from twilio.rest import TwilioRestClient

import os
import pyupm_grove as grove
import smtplib
import base64



def mail(msgCall):
    sender = "mistlab2218@gmail.com"
    reciever = "send to a email address"
    img_data = open("snapshot.png", 'rb').read()
    msg = MIMEMultipart()
    msg['Subject'] = 'Adolescent Monitoring System'
    msg['From'] = 'mistlab2218@gmail.com'
    msg['To'] = 'send to a email address'

    text = MIMEText(msgCall)
    msg.attach(text)
    image = MIMEImage(img_data, name=os.path.basename("snapshot.png"))
    msg.attach(image)

    sendMail = smtplib.SMTP('smtp.gmail.com',587)
    sendMail.ehlo()
    sendMail.starttls()
    sendMail.ehlo()
    sendMail.login("mistlab2218@gmail.com","********")
    sendMail.sendmail(sender, reciever, msg.as_string())
    sendMail.quit()

# put your own credentials here 
ACCOUNT_SID = "***get it from twilio***" 
AUTH_TOKEN = "***get it from twilio***"

client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)  


# Two knobs on AIO pin 0 and 2
knob = grove.GroveRotary(0)
knob2 = grove.GroveRotary(2)

 

# Loop indefinitely
while True:
  # Read values
  absrad = knob.abs_rad()
  absrad2 = knob2.abs_rad()
  

  if absrad > 1 :
    os.system("fswebcam snapshot.png");
    os.system("/media/sdcard/software/envs/python2/bin/python ProcessNew.py");
    os.system("python matching.py");
    message=" coming in."
    print "deg = %5.2f" % absrad , " rad "
    file = open('/home/root/Output.txt', 'r')
    information = file.read()+ message
    mail(information)
    client.messages.create(
        to="household's phone number",
        from_="twilio phone number",
        body=information
    )
    print "message send out for incoming"  

  if absrad2 > 1 :
    sleep(5)
    os.system("fswebcam snapshot.png");
    message="Someone going out."
    print "deg = %5.2f" % absrad2 , " rad "
    mail(message)
    client.messages.create(
       to="household's phone number",
       from_="twilio phone number",
       body="someone going out"
    ) 
    print "message send out" 

ProcessNew.py

Python
Process the snapshot to get its feature value
import os
import numpy as np
import cv2
faceCascade = cv2.CascadeClassifier('/home/root/haarcascade_frontalface_alt.xml')
path = '/home/root'
def main():
    img = cv2.imread(path+'/'+'snapshot.png')
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    #print 'one'
    faces = faceCascade.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5, minSize=(30, 30), flags = cv2.cv.CV_HAAR_SCALE_IMAGE)
    for (x,y,w,h) in faces:
      crop_img = img[y:y+h,x:x+w]
    crop_img = cv2.resize(crop_img,(115,115))
    #print 'two'
    with file('snapshot.txt', 'w') as outfile:
      for data_slice in crop_img: 
        np.savetxt(outfile, data_slice, fmt='%-7.2f')
	
main()

matching.py

Python
Comparing snapshot's feature value with faces in dabatase one by one, to filter the matching pair or return null value
from skimage.measure import structural_similarity as ssim
import os
import numpy as np
path = "/home/root/face"
path1 = "/home/root"
def main():
  test = np.loadtxt(path1 +'/'+'snapshot.txt').reshape(115,115,3)
  result={}
  txtlist = os.listdir(path)
  filename = [txt for txt in txtlist if txt.endswith('txt')]
  for file in filename:
    if  file.startswith('snapshot'): continue
    new_data = np.loadtxt(path +'/'+file).reshape(115,115,3)
    temp = compare_image(test,new_data)
    result[file]=temp
  a = sorted(result.items(),key= lambda x:x[1])
  topPeople = list (a[-1])
  text_file = open("Output.txt","w")
  if topPeople[1] >= 0.5:
    print ("This is %s" % topPeople[0])
    text_file.write(topPeople[0].split('.')[0])
  else:
    print("Stranger is coming")
    text_file.write("Someone")
  text_file.close()
def compare_image(imageA,imageB):
  s = ssim(imageA,imageB,win_size = 3)
  return s
  
  
main()

Credits

Lanyu Xu

Lanyu Xu

1 project • 2 followers
Contact
Mohit Kumar

Mohit Kumar

1 project • 2 followers
Contact
Changxin Bai

Changxin Bai

1 project • 0 followers
Contact

Comments