<commit_before>#!/usr/bin/env python

## python client for capturing faces and send them to server

import cv2
import cv2.cv as cv
import sys
import os
import numpy as np
import time
from websocket import create_connection
import base64
import json

import serial
import thread

## connect to local server
ws_hw = create_connection("ws://localhost:8080/hw")

try:
    ## on raspi
    #ser = serial.Serial('/dev/ttyACM0', 115200, timeout=5, parity="N", bytesize=8)
    
    ## on mac
    ser = serial.Serial('/dev/tty.usbmodemfa131', 115200, timeout=5, parity="N", bytesize=8)
    print "open serial port..."
except:
    print "find your device first -> $ ls /dev/tty.* "
    sys.exit(0)

ts = 0
tracked_faces=[]
fps=0
face_counter = 0
profile_mode = ""
result_recog = None
resetLED = True
training_counter = 0

## connect to local server
ws = create_connection("ws://localhost:8080/ws")
mode = 1  # -1: training, 1: detecting

def readThermal():
    while 1:
        line = ser.readline()
        try:
            if len(line.split(','))==3:
                try:
                    thermal = [float(i) for i in line.split(',')]
                    ws_hw.send(json.dumps(dict(thermal=thermal)))
                except:
                    pass
                
        except:
            break
            
    time.sleep(.001)
    ser.flushInput()
    FILE.close()

def sendToHW():
    global mode, training_counter
    ## receive actions from server
    while True:
        time.sleep(.2)
        result = ws_hw.recv()
        if result == 'T':
            training_counter += 1
            if training_counter < 8:
                mode = -1
            else:
                mode = 1
        else:
            training_counter = 0
            ser.write(result.encode())
            mode = 1

def receiveRecog():
    global result_recog
    ## receive actions from server
    while True:
        time.sleep(.2)
        result_recog = json.loads(ws.recv())
        if result_recog:
            score = result_recog["computed"].values()[0]
            if score > .75:
                ser.write("Y")
            elif score < .4:
                ser.write("D")

## use a different thread to send thermal data to server
thread.start_new_thread(readThermal, ())
thread.start_new_thread(sendToHW, ())
thread.start_new_thread(receiveRecog, ())

## face class for multi-face processing
class Face:
    def __init__(self, age, rect, n):
        self.age = age
        self.rect = rect
        self.name = "ID"+str(n)
        self.life = 0
        self.face_id = n
        self.fx = []
        self.fy = []
        self.cx = -300
        self.cy = -300
        self.faceROI = None
        self.fixed = False
        self.state = ""
        self.face_list=[]
        self.timer = time.time()
        
    def updateFace(self, rect):
        if self.age < 45:
            self.age += 3
        else:
            self.age = 45
        self.rect = rect
        self.life = 0

    def fadeFace(self):
        self.age -= 4
        self.life = 0

    def updateName(self, name):
        self.name = name

    def updateLife(self):
        self.life += 1

    def isTooOld(self):
        if (self.life>1) or self.age<0:
            return True
        else:
            return False

## draw face and related info
def draw_faces(img, faces, c):
    global resetLED, mode
    if len(faces)>0:
        resetLED = True
        for f in faces:
            x1, y1, x2, y2  = f.rect

            # cv2.imshow("roi", img[y1:y2, x1:x2])
            _, data = cv2.imencode('.png', img[y1:y2, x1:x2])
            png_base64 = "data:image/png;base64," + base64.b64encode(data.tostring())
            if c % 20 == 19:
                ws.send(json.dumps(dict(label="known", mode=mode, base64Data=png_base64)))
            # draw duration bar
            if (f.age<=40):
                cv2.rectangle(img, (x1+5, y1+5), (((x2-5)-(x1+5))/40*f.age+x1+5, y1+10), (255, 55,0), 1)
            else:
                cv2.rectangle(img, (x1+5, y1+5), (x2-5, y1+10), (255, 55,0), 1)

            # draw face rectangle
            cv2.rectangle(img, (x1, y1), (x2, y2), (255, 55,0), 1)<commit_msg>add face following<commit_after>#!/usr/bin/env python

## python client for capturing faces and send them to server

import cv2
import cv2.cv as cv
import sys
import os
import numpy as np
import time
from websocket import create_connection
import base64
import json

