fork download
  1. # Single Color Code Tracking Example
  2. #
  3. # This example shows off single color code tracking using the OpenMV Cam.
  4. #
  5. # A color code is a blob composed of two or more colors. The example below will
  6. # only track colored objects which have both the colors below in them.
  7.  
  8. import sensor, image, time, math
  9.  
  10. # Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
  11. # The below thresholds track in general red/green things. You may wish to tune them...
  12. thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds -> index is 0 so code == (1 << 0)
  13. (30, 100, -64, -8, -32, 32)] # generic_green_thresholds -> index is 1 so code == (1 << 1)
  14. # Codes are or'ed together when "merge=True" for "find_blobs".
  15.  
  16. sensor.reset()
  17. sensor.set_pixformat(sensor.RGB565)
  18. sensor.set_framesize(sensor.QVGA)
  19. sensor.skip_frames(time = 2000)
  20. sensor.set_auto_gain(False) # must be turned off for color tracking
  21. sensor.set_auto_whitebal(False) # must be turned off for color tracking
  22. clock = time.clock()
  23.  
  24. # Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
  25. # returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
  26. # camera resolution. "merge=True" must be set to merge overlapping color blobs for color codes.
  27.  
  28. while(True):
  29. clock.tick()
  30. img = sensor.snapshot()
  31. for blob in img.find_blobs(thresholds, pixels_threshold=100, area_threshold=100, merge=True):
  32. if blob.code() == 3: # r/g code == (1 << 1) | (1 << 0)
  33. # These values depend on the blob not being circular - otherwise they will be shaky.
  34. if blob.elongation() > 0.5:
  35. img.draw_edges(blob.min_corners(), color=(255,0,0))
  36. img.draw_line(blob.major_axis_line(), color=(0,255,0))
  37. img.draw_line(blob.minor_axis_line(), color=(0,0,255))
  38. # These values are stable all the time.
  39. img.draw_rectangle(blob.rect())
  40. img.draw_cross(blob.cx(), blob.cy())
  41. # Note - the blob rotation is unique to 0-180 only.
  42. img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)
  43. print(clock.fps())
  44.  
Runtime error #stdin #stdout #stderr 0.04s 63612KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 8, in <module>
    import sensor, image, time, math
ImportError: No module named sensor