So I have a python while look going on, doing my vision process and printing out coordinates. At the end of the while loop, it prints out my coordinates with Table.putStringArray to my NetworkTable. Now the problem is, it now freezes my camera and only outputs coords for every 5seconds or every 12th frame. If I comment out this line, the code runs smoothly and I get solid fps. Is there a solution/can anyone reproduce it?
Get rid of the print statement?
Boom.
Also: Increasing webcam FPS with Python and OpenCV - PyImageSearch
This will walk you through how to multi-thread your frame grabber. i found a significant increase in fps by running my frame grabber on a separate thread.
Recommend you try using robotpy-cscore, it grabs frames in another thread using C++.
Would it be possible to give some code? That would be quite helpful in finding any weird bugs. It can just be hard to visualize things without seeing it.
In case this helps, last year my team used vision tracking on a RPi, sending data back over Network Tables. Our code can be found here - https://github.com/Team4613-BarkerRedbacks/2016-vision
Getting rid of the print statement had absolutely no effect of the code. Also the code ONLY runs slow with NetworkTables, the print statement actually ran extremely fast without it. Here’s some of my code with parts omitted for “epicness” concerns. http://pastebin.com/raw/xFGu0XwB
Oh yea, if need be, I can PM with the full code. Hope you can understand my caution
What are ‘maxOne’ and ‘maxTwo’?
Coords of first and second largest contour sorted by array.
There’s nothing that you’re doing that should cause issues… can you enable logging (see the networktables examples), perhaps there are informational messages there that could be helpful.
After seeing your full code, my guess is that the maxOne and maxTwo numpy arrays are causing issues… I’ve never tried passing a numpy array to putStringArray, but I’m surprised it’s not throwing an exception – after all, the contents of the array aren’t strings.
Either they’re really big (you can see this by running either OutlineViewer or this example), or there are error messages occurring (because it’s a numpy array) that are being hidden because you don’t have logging turned on. Recommend turning on logging and see what errors show up.
Oh, ha, cv2.rectangle returns a full image array. So it’s probably sending the full image array over NetworkTables each time you call putStringArray (hint: raw images are VERY big).
Yeah, definitely don’t do that. Instead, you should be sending the coordinates of the contour, not the rendered image. If you need to send the full image to the DS, use cscore (you’ll also find that cscore has significantly less latency than using cv2.VideoCapture).
I apologize but… how do you turn on logging?
How would I send the coordinates of the contour? Would I send “secondlargestcontour” and “green” instead?
If you don’t know the contents/format of the data you’re thinking about sending, I would try printing out the data that you think you want to send, and see if it makes sense to send it. Then do so if it does.
The pynetworktables troubleshooting documentation mentions this.
Thanks! I’ll test some things out tomorrow and get back to you!
Thank you! After I created a new variable to specifically order points the lag was reduced tremendously.