Example #1
0
    def __init__(self):

        self.person = Person()
        self.detector = dlib.get_frontal_face_detector()
        self.font = cv2.FONT_HERSHEY_DUPLEX
        self.camera = ''
        self.name = ''
        self.path = '/home/pi/prod/rec-facial/images/'
        self.old = '/home/pi/prod/rec-facial/images/old/'
        self.models = {
            'rasp': [
                '/home/pi/prod/rec-facial/models/codes.npy',
                '/home/pi/prod/rec-facial/models/images.npy',
                '/home/pi/prod/rec-facial/models/names.npy'
            ],
            'pc': [
                './models/codes.npy', './models/images.npy',
                './models/names.npy'
            ]
        }

        self.color = (255, 255, 0)
        self.white = (255, 255, 255)
        self.resize = 480
        self.board = BoardSerial()
        self.timeout = 180
        self.timestamp = strftime('%Y%m%d%H%M')
        self.codes, self.images, self.names = [], [], []
Example #2
0
def serial_protocol():
    s = BoardSerial()

    connection = s.open_connection(port='/dev/ttyUSB0')

    s.send_message(connection, '$PNEUL,G,3')

    connection.close()
Example #3
0
 def __init__(self, port):
     self.board = BoardSerial()
     self.port = port
     self.connection = self.board.open_connection(port=self.port)
     self.reboot = 60
     self.PATTERN = '$PNEUL,G'
     self.NOT_FOUND = '$PNEUD,G,0,,'
     self.BOX_1 = '$PNEUL,G,1,1,*21\r\n'
     self.BOX_2 = '$PNEUL,G,1,2,*22\r\n'
     self.BOX_3 = '$PNEUL,G,1,3,*23\r\n'
     self.preview_state = 0
Example #4
0
def example():
    s = BoardSerial()

    connection = s.open_connection(port='/dev/ttyUSB0')

    while True:

        if connection is None:
            connection = s.open_connection(port='/dev/ttyUSB0')

        try:

            if connection.inWaiting() > 0:
                message = connection.readline().decode('utf-8', 'replace')
                print(message)

        except UnicodeError:
            print('Invalid character')
Example #5
0
def send_command():
    s = BoardSerial()

    connection = s.open_connection('/dev/ttyUSB0')

    while True:

        v = raw_input('Digit your command:')
        v = str(v)

        if v == 'a':
            s.send_message(connection, '$PNEUD,G,1,123456782')
        elif v == 'b':
            s.send_message(connection, '$PNEUD,G,1,123456782,1')
        elif v == 'c':
            s.send_message(connection, '$PNEUD,G,1,123456782,')
        elif v == 'd':
            s.send_message(connection, '$PNEUDOK')
