measuring the height of a tennis ball after detection

Hello, I need the openmv to give me the approximate height of a tennis ball after detecting it. I have trained my data and the recognition works fine, however, I’m having difficulty measuring the height. I have seen from your other forums the formula which is used to measure the height

def rect_size_to_distance®: # r == (x, y, w, h) → r[3] == h
return ((lens_mm * average_head_height_mm * image_height_pixels) / (r[3] * sensor_h_mm)) - offest_mm

however, I’m having difficulty getting the “r” coordinates. This is how I’m recognizing the tennis ball

for obj1 in tf.classify(net1, img, min_scale=1.0, scale_mul=0.1, x_overlap=0.5, y_overlap=0.5):
predictions_list1 = list(zip(labels1, obj1.output()))

Could someone please help with drawing a rectangle around the tennis ball and getting the height? Thank you in advance

That example is specific for AprilTags.

The camera can’t really tell you height. However, you can create a mapping table from the pixel location of the ball to height. It’s not a direct measurement and is affected by lens curvature. But, otherwise, is fine.

So, if you want height you should take a few measurements of the pixel location and measured height of the ball… then plug that into excels linear regression calculator… then take that formulae and use it in your code.

Thank you, is there a sample code for that which I can take a look at? Im new to python. So basically for my project the openmv is connected to a car and the car follows a path but Im gonna put tennis balls on the path as obstacles and the car needs to avoid them, so I kinda need the car to know how far the ball is so that it can make the right movement at the right time. Since you have way more experience, do you have any suggestions for that? Thank you again

It’s not a python thing. Just in general, if you have a sensor you need to create a mapping scale between it’s measurements and what you want to do in the real world.

Question, is the camera pointed at the ball and you are trying to avoid them? If so, the camera is very bad at telling you distance. Height off the ground is far easier.

Yes the camera is pointed directly towards the ball and it keeps moving towards it, and at some point i want the car to turn after detecting that there is a tennis ball. Could you explain a little more on what you mean from “height off the ground” also the example that posted earlier was used to tell the distance from a face to the camera for your face detection example code

Ah,

Okay, so… this is just a trick to use the tennis ball size as a proxy for distance. Since the tennnis ball is of a fixed size it’s a fine assumption to believe that if it’s larger in the field of view then it’s closer.

So, if you are using find_blobs() to find the balls then just use the blob area attribute, and input that into a linear regression that returns the distance.

So, print out the ball size in the IDE serial terminal, measure the ball distance with a ruler, then put these x/y values into Excel and use the graph tool to draw a exponential regression on the data. Take that formula and put it into your code. This will then give you distance from pixel area size.

The formula will be valid as long the lens of the camera and the tennis ball size doesn’t change.

thank you for your help sir, so yes I just need an aprox distance so that the car doesn’t turn too soon or too late. No I’m not using the find_blo. I trained my own dataset of tennis balls and using tf.classify

“for obj1 in tf.classify(net1, img, min_scale=1.0, scale_mul=0.1, x_overlap=0.5, y_overlap=0.5):”

to do the classification. I just don’t know how to get the the width or height of the detected object in real time so that I could pass it into that function to get the approx distance from how close or far it is from the camera. could you please help me with that? it’d be much appreciated.

That will just tell you ball or no ball. It will not tell you anything else. You can’t get distance from that method.

Do you have any suggestions? Do you suggest using find_blob along with tf.classification?

Yes