Comments (9)
My repo teaches you how to send data to Unity. But it doesn't care about how you generate that data. You seem to have the problem generating that data which means that your issue is quite unrelated to this repo. But I will help for the sake of it.
It seems that you haven't identified clearly the problem that you are having. The first step is to identify your specific problem. Find out which line of code produce the issue. Ask yourself what you expect to happen. And what actually happened.
Use print()
function to assist in your debugging.
My guess is that you are trying to read an image from Unity, run CNN in python, and then send the prediction back to Unity. So I would suggest you to inspect each step of this whole process. Check whether you got the image, first of all. Print the image's shape. After that, run the model, and see whether you get the prediction. Then check whether Unity got the prediction from ZeroMQ. Definitely one of these steps will be broken.
If any step of the process went wrong then at least you have narrowed down the potential source of problem. If you are not narrowing down the problem then that's not helping you fix the bug.
from unity3d-python-communication.
thank you for your quick response, I am actually not sending anything back to unity (except for a message), the script generates the binary mask and places it on a folder, but I just restarted the PC and it went through the script, however at the end it threw this error:
Traceback (most recent call last):
File "server.py", line 47, in <module>
socket.send(response.encode())
File "PATH\PythonStuff\envs\imageseg\lib\site-packages\zmq\sugar\socket.py", line 400, in send
return super(Socket, self).send(data, flags=flags, copy=copy, track=track)
File "zmq/backend/cython/socket.pyx", line 728, in zmq.backend.cython.socket.Socket.send
File "zmq/backend/cython/socket.pyx", line 775, in zmq.backend.cython.socket.Socket.send
File "zmq/backend/cython/socket.pyx", line 247, in zmq.backend.cython.socket._send_copy
File "zmq/backend/cython/socket.pyx", line 242, in zmq.backend.cython.socket._send_copy
File "zmq/backend/cython/checkrc.pxd", line 26, in zmq.backend.cython.checkrc._check_rc
raise ZMQError(errno)
zmq.error.ZMQError: Operation cannot be accomplished in current state
the final message from python wasn't received on unity.
from unity3d-python-communication.
OK. Now that's a legit question that's based purely on the design of ZeroMQ request-reply pattern.
In ZeroMQ's request-reply pattern, you are expected to send a request and then wait for a reply. You must exactly do one request and then wait for exactly one reply. If you only send requests or only wait for replies, or send one request and then not wait for a reply, things will break. The state machine of ZeroMQ expects strongly that you follow its rule.
This means that in Unity, you must send a request and must also wait for a reply. In python, you must receive a request and send the reply. Both sides must do these things in a loop. If either server
or client
violate the rule, things break.
Let me know if that solves your problem.
from unity3d-python-communication.
You are right, I am doing 2 socket.send from server.py but only 1 from unity, I will test it later to see how it goes.
thank you
from unity3d-python-communication.
Ok, it went through smoothly, I removed the first socket.send and got rid of the thread on server.py. I am just starting to read the documentation on zeromq, and I know you mentioned that the server and client need to be reset or terminated to not crash unity. I check on client that you could do either client.Disconnect()
or client.Dispose()
, which one would you recommend to use after I got the response I wanted from the server? also should I disconnect manually form server.py as well to not crash unity?
Thank you
from unity3d-python-communication.
This code receives messages 10 times on Unity, and it works without crashing.
Just break after you got your message so that NetMQConfig.Cleanup()
can be called. And that should be enough to end your client.
from unity3d-python-communication.
yeah I'm calling both on unity and stopping the thread to see if the ports and everything was closed smoothly (I'm also doing socket.close()
and context.term()
on server.py
just in case) I was wondering if there is a way to activate an environment from script so I don't have to call everything myself in the console, since I pretend to make an android build, I know its kind of unrelated to the repository, but do you know by chance how could we call server.py from unity? I'm using packages such as tensorflow and pycocotools, so I need python3 and can't use ironpython or python for unity since they do not support python 3 yet.
Any help would be appreciated, if not I'll just close the issue since we went out of scope here
from unity3d-python-communication.
Try running the python
command and environment activations using this method at the start of the unity scene.
https://stackoverflow.com/questions/1469764/run-command-prompt-commands
I've never tried it but it should work because it's similar to how you run commands in cmd
.
And if you want to run multiple commands, try something like this: https://stackoverflow.com/questions/437419/execute-multiple-command-lines-with-the-same-process-using-net
You can do
conda activate YOUR_ENVIRONMENT
python server.py
When you quit the game, make sure to kill the Process
that you have created.
Another thing is about packaging your python stuff, you need to learn how to freeze all your python packages into a folder so that it can be distributed with the Unity Build. I've never tried that much. But I'm sure you could do it.
Here's the starting point:
https://docs.python-guide.org/shipping/packaging/
from unity3d-python-communication.
That's perfect, I wasn't sure even I if did all that if it would work on an android build, I also tried to set an environment without anaconda with plain python since anaconda is much heavier. Thanks for the information about how to create python packages as well
from unity3d-python-communication.
Related Issues (20)
- Python to Unity HOT 5
- There were some problems when I opened your project in Unity3D HOT 5
- Can this be used for communication between 2 pc? HOT 3
- Trying to understand the reason for using both threading and non-blocking TryReceiveFrameString HOT 3
- 'Lazy Pirate' Reliability pattern HOT 1
- Sending message based on button press HOT 4
- Sending Frames from Unity to Python HOT 6
- Sending Images from Unity to Python HOT 2
- How to extract messages from RunAbleThread to monobehaviour? HOT 1
- Client just stops receiving stuff after ~30 seconds HOT 2
- I've written nice article based on your code
- Unity-Python communication (the python client is running in the browser) HOT 2
- Can be used for communication web browser and local machine? HOT 11
- Can it send different info to different clients? HOT 6
- About send pictures HOT 1
- about broadcast message HOT 2
- connect SUMO to Unity3D HOT 1
- how to send vector3 list from unity to python HOT 1
- Communication stops HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from unity3d-python-communication.