我正在尝试使用进程(IPC)从python脚本接收数据。这是从python统一阅读词典的某种方法吗???我之所以使用流程,是因为速度在我的情况下至关重要。 理想情况下,我希望从unity发送一个图像,并从python获得基于该图像的结果。先谢谢你
统一代码:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
public class Main : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Console.WriteLine("Execute python process...");
Option1_ExecProcess();
}
// Update is called once per frame
void Update()
{
}
static void Option1_ExecProcess()
{
// 1) Create Process Info
var psi = new ProcessStartInfo();
psi.FileName = @"python.exe";
// 2) Provide script and arguments
var script = @"pyScript.py";
psi.Arguments = $"\"{script}\"";
// 3) Process configuration
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
// 4) Execute process and get output
var errors = "";
var results = "";
using (var process = Process.Start(psi))
{
errors = process.StandardError.ReadToEnd();
results = process.StandardOutput.ReadToEnd();
}
}
}
Python脚本:
import logging
import math
import time
import traceback
import numpy as np
import io
from numpy import asarray, expand_dims
import tensorflow as tf
from tensorflow import keras
import base64
from tensorflow.python.keras.utils import data_utils
import os
def closestDivisors(num):
n = math.floor(math.sqrt(num))
for i in range(n, 0, -1):
if (num) % i == 0:
res = [i, (num)//i]
break
return res
def fixed_length_str(i):
return '%02d' % i
layers_indices = []
data = {}
def init():
image_model = tf.keras.applications.VGG16(include_top=True,
weights='imagenet', pooling='avg')
for index, layer in enumerate(image_model.layers):
if 'fc' in layer.name:
layers_indices.append(index)
if 'dense' in layer.name:
layers_indices.append(index)
if 'conv' in layer.name:
layers_indices.append(index)
if 'predictions' in layer.name:
layers_indices.append(index)
outputs = [image_model.layers[i].output for i in layers_indices]
image_model = tf.keras.Model(inputs=image_model.inputs, outputs=outputs)
return image_model, layers_indices
def return_data(image_model,layers_indices):
try:
img = tf.keras.preprocessing.image.load_img('p4.png', target_size=(224, 224))
img = tf.keras.preprocessing.image.img_to_array(img) # convert to tensor
img = expand_dims(img, axis=0)
feature_maps = image_model.predict(img)
layer_counter = -1
for fmap in feature_maps: # of each layer
feature_map = {}
layer_counter = layer_counter + 1
ix = 1
layer_name = image_model.layers[layers_indices[layer_counter]].name
if 'conv' in layer_name:
x, z = closestDivisors(
image_model.layers[layers_indices[layer_counter]].output_shape[3])
for i in range(x):
for j in range(z):
imageArray = fmap[0, :, :, ix-1] # images ndarray ix-1
imageArray = imageArray - imageArray.min()
if imageArray.max() != 0:
imageArray = imageArray/imageArray.max() * 255.0
imageArray = imageArray.astype(np.uint8)
var_name_conv = 'fmap_{}_{}'.format(
layer_name, fixed_length_str(i) +""+ fixed_length_str(j))
feature_map[var_name_conv] = imageArray.tolist()
ix += 1
elif 'fc' in layer_name:
x, z = closestDivisors(
image_model.layers[layers_indices[layer_counter]].output_shape[1])
fmap = fmap - fmap.min()
if fmap.max() != 0:
fmap = fmap/fmap.max() * 255.0
fmap = fmap.astype(np.uint8)
var_name_fc = 'fmap_{}'.format(
layer_name)
feature_map[var_name_fc] = fmap.tolist()
elif 'predictions' in layer_name:
x, z = closestDivisors(
image_model.layers[layers_indices[layer_counter]].output_shape[1])
fmap = fmap - fmap.min()
if fmap.max() != 0:
fmap = fmap/fmap.max() * 255.0
fmap = fmap.astype(np.uint8)
var_name_pred = 'fmap_{}'.format(layer_name) #, fixed_length_str(x), fixed_length_str(z)
feature_map[var_name_pred] = fmap.tolist()
data[str(image_model.layers[layers_indices[layer_counter]].name)] = feature_map
return data
except:
pass
def __init__(self):
model, layers = init()
return return_data(model, layers)
有许多方法可以通过输入流发送数据。标准输出,就像互联网只是从一个进程向另一个进程发送字节的一种方式。有许多方法可以通过数据流发送字典之类的内容
一种是JSON,在互联网上非常常见。它是一种基于文本的格式。这意味着在流上发送信息需要更多的数据,编码和解码也会更慢。但是,使用它要容易得多。Python包含
json
模块,用于轻松地与json进行转换如果您发现json提供的速度太慢,那么可以设计一种costum字节压缩数据格式。这种格式可以更加紧凑,因为您不需要包含键的名称,并且可以使用固定长度的数字而不是十进制表示。由于数据已经是本机数据格式,因此打包和解包的速度也会快得多。python中的
struct
模块允许您将基本原语(整数、字符串)转换为结构表示形式,但是您需要将它们组合起来,自己对整个字典进行编码哪种选择最好取决于你。有很多其他的方法可以做到这一点,如果你想要更多的灵感,可以查阅数据传输协议
相关问题 更多 >
编程相关推荐