示例#1
0
    def handle_conn(self):
        """
        连接处理
        :return:
        """
        log.info('Socket conn eastablished with %s:%s' % (self.addr))
        # 检查美学模型是否是ok的,如果不是ok的,那么告诉客户端你凉了
        if self.aes_model == None or (not self.aes_model.initFinish()):
            self.sock.send(Config.CODE_ERROR)
            self.sock.close()
            log.info('Aes not init, return CODE_ERROR')
            return

        # 美学检测通过,返回CODE_OK,进行下一步
        self.sock.send(Config.CODE_OK.encode('utf-8'))

        # 正式接受数据
        received_path = self.recv_end()
        log.info('Received path at -> %s' % (received_path))

        # 路径校验
        suc, normed_path, msg = FileUtils.valid_path(received_path,
                                                     relative_path=False)
        if not suc:
            self.sock.send(Config.CODE_FILE_ERROR.encode('utf-8'))
            self.sock.close()
            log.info('Received path at -> %s is invalid!' % (received_path))
            return

        # 进行美学评估
        aesScore = self.aes_model.runModelForSingleImg(normed_path, True)
        self.sock.send(aesScore.encode('utf-8'))
        self.sock.close()
        log.info('Aes over, score = %s for img->%s' % (aesScore, normed_path))
示例#2
0
def launch_model():
    """
    启动美学模型
    :return:
    """
    from core.PhotoAesModel import aes_model
    aes_model.initModel()
    log.info('Init aes_model over')
    return aes_model
示例#3
0
def start_listen():
    """
    开启端口监听
    :return:
    """
    # 使用ipV4
    socket_conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 本机服务,端口8848
    socket_conn.bind((Config.LISTEN_IP, Config.LISTEN_PORT))
    # 并发连接数目为4
    socket_conn.listen(Config.CONCURRENT_NUM)
    log.info('Start to listener at port %d' % (Config.LISTEN_PORT))
    return socket_conn
示例#4
0
    def initModel(self):
        """
        模型初始化
        全局只会初始化一次
        包括:
        1.inceptionV2结构化定义
        2.加载模型权重
        :param weightsFilePath:
        :return:
        """
        from keras.models import Model
        from keras.layers import Dense, Dropout
        from keras.applications.inception_resnet_v2 import InceptionResNetV2

        # 初始化过,就不要再初始化了
        if self.initFinish():
            log.info('the model has already init before, now re-use it')
            return

        # 同步Graph
        self.graph = tf.Graph()
        # session = tf.Session()

        # 模型构建
        log.info(('start to build model based on weights in -> %s' %
                  (self.weightsFile)))
        # with tf.device('/CPU:0'):

        with self.graph.as_default():
            # with session.as_default():
            # 模型参数定义
            base_model = InceptionResNetV2(input_shape=(None, None, 3),
                                           include_top=False,
                                           pooling='avg',
                                           weights=None)
            log.info('suc create v2 model')
            x = Dropout(0.75)(base_model.output)
            x = Dense(10, activation='softmax')(x)
            self.model = Model(base_model.input, x)
            # 读取权重数据
            self.model.load_weights(self.weightsFile)
            self.model._make_predict_function()

        # graph = tf.get_default_graph()
        self.hasModelInit = True
        log.info('model init complete!')
        return
示例#5
0
    def runModelForSingleImg(self, imgFilePath, resize):
        """
        运行模式 - 单照片运行
        这个函数必须在模型已经初始化完毕之后才能使用
        注意,此函数线程安全,进程不安全
        :param imgFilePathLst:
        :param resize:
        :return:
        :param imgFilePath:
        :param resize:
        :return:
        """
        if not self.initFinish:
            log.error(
                'Failed to calculate aes score : model is not initialized!')
            return

        # with tf.device('/CPU:0'):
        #     with graph.as_default():
        with self.graph.as_default():

            log.info('Now processing img -> %s' % (imgFilePath))
            # 预处理
            imgArr = self._preImgProcess(imgFilePath, resize)
            try:
                self.lock.acquire()
                log.info('Now preprocessing img -> %s' % (imgFilePath))
                # 预测
                scores = self.model.predict(imgArr, batch_size=1, verbose=0)[0]
                aesScore = score.calculateAesScore(scores)
                aesScore = aesScore * 10
                log.info('The score for img -> %s is %.2f' %
                         (imgFilePath, aesScore))
            finally:
                self.lock.release()

        return str(aesScore)