meilily Li
Published

Facial Recognition with ARTIK Eagleye 530s

To ensure the family security, the ARTIK team developed a security system prototype for boards using the Kairos face recognition service.

IntermediateFull instructions provided670
Facial Recognition with ARTIK Eagleye 530s

Things used in this project

Hardware components

Eagleye 530s
Seeed Eagleye 530s
×1
OV5640 5M Auto Focus USB Camera
×1
10.1” PCAP Touch Screen
×1

Software apps and online services

Windows 10
Microsoft Windows 10
Python Qt (pyqt)

Hand tools and fabrication machines

hexagon plastic box

Story

Read more

Code

enroll.py

Python
import base64
import sys
from urllib2 import Request, urlopen

import json
from pprint import pprint

with open('./original.jpg', "rb") as image_file:
    encoded_string = base64.b64encode(image_file.read())

    values = """ {
      "image":" """+encoded_string+""" " ,
      "subject_id": "abc123",
      "gallery_name": "KP"
    }
    """
    headers = {
      'Content-Type': 'application/json',
      'app_id': 'c7cxxxx0a',
      'app_key': 'ac4705bdacxxff406xxxx32b9453'
    }
    request = Request('https://api.kairos.com/enroll', data=values, headers=headers)

    response_body = urlopen(request).read()

    target = open('./Enroll.txt', 'w')
    target.truncate()
    target.write(response_body)
    target.write("\n")
    target.close()

    print(str(response_body))

validate.py

Python
import base64
import sys
from urllib2 import Request, urlopen
import json
from pprint import pprint

detected = "..."

with open('./original.jpg', "rb") as image_file:
    encoded_string = base64.b64encode(image_file.read())
  #  print(encoded_string)
    values = """ {
      "image":" """+encoded_string+""" " ,
      "gallery_name": "KP"
    }
    """
    headers = {
      'Content-Type': 'application/json',
      'app_id': 'c7cxxxx0a',
      'app_key': 'ac4705bdacxxff406xxxx32b9453'
    }
    request = Request('https://api.kairos.com/recognize', data=values, headers=headers)
    response_body = urlopen(request).read()

    target = open('/root/Result.txt', 'w')
    target.truncate()
    target.write(response_body)
    target.write("\n")
    target.close()

with open('/root/Result.txt') as data_file:
    data = json.load(data_file)

    target = open('/root/Detection.txt', 'w')
    target.truncate()
    try:
        if "images" in data:
           target.write(data["images"][0]["transaction"]["status"])
           detected = data["images"][0]["transaction"]["status"]
        else:
           target.write("failure")
           detected = "failure"
    except:
        detected = "failure"
        target.write("failure")
    target.write("\n")
    target.close()
print(detected)

videowindow.py

Python
from PyQt4 import QtGui, QtCore
import cv2
import numpy as np
import os
import sys
import time
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler

class MyHandler(PatternMatchingEventHandler):
    patterns = ["*.txt"]

    def process(self, event):
        """
        event.event_type
            'modified' | 'created' | 'moved' | 'deleted'
        event.is_directory
            True | False
        event.src_path
            path/to/observed/file
        """
        # the file will be processed there
        print event.src_path, event.event_type  # print now only for degug

    def on_modified(self, event):
       with open('/root/Detection.txt') as data_file:
          for line in data_file:
             data = line
       if "success" in data:
             data = "Successful!"
       else:
             data = "Failed!"
       img = cv2.imread('original.jpg')
       font2 = cv2.FONT_HERSHEY_SIMPLEX
       bottomLeftText2 = (10,220)
       fontScale2 = 1.5
       fontColor2 = (255, 166, 47)
       lineType2 = 2
       cv2.putText(img,data,bottomLeftText2,font2,fontScale2,fontColor2,lineType2)
       cv2.imshow('Photo',img)

    def on_created(self, event):
        os.system('ls')

class Capture():
    def __init__(self):
        self.capturing = False

    def startCapture(self):
        observer = Observer()
        observer.schedule(MyHandler(), path='.')
        observer.start()

        self.capturing = True
        while(self.capturing):
            ret, frame = cap.read()
            cv2.imshow("Video", frame)
            font = cv2.FONT_HERSHEY_SIMPLEX
            bottomLeftText = (10,220)
            fontScale = 1.5
            fontColor = (255,0,0)
            lineType = 2
        observer.join()

    def takepicture(self):
            ret, frame = cap.read()
            font = cv2.FONT_HERSHEY_SIMPLEX
            bottomLeftText = (10,220)
            fontScale = 1
            fontColor = (255,0,0)
            lineType = 2
    
            cv2.imwrite('original.jpg', frame)
            cv2.putText(frame,'Validating Face',bottomLeftText,font,fontScale,fontColor,lineType)
            cv2.imshow('Photo', frame)

    def enroll(self):
       # self.capturing = False
       # cap.release()
        os.system('python ./enroll.py')

    def quitCapture(self):
        cv2.destroyAllWindows()
        cap.release()
        QtCore.QCoreApplication.quit()


class Window(QtGui.QWidget):
    def __init__(self):

        QtGui.QWidget.__init__(self)
        self.setWindowTitle('Control Panel')

        self.capture = Capture()

        self.start_button = QtGui.QPushButton('Start',self)
        self.start_button.setGeometry(200,140,80,40)
        self.start_button.clicked.connect(self.capture.startCapture)

        self.takepicture_button = QtGui.QPushButton('Take Picture',self)
        self.takepicture_button.setGeometry(300,140,80,40)
        self.takepicture_button.clicked.connect(self.capture.takepicture)

        self.end_button = QtGui.QPushButton('Enroll',self)
        self.end_button.setGeometry(400,140,80,40)
        self.end_button.clicked.connect(self.capture.enroll)

        self.quit_button = QtGui.QPushButton('Quit',self)
        self.quit_button.setGeometry(500,140,80,40)
        self.quit_button.clicked.connect(self.capture.quitCapture)
      
        w = QtGui.QWidget()
        p = w.palette()
        p.setColor(w.backgroundRole(), QtGui.QColor(255, 204, 204))
        w.setPalette(p)

        pic = QtGui.QLabel(self)
        pic.setGeometry(5, 5, 790, 145)
        pic.setPixmap(QtGui.QPixmap(os.getcwd() + "/background_a.gif"))

        self.setGeometry(0,0,800,190)
        self.show()


if __name__ == '__main__':
    cap = cv2.VideoCapture(0) 
    cap.set(3,640)
    cap.set(4,400)
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

watch_for_changes.py

Python
import os
import sys
import time  
from watchdog.observers import Observer  
from watchdog.events import PatternMatchingEventHandler  

class MyHandler(PatternMatchingEventHandler):
    patterns = ["*.jpg"]

    def process(self, event):
        """
        event.event_type 
            'modified' | 'created' | 'moved' | 'deleted'
        event.is_directory
            True | False
        event.src_path
            path/to/observed/file
        """
        # the file will be processed there
        print event.src_path, event.event_type  # print now only for degug

    def on_modified(self, event):
        os.system('python /root/validate.py')
    def on_created(self, event):
        os.system('python /root/validate.py')

if __name__ == '__main__':
    args = sys.argv[1:]
    observer = Observer()
    observer.schedule(MyHandler(), path=args[0] if args else '.')
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()

    observer.join()

watch.sh

SH
startx &
python watch_for_changes.py

start.sh

SH
python videowindow.py

Credits

artik.io

Posted by 7 pic hd ff0hphbdfc meilily Li

Comments

Add projectSign up / Login