Object track and print position

Hi everyone,

I want to track a red object (its 2 inches square roughly)

All I need is to recognize it and print the x,y coordinates to serial console

Once I do this I can prove my prototype will work and hopefully get into learning the code.any chance someone can help with a simple script while I’m learning? Would be great to prove this works before spending a month learning :slight_smile:

See the color tracking example scripts.

Yes, I have played with the threshold, removed tracking anything but red, removed the print frames per second,

Still struggling with just printing x,y?

Feel like I’m missing something really obvious

Print(blob.cx(), blob.cy())

Thanks, I did try that earlier but over indented ever so slightly and kept getting errors, hence the handle, lol

Here is my working code, it works very well for testing right now, does anyone have any suggestions how I can make it better/faster? Like I said, working great, interested in hearing how I could improve it.

import sensor, image, time, math

threshold_index = 0 # 0 for red, 1 for green, 2 for blue

# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general red/green/blue things. You may wish to tune them...
thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds
              (30, 100, -64, -8, -32, 32), # generic_green_thresholds
              (0, 30, 0, 64, -128, 0)] # generic_blue_thresholds

sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()

# Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
# returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
# camera resolution. "merge=True" merges all overlapping blobs in the image.

    img = sensor.snapshot()
    for blob in img.find_blobs([thresholds[threshold_index]], pixels_threshold=200, area_threshold=200, merge=True):
        # These values depend on the blob not being circular - otherwise they will be shaky.
        if blob.elongation() > 0.5:
            img.draw_edges(blob.min_corners(), color=(255,0,0))

        # These values are stable all the time.
        img.draw_cross(blob.cx(), blob.cy())
        img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)

        x = blob.cx()
        y = blob.cy()

        #Copy of Arduino map function
        def map(x, in_min, in_max, out_min, out_max):
            return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
        xAxis= map(x,0,300, -15, 15)
        yAxis= map(y,0,240,15,-15)
        print(xAxis, ",", yAxis)

What do you want to improve?

Sorry, after more testing it seems the code is plenty fast enough, and I can learn to lean to improve the code as I go.

However, I do need to display the output over serial, can anyone give me a nudge in the right direction? Values will be in the neighborhood of -15 to +15

Preferably I could go straight from the mv to serial, but I dont think thats possible.

Next I would use an arduino nano or uno. But can someone suggest if I should be looking at uart or spi?

I’d like to play around with this some more and figure out, but would appreciate if someone could narrow my research down.

Thanks again, this forum has so much information!

Please read the documentation… it can do all this. See the pyb module.

Ok, so I’ve read the pyb module, tried to modify the code above and have failed miserably. I’ve tried the pixy emulation.

I did find a code that allowed me to go through a arduino mega and then read the serial in unity, but that seems like a waste of hardware.

I know this camera is capable, I just can’t seem to get the code above to display in serial

If you want to print the data to a PC: GitHub - openmv/openmv: OpenMV Camera Module

If you want to print to a UART see the example under Examples → Board Control → Uart.