当前位置:首页 > AI > 正文内容

PHP使用SOCKET调用TensorFlow服务器实现图片鉴黄

chanra1n3年前 (2021-02-01)AI3628

PHP代码

<?php
define("UNIX_DOMAIN","/socks/tfserver.sock");
$socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
$result = socket_connect($socket, UNIX_DOMAIN);
$in = "test.jpg";     
if(socket_write($socket, $in, strlen($in))) 
 { 
      echo socket_read($socket, 8192);
 }  
?>

打开sock链接,然后发送$in至TensorFlow,等待结果并输出至客户端,test.jpg可以是其他的图片,也可以是路径,代表需要被简单的图片


TF服务器代码

# -*- coding: utf-8 -*-
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import os
import stat
import numpy as np
import socket

def init_socket():
    server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) #创建Socket对象server
    if os.path.exists("/socks/tfserver.sock"): #判断/tmp/tfserver.sock是否存在
        os.unlink("/socks/tfserver.sock") #删除/tmp/tfserver.sock
    server.bind("/socks/tfserver.sock") #绑定地址到嵌套字
    os.chmod("/socks/tfserver.sock",stat.S_IRWXU|stat.S_IRWXG|stat.S_IRWXO)
    server.listen(0) #不监听TCP
    return server #返回server对象

def id_to_string(node_id):
    if node_id not in uid_to_human:
        return ''
    return uid_to_human[node_id]

lines = tf.gfile.GFile('./inception_model/output_labels.txt').readlines()
uid_to_human = {}
TFServer = init_socket()

for uid, line in enumerate(lines):
    line = line.strip('\n')
    uid_to_human[uid] = line

with tf.gfile.FastGFile('./inception_model/output_graph.pb', 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    tf.import_graph_def(graph_def, name='')

with tf.Session() as sess:
    softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
    while True:
        #这里等待接收用户的图片信息。
        connection, address = TFServer.accept()
        rec = connection.recv(1024)
        picPath = str(rec).encode('utf-8')
        #picPath = str(rec)
        print (picPath)
        image_data = tf.gfile.GFile(picPath, 'rb').read()
        predictions = sess.run(softmax_tensor, {'DecodeJpeg/contents:0': image_data})
        predictions = np.squeeze(predictions)

        top_k = predictions.argsort()[::-1]
        result = ''
        for node_id in top_k:
            human_string = id_to_string(node_id)
            score = predictions[node_id]
            buf = ('\"%s\":%.2f,' % (human_string, score*100))
            result = result + buf
        result = "{"+ result[:-1] +"}"
        print(result)
        result = str(result).encode('utf-8')
        #将打分结果返回给提交打分申请的客户端
        connection.send(bytes(result))
    connection.close()

该代码引自 CSDN Tensorflow图片鉴黄 完整项目 ,并进行了优化和修改,改变了sock的保存模式,解决了权限不足的问题,其次优化了结果的显示方式,结果会被返回为JSON格式,方便PHP解析或JavaScript分析。

在进行了大量的自动化测试之后发现,该项目pb精确度并不是很高,需要进行进一步训练,但是模型稳定性较高,如果用于生产,请在PHP中加入安全限制。

扫描二维码推送至手机访问。

版权声明:本文由我的FPGA发布,如需转载请注明出处。

本文链接:https://www.myfpga.cn/index.php/post/179.html

分享给朋友:

“PHP使用SOCKET调用TensorFlow服务器实现图片鉴黄” 的相关文章

python基础三层深度学习网络

python基础三层深度学习网络

#coding:utf-8 #neural network class definition import numpy import scipy.spatial class neuralNetwork:   &...

解决tfClassifier训练报错的问题 修正后python 适用于tensorflow2.x python3.x

解决tfClassifier训练报错的问题 修正后python 适用于tensorflow2.x python3.x

# -*- coding: utf-8 -*-"""Created on Sun Dec 29 19:21:08 2019@原作者: xiuzhang Eastmount CSDN@修改作者:ChanRa1n修正问题:TensorFlow版本低,学习速率过高,修正为0....

简单OpenCV人脸识别

简单OpenCV人脸识别

# -*- coding: utf-8 -*- """ Created on Sat Dec  5 22:39:13 2020 @author:&nb...

基于CycloneV使用Paddle Lite,并分别使用单独HPS和FPGA加速对比效果。

基于CycloneV使用Paddle Lite,并分别使用单独HPS和FPGA加速对比效果。

第一部分、仅使用HPS进行计算第一步、通过ssh链接至开发板第二步、解决apt-get存在的问题chmod 644 /usr/lib/sudo/sudoers.so && chown -R root /usr/li...