leswright1977 / PySpectrometer2

The Second Incarnation of the Spectrometer project!
Apache License 2.0
228 stars 34 forks source link

Simplifying to a single file, perf improvements, better user experience #7

Open JesseBuesking opened 1 year ago

JesseBuesking commented 1 year ago

I've broken up my changes into several commits to make it easier to understand what's going on.

I've combined the picam and usb independent files into a single file, that way any code changes done won't have to be replicated over multiple files. README updated to reflect the change. Basically pass --picam, --webcam, or --device X to choose your target.

Next up I made it possible to change the frameWidth and frameHeight without breaking the code. That way i can make the frame bigger so that selecting a specific pixel becomes easier.

After that we have several performance improvements. I basically profiled for both speed and memory, and made changes to address both. Nothing too crazy, just moving some logic out of the main loop that can be computed once and reused from within the loop. Also trading for loops for builtin numpy functions in spots, and finally reusing arrays instead of allocating new ones. Note: to simplify the review of a particular change here, I'd add ?w=1 to the end of the url above. That ignores whitespace changes, and there are a bunch of lines that were indented which will make it harder to review (IMO).

Finally I added logic when selecting a point to do calibration that looks for the nearest peak (within 5 places at the moment). That way if your abilities with a mouse are rubbish, we'll help you select the peak you were aiming for. Right-clicking undoes the last selection in case you make a mistake.

Let me know if you have any questions. Perf boosts result in a roughly 1.8x FPS increase on my Pi4, which makes the experience a bit smoother. I mainly started doing the perf changes because I increased the frameWidth and frameHeight which slows down cv2s drawing speed, making the experience less pleasant.