class CamAPI(): """ CamAPI Class The CamAPI module is used by the HIAS Sercurity Service to provide an API endpoint allowing devices on the HIAS network to identify known and unknown users via HTTP request. """ def __init__(self): """ Initializes the class. """ self.Helpers = Helpers("Camera") # Initiates the iotJumpWay connection class self.iot = iot() self.iot.connect() # Starts the TassAI module self.TassAI = TassAI() # Loads the required models self.TassAI.load_models() # Loads known images self.TassAI.load_known() self.Helpers.logger.info("Camera API Class initialization complete.") def life(self): """ Sends vital statistics to HIAS """ cpu = psutil.cpu_percent() mem = psutil.virtual_memory()[2] hdd = psutil.disk_usage('/').percent tmp = psutil.sensors_temperatures()['coretemp'][0].current r = requests.get('http://ipinfo.io/json?token=' + self.Helpers.confs["iotJumpWay"]["ipinfo"]) data = r.json() location = data["loc"].split(',') self.Helpers.logger.info("TassAI Life (TEMPERATURE): " + str(tmp) + "\u00b0") self.Helpers.logger.info("TassAI Life (CPU): " + str(cpu) + "%") self.Helpers.logger.info("TassAI Life (Memory): " + str(mem) + "%") self.Helpers.logger.info("TassAI Life (HDD): " + str(hdd) + "%") self.Helpers.logger.info("TassAI Life (LAT): " + str(location[0])) self.Helpers.logger.info("TassAI Life (LNG): " + str(location[1])) # Send iotJumpWay notification self.iot.channelPub("Life", { "CPU": str(cpu), "Memory": str(mem), "Diskspace": str(hdd), "Temperature": str(tmp), "Latitude": float(location[0]), "Longitude": float(location[1]) }) threading.Timer(300.0, self.life).start() def startIoT(self): """ Initiates the iotJumpWay connection. """ self.Device = Device({ "host": self.Helpers.confs["iotJumpWay"]["host"], "port": self.Helpers.confs["iotJumpWay"]["MQTT"]["port"], "lid": self.Helpers.confs["iotJumpWay"]["MQTT"]["TassAI"]["lid"], "zid": self.Helpers.confs["iotJumpWay"]["MQTT"]["TassAI"]["zid"], "did": self.Helpers.confs["iotJumpWay"]["MQTT"]["TassAI"]["did"], "an": self.Helpers.confs["iotJumpWay"]["MQTT"]["TassAI"]["dn"], "un": self.Helpers.confs["iotJumpWay"]["MQTT"]["TassAI"]["un"], "pw": self.Helpers.confs["iotJumpWay"]["MQTT"]["TassAI"]["pw"] }) self.Device.connect() self.Device.channelSub() self.Device.commandsCallback = self.commandsCallback self.Helpers.logger.info("iotJumpWay connection initiated.") def threading(self): """ Creates required module threads. """ # Life thread Thread(target=self.life, args=(), daemon=True).start() def signal_handler(self, signal, frame): self.Helpers.logger.info("Disconnecting") self.iot.disconnect() sys.exit(1)
class CamRead(Thread): """ CamRead Class The CamRead Class processes the frames from the local camera and identifies known users and intruders. """ def __init__(self): """ Initializes the class. """ self.Helpers = Helpers("CamRead") super(CamRead, self).__init__() self.Helpers.logger.info("CamRead Class initialization complete.") def run(self): """ Runs the module. """ fps = "" framecount = 0 time1 = 0 time2 = 0 mesg = "" self.font = cv2.FONT_HERSHEY_SIMPLEX self.color = (0, 0, 0) # Starts the TassAI module self.TassAI = TassAI() # Connects to the camera self.TassAI.connect() # Loads the required models self.TassAI.load_models() # Loads known images self.TassAI.load_known() self.publishes = [None] * (len(self.TassAI.faces_database) + 1) # Starts the socket server soc = self.Sockets.connect(self.Helpers.confs["Socket"]["host"], self.Helpers.confs["Socket"]["port"]) while True: try: t1 = time.perf_counter() # Reads the current frame frame = self.TassAI.camera.get(0.65) width = frame.shape[1] # Processes the frame detections = self.TassAI.process(frame) # Writes header to frame cv2.putText(frame, "TassAI Camera", (10, 30), self.font, 0.5, self.color, 1, cv2.LINE_AA) # Writes date to frame cv2.putText(frame, str(datetime.now()), (10, 50), self.font, 0.3, self.color, 1, cv2.LINE_AA) if len(detections): for roi, landmarks, identity in zip(*detections): frame, label = self.TassAI.draw_detection_roi( frame, roi, identity) #frame = self.TassAI.draw_detection_keypoints(frame, roi, landmarks) if label is "Unknown": label = 0 mesg = "TassAI identified intruder" else: mesg = "TassAI identified User #" + str(label) # If iotJumpWay publish for user is in past if (self.publishes[int(label)] is None or (self.publishes[int(label)] + (1 * 120)) < time.time()): # Update publish time for user self.publishes[int(label)] = time.time() # Send iotJumpWay notification self.iot.channelPub( "Sensors", { "Type": "TassAI", "Sensor": "USB Camera", "Value": label, "Message": mesg }) # Send iotJumpWay notification self.iot.channelPub( "Cameras", { "Type": "TassAI", "Sensor": "USB Camera", "Value": label, "Message": mesg }) cv2.putText(frame, fps, (width - 120, 26), cv2.FONT_HERSHEY_SIMPLEX, 0.3, self.color, 1, cv2.LINE_AA) # Streams the modified frame to the socket server encoded, buffer = cv2.imencode('.jpg', frame) soc.send(base64.b64encode(buffer)) # FPS calculation framecount += 1 if framecount >= 15: fps = "Stream: {:.1f} FPS".format(time1 / 15) framecount = 0 time1 = 0 time2 = 0 t2 = time.perf_counter() elapsedTime = t2 - t1 time1 += 1 / elapsedTime time2 += elapsedTime except KeyboardInterrupt: self.TassAI.lcv.release() break