def findQR(pathToImage): if (pathToImage == "ERROR"): logger.error("Image not found! " + pathToImage) return False isQR = False qrkey = "" logger.debug("Checking QR Codes in " + pathToImage) img_original = cv2.imread(pathToImage) image = upscale(img_original, 3.0) barcodes = pyzbar.decode(image) if len(barcodes) == 0: image = greyscale(image, 65) barcodes = pyzbar.decode(image) if len(barcodes) == 0: image = upscale(img_original, 2.0) barcodes = pyzbar.decode(image) if len(barcodes) == 0: image = greyscale(image, 55) barcodes = pyzbar.decode(image) # loop over the detected barcodes for barcode in barcodes: barcodeData = barcode.data.decode("utf-8") logger.debug("QR-Code found: " + barcodeData) isQR = True qrkey = barcodeData if not (isQR): logger.info("No QR-Code found") result = (isQR, qrkey) return result
def load_known_faces(self): self.known_faces = [] self.filelist = [] time1 = datetime.now() if not os.path.exists(self.path): logger.error("Path not found_ " + self.path) logger.info("Loading known faces") for face in os.listdir(self.path): logger.debug("Loading face " + face) if face.endswith(".jpg"): face_image = face_recognition.load_image_file(self.path + face) logger.debug("Checking File for faces: " + self.path + face) self.filelist.append(self.path + face) try: encodings = face_recognition.face_encodings(face_image) for face_encode in encodings: self.known_faces.append(face_encode) except IndexError: logger.error("No faces found in " + face) # sendMail("No Face found in file", "self.path + face") except IOError: logger.error("Cant open the face file " + face) # sendMail("No Face found in file", "self.path + face") logger.info("Loaded " + str(len(self.known_faces)) + " faces in " + str( datetime.now() - time1) + " seconds")
def sendMail(content, files=None): if str(config.get("mail", "smpt_use")) != "true": return s = smtplib.SMTP(host=config.get("mail", "smpt_host"), port=config.get("mail", "smtp_port")) s.starttls() logger.debug("Log in to " + config.get("mail", "smpt_host") + " for sending e-mail") try: s.login(config.get("mail", "smtp_user"), config.get("mail", "smtp_pass")) except smtplib.SMTPAuthenticationError: logger.error("Error in Login to SMTP Server") return msg = MIMEMultipart() # create a message msg['From'] = config.get("mail", "mail_from") msg['To'] = config.get("mail", "mail_receiver") msg['Subject'] = config.get("mail", "mail_subject") # add in the message body msg.attach(MIMEText(content, 'plain')) for f in files or []: with open(f, "rb") as fil: part = MIMEApplication( fil.read(), Name=basename(f) ) # After the file is closed logger.debug("Adding file to e-mail " + basename(f)) part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f) msg.attach(part) # send the message via the server set up earlier. s.send_message(msg) del msg
def lockDoor(): logger.debug("Lock the door") try: r = requests.post(config.get("danalock", "openhab_lock_item"), 'ON') logger.debug(str(r)) except: logger.error("Danalock not available") sendMail("Danalock not available!: " + str(config.get("danalock", "openhab_lock_item")))
def openDoor(): # open the door logger.debug("Opening the door") payload = {"command": "OFF"} try: r = requests.post(config.get("danalock", "openhab_lock_item"), 'OFF') logger.debug(str(r)) except: logger.error("Danalock not available") sendMail("Danalock not available!: " + str(config.get("danalock", "openhab_lock_item")))
def downloadImage(): # request picture from API and save it door_bird_url = config.get("doorbird", "door_bird_url") currentdate = datetime.datetime.now().timestamp() filepath = config.get("data", "data_path_unknown_faces") filename = filepath + str(currentdate) + '.jpg' logger.debug("sending http request to: " + door_bird_url + " and saving to " + filename) try: urllib.request.URLopener().retrieve(door_bird_url, filename) return filename except: sendMail("Doorbird not available!: " + str(door_bird_url)) logger.debug("Doorbird not available: ") return "ERROR"
def crop(image, file): result = False image = face_recognition.load_image_file(image) face_locations = face_recognition.face_locations(image) logger.debug("Cropper found {} face(s) in this photograph.".format( len(face_locations))) for face_location in face_locations: result = True top, right, bottom, left = face_location top = int(top * 0.7) left = int(left * 0.8) bottom = int(bottom * 1.1) right = int(right * 1.1) face_image = image[top:bottom, left:right] pil_image = Image.fromarray(face_image) # pil_image.show() pil_image.save(file) return result
def waitForEventAndDownloadImage(): # bind socket udp_address = config.get("doorbird", "udp_ip_address") udp_port = config.get("doorbird", "udp_port") server_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) server_sock.bind((udp_address, int(udp_port))) server_sock.settimeout(int(config.get("doorbird", "keep_alive_timeout"))) logger.info("UDP service started on address: {} and port: {}".format( udp_address, udp_port)) old_message = "" old_event = "" while True: try: data = server_sock.recvfrom(1024) except: # Timeout: No Keep Alive Packets sendMail( "No KeepAlive Pakcets from Doorbird! Check your Connection") logger.error( "No KeepAlive Pakcets from Doorbird! Check your Connection") continue try: #read received udp data message = data[0].decode() if message != old_message: logger.debug("Keep-alive msg: {}".format(message)) old_message = message except: hex_bytes = binascii.hexlify(data[0]) event = hex_bytes.decode("ascii") if event != old_event: logger.info("Message: An event has occured!") old_event = event return downloadImage() old_event = event
def check_face(self, image): unknown_face_encoding = [] time1 = datetime.now() logger.info("Checking unknown face") logger.warn(image) faces = [] try: unknown_image = face_recognition.load_image_file(image) unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0] except IndexError: logger.error("No faces found in " + self.path + image) return faces except IOError: logger.error("Cant open the unkown face file") return faces logger.debug("Comparing the known faces with the unkown picture") results = face_recognition.compare_faces(self.known_faces, unknown_face_encoding) index = 0 for result in results: if result: logger.info("Face found in : " + self.filelist[index]) faces.append(self.filelist[index]) index += 1 logger.debug("Processed Faces in " + str(datetime.now() - time1) + " seconds") logger.debug("Found known Face? : " + str((len(faces) != 0))) return faces