jo-m / trainbot

Watches a piece of train track, detects trains, and stitches together images of them.
https://trains.jo-m.ch/
MIT License
467 stars 8 forks source link

Tips for cropping? #16

Closed natevw closed 11 months ago

natevw commented 11 months ago

I am nearly half a kilometer from the tracks but do have a view of passing trains. But I am not getting great results (sidestepping the issues of #15 for now by passing it directly a file path instead of V4L) and wondering if you have any advice/insight.

With the annotated crop region it does detect the motion of the train:

webcam image of train with a rectangle drawn over partial view of train

But it does not combine the whole train, only small bits. This is the first image, i.e. it misses the engine completely: train_00010101_000038 75_Z

followed by:

train_00010101_000039 75_Z

and:

train_00010101_000136 708_Z

and a nice little stretch:

train_00010101_000156 791_Z

but then that's it for the whole train!

Any tips as to what crop region I should be aiming for? For example, if I expand the box to include the train on both sides of the tree, trainbot seems to just not trigger on the motion at all.

Admittedly this is not very ideal view of the train, but it seems to be showing some promise! 🥳 Any tips as to what to try? What sorts of things matter most — would the first step be to try straighten out the camera so it's not angled? adjust the crop region bigger or smaller? use a longer telephoto lens for more pixels/meter?

jo-m commented 11 months ago

Hi!

  1. The crop region shown looks good IMO. Unless the trains are running much faster than the one shown here, there is little benefit in having a bigger one, especially if there are trees in the way.
  2. Yes, straightening the camera should help, I am sure the patch matching does not work well with this much rotation.
  3. Lastly, have you set the --px-per-m, --min-speed-kph, --max-speed-kph params? This influences the decisionmaking on when a train "starts" and "ends" and can also lead to this slicing you are seeing.
clonejo commented 11 months ago

I have had some issues with trains being split too often as well. Mostly with ICEs and freight trains. I guess the ICEs just look to uniform in the X direction at some points. And the freight trains do have gaps, at least in the upper two thirds (empty cars).

I have tinkered with the train detection a bunch in https://github.com/clonejo/trainbot/commit/bed35a54f9c1424e5e150c8b241370a45a53ea77 to make things work, but i need to clean that up before an MR.

natevw commented 11 months ago

@jo-m Thanks, getting much closer! I think the main thing was rotating it by about 6º anti-clockwise. (And then also had to pre-crop my sample video due to #19.)

Then with:

export INPUT=sample-crop2.mp4
export RECT_X=50
export RECT_Y=50
export RECT_W=198
export RECT_H=133
export PX_PER_M=19
export MIN_SPEED_KPH=5
export MAX_SPEED_KPH=200
./trainbot

I do get quite a bit of the train 🎉

long panorama of a freight train

from this file:

https://github.com/jo-m/trainbot/assets/265902/611dc998-bd75-4e0b-ac34-6eac70d58e02

but it is missing the front engines. (Which it did detect in an earlier run with some different settings which I've lost :-/ but that was still lots of smaller chunks rather than almost the whole train.)

natevw commented 11 months ago

I think this can probably be closed, as it was a discussion rather than an issue anyway!

With a little different sample and playing around with exact crop regions and stuff (still only pre-recorded files), I was able to get my first full train panorama! 🎊

train_00010101_000006 96_Z-2

Thank you, so very fun! And @clonejo thank you as well I hope to at some point test out your fork as well and maybe try some of my own changes if I have a chance in coming weeks.

Next step for me is probably get set up with even further telephoto lens and camera mounted better so it's not needing software rotation — then dial in the values better.

jo-m commented 11 months ago

nice!