Example #6
0
class Communication:
    def __init__(self, port):
        self.board = BoardSerial()
        self.port = port
        self.connection = self.board.open_connection(port=self.port)
        self.reboot = 60
        self.PATTERN = '$PNEUL,G'
        self.NOT_FOUND = '$PNEUD,G,0,,'
        self.BOX_1 = '$PNEUL,G,1,1,*21\r\n'
        self.BOX_2 = '$PNEUL,G,1,2,*22\r\n'
        self.BOX_3 = '$PNEUL,G,1,3,*23\r\n'
        self.preview_state = 0

    def close_connection(self):
        self.connection = None

    def run(self, actions, battery, lat_long_actual, box):

        logger.success('Start communication')

        init_hour = self.time_hour()
        init_minute = self.time_minute(init_hour)

        while True:

            if self.connection is None:
                self.connection = self.board.open_connection(port=self.port)
                sleep(0.9)

            try:

                message_board = self.connection.readline().decode('utf-8', 'replace')

                if str(message_board[:8]) == str(self.PATTERN):

                    if self.board.digit_verify(str(message_board)):

                        self.board.send_message(connection=self.connection, message=self.board.SEND_OK)

                        message_board = message_board.split(',')

                        # basculamento

                        if message_board[2] == '1':

                            logger.debug('{}'.format(message_board))

                            box[0] = int(message_board[3])

                            code = self.code_cart(box=box[0], battery=battery[:])

                            if code is None:
                                self.board.send_message(connection=self.connection, message=self.NOT_FOUND)
                                logger.info('Not identified :(')
                            else:
                                logger.success('Caixote {}, basculou na carreta: {} '.format(box[0], code))

                                attempts = 1

                                while attempts <= 5:
                                    
                                    self.board.send_message(connection=self.connection,
                                                            message='$PNEUD,G,1,{}'.format(code))

                                    logger.debug('Send: {}'.format(attempts))
                                    sleep(0.5)
                                    attempts += 1

                        if message_board[2] == '2':

                            # camera off and analise reboot

                            if message_board[3] == '0':

                                if self.preview_state == 1:

                                    # actions[0] = 0
                                    self.preview_state = int(message_board[3])

                                    actual_hour = self.time_hour()

                                    if actual_hour[0] < init_hour[0]:
                                        actual_hour[0] = int(actual_hour[0]) + 24

                                    actual_minute = self.time_minute(actual_hour)
                                    reboot = actual_minute - init_minute

                                    if reboot >= self.reboot:
                                        logger.info('Reboot system')
                                        sleep(0.8)
                                        os.system('sudo reboot')

                            # camera start

                            elif message_board[3] == '1':

                                if self.preview_state == 0:
                                    # actions[0] = 1
                                    self.preview_state = int(message_board[3])

                                    try:
                                        lat_long_actual[0], lat_long_actual[1] = self.convert_coord(
                                            array=[float(message_board[4]), float(message_board[5])])

                                    except ValueError:
                                        logger.error('Invalid GPS')

            except UnicodeError:
                logger.error('Unicode error')

            except IOError:
                self.close_connection()
                logger.error('Serial error')

            except AttributeError:
                self.close_connection()
                logger.error('Invalid argument')

    @staticmethod
    def code_cart(box, battery):

        code = str(battery[-1])
        if code != '0':
            seq = code[-1]

            if seq == '1' and box == 2:
                if battery[-2] != 0:
                    code = str(battery[-2])

            code = code[:-1]

            return code[:]
        else:
            return None

    @staticmethod
    def convert_coord(array):
        output = [0, 0]

        for idx, n in enumerate(array):
            temp = n * 0.01
            ent = int(temp)
            dec = (temp - ent) * 1.6667
            output[idx] = (ent + dec)

        return output[0], output[1]

    @staticmethod
    def time_hour():
        hour = strftime('%H,%M', localtime())
        hour = hour.split(',')

        return hour

    @staticmethod
    def time_minute(hour):
        minutes = (int(hour[0]) * 60) + int(hour[1])

        return minutes
