def on_message(self, client, userdata, msg): pr.pl("MQTT Received message: " + str(msg.topic) + " " + str(msg.payload)) if not self.downloader.downloadStarted: self.downloader.downloadfile( "../trainer", "trainer.yml", "url.to.trainer.file.generated.by.server")
def __init__(self): pr.pl("Firestore connecting") self.db = firestore.Client("projectname", self.cred) self.fb = firebase_admin.initialize_app( firebase_admin.credentials.Certificate(self.JSONPATH), options={'databaseURL': 'firestoredb.url'}) pr.pl("Firestore connected")
def __init__(self): pr.pl("MQTT Listener init") self.client = mqtt.Client() self.client.username_pw_set("username", "password") self.client.tls_set_context(self.context) self.client.on_subscribe = self.on_subscribe self.client.on_connect = self.on_connect self.client.on_message = self.on_message
def __init__(self, src_path): pr.pl("Watcher init") self.__src_path = src_path self.__event_handler = ImagesEventHandler() self.__event_observer = Observer() self.mqc.username_pw_set("username","password") self.mqc.connect("localhost") pr.pl("Watcher init done. MQTT connected")
def downloadfile(self, path, filename, url): pr.pl("Downloading file " + str(url)) file = path + "/" + filename self.downloadStarted = True self.downloadFinished = False urllib.request.urlretrieve(url, file, self.show_prorgress) self.downloadFinished = True pr.pl("Completed download") self.downloaded = 0
def train(self, faces, ids): pr.pl("Training faces. It will take a few seconds. Wait ...") self.recognizer.train(faces, np.array(ids)) pr.pl("{0} faces trained. Finished training.".format( len(np.unique(ids)))) pr.pl("Writing trainer.yml file. Please Wait ...") self.recognizer.write('./trainer.yml') pr.pl("Write completed.")
def __init__(self, cascadePath, trainerPath, cam, flipImage=False): pr.pl("OpenCV starting") self.cam = cam self.trainerPath = trainerPath self.cascadePath = cascadePath self.recognizer = cv2.face.LBPHFaceRecognizer_create() self.recognizer.read(self.trainerPath) self.faceCascade = cv2.CascadeClassifier(cascadePath) self.cam.set(3, 640) # set video widht self.cam.set(4, 480) # set video height # Define min window size to be recognized as a face self.minW = 0.1 * self.cam.get(3) self.minH = 0.1 * self.cam.get(4) self.flipImage = flipImage pr.pl("OpenCV started")
def refresh(self): if self.downloader.downloadStarted: if self.downloader.downloadFinished: pr.pl("New trainer file downloaded, reloading") self.fr = None self.fr = FaceRecognition(self.CASCADE_PATH, self.TRAINER_PATH, self.cam, self.FLIP_IMAGE) pr.pl("Reloaded") self.downloader.downloadStarted = False self.downloader.downloadFinished = False ret, img = self.cam.read() img = self.fr.recognizeImg(img) toShow = ImageTk.PhotoImage( image=self.ui.convert(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))) self.ui.lmain.imgtk = toShow self.ui.lmain.configure(image=toShow) self.framecounter += 1 if self.fr.isFacePresent: if not self.fr.studentid == 0: if self.framecounter > 30: self.lesson = self.fr.sqlm.getCurrentLessonForStudent( self.fr.studentid) self.framecounter = 0 pr.pl("student id : " + str(self.fr.studentid)) else: if self.lesson == 0: self.lesson = self.fr.sqlm.getCurrentLessonForStudent( self.fr.studentid) if self.lesson is not None: if self.lesson[-1] == 1: if not self.ui.buttonsDisabled: self.ui.disableButtons() if self.ui.defaultText: self.ui.writeAlreadyAttended() else: if not self.ui.defaultText: self.ui.writeDefaultText() if self.ui.buttonsDisabled: self.ui.enableButtons() else: self.ui.writeText("You don't have lessons right now.") if not self.ui.buttonsDisabled: self.ui.disableButtons() else: self.ui.writeText("I don't know you :(") if not self.ui.buttonsDisabled: self.ui.disableButtons() else: self.fr.fbm.lastid = 0 self.lesson = 0 if not self.ui.defaultText: self.ui.writeDefaultText() if not self.ui.buttonsDisabled: self.ui.disableButtons() k = cv2.waitKey(100) if k == 27: quit() self.ui.lmain.after(10, self.refresh)
def __init__(self,host,port,username,password,db,firebasemanager): pr.pl("MySQL connecting") self.client = mysql.connector.connect(host=host,port=port,user=username,passwd=password,database=db) pr.pl("MySQL connected") self.fbm = firebasemanager
def stop(self): pr.pl("Stopping MQTT") self.client.loop_stop()
def start(self): pr.pl("MQTT Connecting to MQTT server") self.client.connect("MQTT server host", 8883) self.client.loop_start()
def on_subscribe(self): pr.pl("Successful subscribe")
def pressedNo(self): pr.pl("pressed no")
else: self.fr.fbm.lastid = 0 self.lesson = 0 if not self.ui.defaultText: self.ui.writeDefaultText() if not self.ui.buttonsDisabled: self.ui.disableButtons() k = cv2.waitKey(100) if k == 27: quit() self.ui.lmain.after(10, self.refresh) def main(self): try: self.ui.app.mainloop() except KeyboardInterrupt: quit() def startMqtt(self): self.listener.start() if __name__ == "__main__": p = Main("640x550", "testcam") pr.pl("Starting MQTT") p.startMqtt() pr.pl("Started MQTT") p.refresh() pr.pl("Setting TKinter and OpenCV") pr.pl("Opening window") p.main() pr.pl("Exiting")
def run(self): self.start() seconds = 0 pr.pl("Started file observer on "+str(self.__src_path)) try: while True: self.mqc.loop_start() if seconds >= self.TRAIN_DELAY and len(self.__event_handler.modimg) > 0: trainer = Trainer() faces,ids = trainer.getImagesAndLabels(self.__src_path) if(len(faces) > 0): pr.pl("Found {0} files.".format(len(faces))) trainer.train(faces,ids) pr.pl("Publishing done message") self.mqc.publish("cv/training", "training done at {0}".format(datetime.now()), 1) pr.pl("Published") else: pr.pl("No faces found. Cancelling training.") seconds = 0 self.__event_handler.clearArray() time.sleep(1) self.mqc.loop_stop() if seconds % 5 == 0: pr.pl("Time elapsed {0} seconds".format(seconds)) seconds += 1 except KeyboardInterrupt: self.stop()
def on_connect(self, client, userdata, flags, rc): pr.pl("MQTT Connected. Code: " + str(rc)) pr.pl("MQTT Subscribing") self.client.subscribe("cv/training") pr.pl("MQTT Subscribed")
def pressedYes(self): pr.pl("pressed yes") self.sqlm.setAttended(self.main.fr.studentid, True)