import serial
import thread

## connect to local server
ws_hw = create_connection("ws://localhost:8080/hw")

try:
    ## on raspi
    #ser = serial.Serial('/dev/ttyACM0', 115200, timeout=5, parity="N", bytesize=8)
    
    ## on mac
    ser = serial.Serial('/dev/tty.usbmodemfd141', 115200, timeout=5, parity="N", bytesize=8)
    print "open serial port..."
except:
    print "find your device first -> $ ls /dev/tty.* "
    sys.exit(0)

ts = 0
tracked_faces=[]
fps=0
face_counter = 0
profile_mode = ""
result_recog = None
resetLED = True
training_counter = 0

## connect to local server
ws = create_connection("ws://localhost:8080/ws")
mode = 1  # -1: training, 1: detecting

def readThermal():
    while 1:
        line = ser.readline()
        try:
            if len(line.split(','))==3:
                try:
                    thermal = [float(i) for i in line.split(',')]
                    ws_hw.send(json.dumps(dict(thermal=thermal)))
                except:
                    pass
                
        except:
            break
            
    time.sleep(.001)
    ser.flushInput()
    FILE.close()

def sendToHW():
    global mode, training_counter
    ## receive actions from server
    while True:
        time.sleep(.2)
        result = ws_hw.recv()
        if result == 'T':
            training_counter += 1
            if training_counter < 8:
                mode = -1
            else:
                mode = 1
        else:
            training_counter = 0
            ser.write(result.encode())
            mode = 1

def receiveRecog():
    global result_recog
    ## receive actions from server
    while True:
        time.sleep(.2)
        result_recog = json.loads(ws.recv())
        if result_recog:
            score = result_recog["computed"].values()[0]
            if score > .75:
                ser.write("Y")
            elif score < .4:
                ser.write("D")

## use a different thread to send thermal data to server
thread.start_new_thread(readThermal, ())
thread.start_new_thread(sendToHW, ())
thread.start_new_thread(receiveRecog, ())

## face class for multi-face processing
class Face:
    def __init__(self, age, rect, n):
        self.age = age
        self.rect = rect
        self.name = "ID"+str(n)
        self.life = 0
        self.face_id = n
        self.fx = []
        self.fy = []
        self.cx = -300
        self.cy = -300
        self.faceROI = None
        self.fixed = False
        self.state = ""
        self.face_list=[]
        self.timer = time.time()
        
    def updateFace(self, rect):
        if self.age < 45:
            self.age += 3
        else:
            self.age = 45
        self.rect = rect
        self.life = 0

    def fadeFace(self):
        self.age -= 4
        self.life = 0

    def updateName(self, name):
        self.name = name

    def updateLife(self):
        self.life += 1

    def isTooOld(self):
        if (self.life>1) or self.age<0:
            return True
        else:
            return False

## draw face and related info
def draw_faces(img, faces, c):
    global resetLED, mode
    if len(faces)>0:
        resetLED = True
        for f in faces:
            x1, y1, x2, y2  = f.rect

            # cv2.imshow("roi", img[y1:y2, x1:x2])
            _, data = cv2.imencode('.png', img[y1:y2, x1:x2])
            png_base64 = "data:image/png;base64," + base64.b64encode(data.tostring())
            if c % 20 == 19:
                ws.send(json.dumps(dict(label="known", mode=mode, base64Data=png_base64)))
            # draw duration bar
            if (f.age<=40):
                cv2.rectangle(img, (x1+5, y1+5), (((x2-5)-(x1+5))/40*f.age+x1+5, y1+10), (255, 55,0), 1)
            else:
                cv2.rectangle(img, (x1+5, y1+5), (x2-5, y1+10), (255, 55,0), 1)

            # draw face rectangle
            cv2.rectangle(img, (x1, y1), (x2, y2), (255, 55,0), 1)
            if (f.age>10):
                if x1 < 140:
                    ser.write("Q")
                if x1 > 170:
                    ser.write("W")
{'fraction_non_alphanumeric': 0.12706465313827278, 'fraction_numerical': 0.03893345917885795, 'mean_word_length': 1.7612377850162866, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6294447', 'n_tokens_mistral': 2948, 'n_tokens_neox': 2709, 'n_words': 707}