def _process(self): format_tail = ".png" while True: ret = self.check_is_new() if ret: for one_p in ret: tag, face_encoding, face_name = self.load_encodings_names( one_p) if tag: self.add_person_info(face_name, face_encoding) serialize_file = self.serialize_encoding_2_file( face_encoding, face_name) add_person_info( face_name, 0, 'm', os.path.join(self.target_folder, one_p), serialize_file) logger.info("add a person %s " % face_name) else: logger.info( "not find a face, will remove this image to /tmp/ dir!" ) if not tag: shutil.move(os.path.join(self.listen_folder, one_p), os.path.join("/tmp/", one_p)) else: shutil.move(os.path.join(self.listen_folder, one_p), os.path.join(self.target_folder, one_p)) else: time.sleep(0.5)
def __init__(self): # some default initialize self.face_frame = None self.thread = None self.already_initialized = False self.folder_path = None self.known_face_encodings = None self.known_face_names = None self.process_this_frame = True self.snapshot = cp.get("camera_dev", "snapshot") self.camera_id = cp.get("camera_dev", "camera_id") self.show = cp.getboolean("show", "show_frame") try: self.camera_id = int(self.camera_id) logger.info("camera_id = %d" % self.camera_id) except Exception: logger.info("camera_url = %s" % self.camera_id) width = cp.getint("camera_dev", "width") height = cp.getint("camera_dev", "height") # 配置摄像头参数 self.cap = cv2.VideoCapture(self.camera_id) self.cap.set(3, height) self.cap.set(4, width) self.png_data = None self.initialize()
def file2encoding(self): try: image_encoding = pickle.load(open(self.serialize_file)) logger.debug("serialize_file %s" % self.serialize_file) return image_encoding, self.name except Exception, e: logger.info("error : %s" % e) return None, None
def load_encodings_names(self, imagepath): image = face_recognition.load_image_file(imagepath) try: image_encoding = face_recognition.face_encodings(image)[0] except Exception: logger.info("can not find a face in this pic %s" % imagepath) if len(image_encoding) > 0: return image_encoding, imagepath[:-4] else: return None, None
def ndarray2disk(data): n, length, height, width, channel = data.shape imagespath = getstrtime() logger.info(imagespath) imagespath = os.path.join("/home/jack/Desktop/test_cam/", imagespath) if not os.path.exists(imagespath): os.mkdir(imagespath) for i in range(length): imagename = "%06d.jpg" % i cv2.imwrite(os.path.join(imagespath, imagename), data[0, i, :])
def load_encodings_names(self, imagepath): image = face_recognition.load_image_file( os.path.join(self.listen_folder, imagepath)) try: face_locations = face_recognition.face_locations(image) face_location = self.max_face_location(face_locations) image_encoding = face_recognition.face_encodings( image, face_location)[0] except Exception, e: logger.info(str(e)) logger.info("can not find a face in this pic %s" % imagepath) image_encoding = []
def read(self): """ get frames form the url self.snaphsot!!! """ stream = None bytes = None try: stream = urllib2.urlopen(self.snapshot) bytes = stream.read() except Exception, e: logger.info("error handing the url % s" % self.snapshot) return False, None
def add_person_info(name, age, sex, imagepath, serializefile, tablename="steaf"): db = pymysql.connect("localhost", "root", "abc", "trycatch", charset="utf8") sql = "insert into %s values ('%s', %d, '%s', '%s', '%s')" % (tablename, name, age, sex, imagepath, serializefile) logger.info("execute %s " % sql) cursor = db.cursor() try: cursor.execute(sql) db.commit() return True except Exception, e: logger.info("an error happend when execute sql %s " % sql) db.rollback() return False
def select_person_info(tablename="steaf"): db = pymysql.connect("localhost", "root", "abc", "trycatch", charset="utf8") cursor = db.cursor() sql = "select name, image_path, serialize_file from %s ;" % tablename logger.info(sql) person_list = [] try: cursor.execute(sql) results = cursor.fetchall() for row in results: person = Person(row[0], row[1], row[2]) person_list.append(person) except: logger.info("error when select in db operation") return None return person_list
def load_encoding_names_via_database(self): person_list = select_person_info() known_face_encodings = [] known_names = [] if person_list: for person in person_list: encoding, name = person.file2encoding() if not name: logger.warning("CAN NOT FIND A SERALIZED FILE %s" % person.serialize_file) continue known_names.append(name) known_face_encodings.append(encoding) logger.info("add a person %s" % name) else: logger.info("can not load a person") return known_face_encodings, known_names
def initialize(self): if self.initialized: logger.info("palistener already start") else: self.initialized = True logger.info("ready to start palistener") process = Thread(target=self._process, args=()) logger.info("face add listener thread start") process.setDaemon(True) process.start()
def __init__(self, name, image_path, serialize_file): self.name = name self.image_path = image_path self.serialize_file = serialize_file logger.info("person %s %s %s" % (self.name, self.image_path, self.serialize_file))
def getcp(): cp1 = configparser.ConfigParser() logger.info("configparser parse the file config.txt config") cp1.read("configurations/config.txt") return cp1
def signal_handler(signum, frame): logger.info("pid %d going to terminate!!!" % os.getpid()) sys.exit(0)
while True: send = facereco.png_data yield (b'--frame\r\n' b'Content-Type: image/png\r\n\r\n' + send + b'\r\n') #"""python GEL Lock !!! must need to sleep some time !!!""" time.sleep(1.0/12) def writepid(): pidfile = cp.get("store", "pidfile_path") file = open(pidfile, "w") pid = os.getpid() file.write(str(pid)) file.close() def signal_handler(signum, frame): logger.info("pid %d going to terminate!!!" % os.getpid()) sys.exit(0) if __name__ == '__main__': writepid() port = cp.getint("server_dev", "port") host = cp.get("server_dev", "host") http_server = WSGIServer((host, port), app) signal.signal(signal.SIGINT, signal_handler) try: http_server.serve_forever() except Exception, e: print e logger.info("this server to be killed")
def stophandler(signum, frame): print "receive signal : ", signum if signum == 2: logger.info("stop this module") sys.exit(signum)
#-*- coding:utf-8 -*- import os import signal from configurations.cpp import cp from loggingconf.log import logger pidfile = cp.get("store", "pidfile_path") try: file = open(pidfile, "r") except: logger.info("pidfile not exists") pid = file.read() file.close() pid = int(pid) try: os.kill(pid, signal.SIGINT) #interrupt zhongduan logger.info("will stop the process of %d." % pid) print "kill success" except OSError, e: logger.info("an error happend when kill the process %d" % pid) print "kill error"
# todo change the numpy to json format def pred2json(pred): pass if __name__ == '__main__': pidfilepath = cp.get("store", "pidfile_path") pidfile = open(pidfilepath, "w") pid = os.getpid() pidfile.write(str(pid)) pidfile.close() camera_mode = camera.camera_mode # os.chdir(trycatch_path) logger.info("load the net") network = cp.get("strategy", "net") tail = None pwd = None prototxt = None model = None if network == "tc_3d": tail = network elif network == "tc_3d_lstm_res": tail = network else: tail = None if not tail: logger.warning( "the strategy in the config.txt need to be one of tc_3d, tc_3d_lstm_res"