diff --git a/classes/IPCamera.py b/classes/IPCamera.py index 21cff11..fd80ed4 100644 --- a/classes/IPCamera.py +++ b/classes/IPCamera.py @@ -10,30 +10,43 @@ class IPCamera(object): def __init__(self, src=0): self.log = logging.getLogger(__name__) - self.capture = cv2.VideoCapture(src) - self.capture.set(cv2.CAP_PROP_BUFFERSIZE, 1) + self.src = src self.FPS = 1/100 self.FPS_MS = int(self.FPS * 1000) - # First initialisation self.status and self.frame - - (self.status, self.frame) = self.capture.read() - - if self.frame is not None: - self.log.info("Uma nova conexao de camera foi criada com sucesso") - else: - self.log.error("Falha ao criar nova conexao de camera") + + self.create_connection() # Start frame retrieval thread self.thread = Thread(target=self.update, args=()) self.thread.daemon = True self.thread.start() + + + def create_connection(self): + self.running = False + while True: + self.capture = cv2.VideoCapture(self.src) + self.capture.set(cv2.CAP_PROP_BUFFERSIZE, 1) + mensagem = "" + if self.capture.isOpened(): + (self.status, self.frame) = self.capture.read() + if self.frame is None: + mensagem = "Falha ao criar nova conexao de camera" + else: + mensagem = "Falha ao abrir camera" + if not mensagem: + break + self.log.error(mensagem) + time.sleep(1) + self.log.info("Uma nova conexao de camera foi criada com sucesso") + self.running = True def update(self): while True: try: - if self.capture.isOpened(): + if self.running and self.capture.isOpened(): (self.status, new_frame) = self.capture.read() self.frame = new_frame time.sleep(self.FPS) diff --git a/classes/IPWall.py b/classes/IPWall.py index 1236c7b..5d2226c 100644 --- a/classes/IPWall.py +++ b/classes/IPWall.py @@ -3,22 +3,43 @@ import logging import requests import json +import time # ---- MODULES ----# -####################### -#IMPLEMENTACAO OCULTADA -####################### class IPWall: def __init__(self): - pass + self.log = logging.getLogger(__name__) + self.base_url = "https://192.168.115.2/integracao/" def open_door(self, porta=None, ramal=None, audio=None, timeout=5): - pass + url = self.base_url + "abrePortaria.php?" + params_nome = ["porta", "ramal", "audio"] + params_values = [porta, ramal, audio] + for i in range(len(params_nome)): + if params_values[i] is not None: + url += f"{params_nome[i]}={params_values[i]}&" + try: + response = requests.get(url, verify=False, timeout=timeout) + print(url) + response_text_result = json.loads(response.text)['result'] + print(response_text_result) + return response_text_result == 'true' + except requests.exceptions.Timeout: + self.log.error("Timeout ao tentar abrir a porta") + return False + except Exception as e: + self.log.error(str(e)) + return False def executa_audio(self): pass +if __name__ == "__main__": + ipwall = IPWall() + ipwall.open_door(porta=101, ramal=2000) + time.sleep(3) + ipwall.open_door(porta=102,ramal=2000) diff --git a/main.py b/main.py index 1bb193e..dadcb3b 100644 --- a/main.py +++ b/main.py @@ -46,6 +46,7 @@ class Main: area = abs(top-bottom)*abs(left-right) array_faces.append((area, face_encoding)) if len(array_faces) == 0: + time.sleep(0.2) continue array_faces_sorted = sorted(array_faces, key=lambda x: -x[0]) chosen_face_area = array_faces_sorted[0][0] @@ -76,7 +77,7 @@ class Main: else: self.log.error("Falha ao acessar frame da camera") - self.ip_camera = IPCamera(self.src) + self.ip_camera.create_connection() except Exception as e: self.log.critical(str(e)) break @@ -91,9 +92,11 @@ class Main: self.log = logging.getLogger(__name__) def insere_buffer(self, tupla): + self.log.info("Inserindo no buffer") self.buffer.appendleft(tupla) def descarrega_buffer(self): + self.log.info("Descarregando buffer") for (image_filename, frame) in self.buffer: try: cv2.imwrite(filename=image_filename,img=frame)