Example #7
0
class Recognition:
    def __init__(self):

        self.person = Person()
        self.detector = dlib.get_frontal_face_detector()
        self.font = cv2.FONT_HERSHEY_DUPLEX
        self.camera = ''
        self.name = ''
        self.path = '/home/pi/prod/rec-facial/images/'
        self.old = '/home/pi/prod/rec-facial/images/old/'
        self.models = {
            'rasp': [
                '/home/pi/prod/rec-facial/models/codes.npy',
                '/home/pi/prod/rec-facial/models/images.npy',
                '/home/pi/prod/rec-facial/models/names.npy'
            ],
            'pc': [
                './models/codes.npy', './models/images.npy',
                './models/names.npy'
            ]
        }

        self.color = (255, 255, 0)
        self.white = (255, 255, 255)
        self.resize = 480
        self.board = BoardSerial()
        self.timeout = 180
        self.timestamp = strftime('%Y%m%d%H%M')
        self.codes, self.images, self.names = [], [], []

    def find_face(self, mean, stop, camera, fps):

        frame = np.array([])

        face = ''

        count = 0

        while len(face) < 1:

            count += 1

            frame = camera.read()
            face, confidence, idx = self.detector.run(frame, 0, mean)

            if count == stop:
                frame = np.array([])
                logger.error('Time\'s up')
                break

            fps.update()

        if frame.any():
            logger.error('successful registration')
        else:
            logger.error('error registering')

        return frame

    def load_models(self):

        try:

            return np.load(self.models['pc'][0]), np.load(
                self.models['pc'][1]), np.load(self.models['pc'][2])

        except FileNotFoundError:

            logger.error('Models not found')

            return None, None, None

    def recognition(self, serial, actions, data, device, show_image=False):

        old_person = 0
        alt_person = 0

        not_identified = 0
        not_detected = 0

        try:

            if None in self.load_models():
                logger.error('Models not found')
                exit()
            else:
                self.codes, self.images, self.names = self.load_models()

        except ValueError:
            self.codes, self.images, self.names = self.load_models()

        self.camera = VideoStream(src=device).start()
        fps = FPS().start()

        sleep(0.9)

        logger.info('Camera start')

        while True:

            if actions[3] and actions[4]:

                # change turn

                self.board.send_message(serial, self.board.SEND_OK)

                alt_person = old_person
                old_person = 0
                sleep(self.timeout)

                actions[4] = 1
                actions[3] = 0
                actions[4] = 0
                actions[1] = 1

            if actions[2]:

                # create a new person

                info = data.recv()
                person = self.find_face(mean=1.5,
                                        stop=30,
                                        camera=self.camera,
                                        fps=fps)

                if person.any():

                    self.codes = list(np.load(self.models['codes']))
                    self.names = list(np.load(self.models['names']))

                    info = info.spit(',')

                    code = info[3]

                    face = imutils.resize(person, width=480)

                    if code in self.codes:

                        code = self.codes.index(code)
                        name = self.names[code]

                        path_image = '{}{}.{}.jpg'.format(
                            self.path, code, name)
                        path_move = '{}{}.{}.{}.jpg'.format(
                            self.old, code, name, self.timestamp)

                        if os.path.isfile(path_image):
                            shutil.move(path_image, path_move)

                    final_name = '{}{}.{}.jpg'.format(self.path, code, info[4])

                    cv2.imwrite(final_name, face)

                    hexadecimal = '$PNEUD,C,0,{},{}'.format(code, info[4])

                    self.board.send_message(serial, hexadecimal)

                    self.person.train(show_image=None)

                    self.codes, self.images, self.names = self.load_models()

                else:
                    self.board.send_message(serial, 'PNEUD,C,0,-1')

                actions[1] = 1
                actions[2] = 0

            while actions[1]:

                # recognition a person

                init = time()

                codes_persons = []
                names_persons = []

                frame = self.camera.read()
                frame = imutils.resize(frame, width=480)
                frame_process = imutils.resize(frame, width=240)

                frame_process = frame_process[:, :, ::-1]

                face_locations = fr.face_locations(frame_process)
                face_encodings = fr.face_encodings(frame_process,
                                                   face_locations)

                if len(face_locations) != 0:

                    for face_encoding in face_encodings:

                        matches = fr.compare_faces(self.images, face_encoding)
                        self.name = "Not identified"

                        if True in matches:
                            person_index = matches.index(True)
                            self.name = self.names[person_index]

                            codes_persons.append(self.codes[person_index])
                            names_persons.append(self.name)

                    codes_persons = list(set(codes_persons))
                    names_persons = list(set(names_persons))

                    if old_person == 0 and actions[3]:

                        if old_person in codes_persons:
                            idx = codes_persons.index(alt_person)
                            del codes_persons[idx]
                            del names_persons[idx]

                    if old_person in codes_persons:
                        not_detected = 0
                        not_identified = 0
                    else:

                        if len(codes_persons) > 0:

                            not_detected = 0
                            not_identified = 0

                            hexadecimal = '$PNEUD,C,1,{},{}'.format(
                                str(codes_persons[0]), str(names_persons[0]))

                            self.board.send_message(serial, hexadecimal)

                            old_person = codes_persons[0]

                        else:
                            not_detected = 0

                            if not_identified == 20:
                                not_identified = 0
                            else:
                                not_identified += 1
                else:
                    not_identified = 0

                    if not_detected == 20:
                        not_detected = 0
                    else:
                        not_detected += 1

                if show_image:
                    cv2.imshow('Solinftec', frame)

                    if cv2.waitKey(1) & 0xFF == ord('q'):
                        break

                fps.update()
Example #8
0
 def __init__(self):
     self.s = BoardSerial()
     self.pattern = '$PNEUL,C'
Example #9
0
class App:

    def __init__(self):
        self.s = BoardSerial()
        self.pattern = '$PNEUL,C'

    def run(self):

        board = self.s.open_connection('/dev/ttyUSB0')

        if board is None:
            logger.error('Serial port error')

        receive, send = mp.Pipe()
        actions = mp.Array('i', [1, 1, 0, 0, 1])

        s_service = mp.Process(target=self.communication, args=(board, actions, send))

        r = Recognition()
        r_service = mp.Process(target=r.recognition,
                               args=(board, actions, receive, 0, True))

        s_service.start()
        r_service.start()

        s_service.join()
        r_service.join()

    def communication(self, serial, actions, data):

        logger.info('Serial start')

        while True:

            if serial is None:
                serial = self.s.open_connection(port='/dev/ttyUSB0')
            else:

                while actions[0]:

                    try:

                        if serial.inWaiting() > 0:

                            message = serial.readline().decode('utf-8', 'replace')

                            if message[:8] == self.pattern:

                                self.s.send_message(serial, self.s.SEND_OK)

                                idx = message.find('*')

                                if idx != -1:

                                    if self.s.digit_verify(message) is True:

                                        if message[9] == '0':

                                            actions[1] = 0
                                            actions[2] = 1

                                            data.send(message)
                                        elif message[9] == '1':

                                            actions[1] = 0
                                            actions[3] = 1

                                        else:
                                            pass

                    except UnicodeError:
                        logger.error('Unicode error')
                    except IOError:
                        logger.error('Cable disconnected')