[英]Using single AsyncTask to both send and then receive data to/from Python server
I want to send 40x40px blobs to my Python server, then process it there and send back a reply with id representing the image class (its an image classification task).我想将 40x40px 的 blob 发送到我的 Python 服务器,然后在那里处理它并发回一个带有代表图像类的 id 的回复(它是一个图像分类任务)。 I use AsyncTask and here comes a problem - the blob is sent to the server but then the part responsible for receiving the reply is not reached in my Android code.我使用 AsyncTask 并且出现了一个问题 - blob 被发送到服务器,但随后在我的 Android 代码中没有到达负责接收回复的部分。
I wonder whether it is correct to both send and then receive data in single AsyncTask.我想知道在单个 AsyncTask 中发送和接收数据是否正确。 I have read that tasks taking about <10 seconds are fine for this solution, so theoretically there should be no problem in my case.我已经读过大约 <10 秒的任务对于这个解决方案来说很好,所以理论上我的情况应该没有问题。
Here I enclose my code, for Client:在这里,我附上了我的代码,用于客户端:
public class ServerConnectAsyncTask extends AsyncTask<Void, Void, Integer> {
private AsyncTaskResultListener asyncTaskResultListener;
private Socket socket;
private Mat img;
ServerConnectAsyncTask(Mat blob, Context c) throws IOException {
img = blob;
asyncTaskResultListener = (AsyncTaskResultListener) c;
}
@Override
protected Integer doInBackground(Void... voids) {
MatOfByte buf = new MatOfByte();
Imgcodecs.imencode(".jpg", img, buf);
byte[] imgBytes = buf.toArray();
try {
socket = new Socket("192.168.0.109",8888);
DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
DataInputStream din = new DataInputStream(socket.getInputStream());
dout.write(imgBytes);
dout.flush();
String str = din.readUTF(); // it seems that it doesn't reach this line
dout.close();
din.close();
socket.close();
return Integer.valueOf(str);
} catch (IOException e) {
e.printStackTrace();
return 99;
}
}
@Override
protected void onPostExecute(Integer imgClass) {
asyncTaskResultListener.giveImgClass(imgClass);
}
}
And for python server:对于 python 服务器:
HOST = "192.168.0.109"
PORT = 8888
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(10)
while True:
conn, addr = s.accept()
print("Got connection from", addr)
msg = conn.recv(4096)
buf = np.frombuffer(msg, dtype=np.uint8).reshape(-1, 1)
img = cv2.imdecode(buf, 0)
cv2.imwrite("output.jpg", img) # here I save my blob correctly
if msg:
message_to_send = "0".encode("UTF-8") # then I send my "predicted" image class
conn.send(message_to_send)
else:
print("no message")
What is also important is that I call the AsyncTask.execute()
in my onCameraFrame()
method - once a while (not in every frame, only when my blob is sufficiently "stable", which happens rather rarely).同样重要的是,我在onCameraFrame()
方法中调用AsyncTask.execute()
- 一次(不是在每一帧中,只有当我的 blob 足够“稳定”时,这种情况很少发生)。
Apparently it got stuck on readUTF()
part.显然它卡在了readUTF()
部分。 Now it works:现在它的工作原理:
DataInputStream din = new DataInputStream(socket.getInputStream());
int str = din.read();
char sign = (char) str;
dout.close();
din.close();
socket.close();
return Character.getNumericValue(sign);
Now it returns 0 when I send "0" from the python server, so it works fine for me.现在当我从 python 服务器发送“0”时它返回 0,所以它对我来说很好用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.