ルーチン説明 35-100_fps_ir_led_tracking 100_fps_ir_led_tracking 100 フレーム IR 赤外線 LED トラッキング
import sensor, image, time
EXPOSURE_MICROSECONDS = 1000
TRACKING_THRESHOLDS = [(128, 255)]
SEARCHING_RESOLUTION = sensor.VGA
SEARCHING_AREA_THRESHOLD = 16
SEARCHING_PIXEL_THRESHOLD = SEARCHING_AREA_THRESHOLD
TRACKING_RESOLUTION = sensor.QQVGA
TRACKING_AREA_THRESHOLD = 256
TRACKING_PIXEL_THRESHOLD = TRACKING_AREA_THRESHOLD
TRACKING_EDGE_TOLERANCE = 0.05
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(SEARCHING_RESOLUTION)
sensor.skip_frames(time = 1000)
clock = time.clock()
sensor.set_auto_gain(False)
sensor.set_auto_exposure(False, exposure_us=EXPOSURE_MICROSECONDS)
sensor.skip_frames(time = 1000)
x, y, sensor_w, sensor_h = sensor.ioctl(sensor.IOCTL_GET_READOUT_WINDOW)
while(True):
clock.tick()
img = sensor.snapshot()
blobs = img.find_blobs(TRACKING_THRESHOLDS,
area_threshold=SEARCHING_AREA_THRESHOLD,
pixels_threshold=SEARCHING_PIXEL_THRESHOLD)
if len(blobs):
most_dense_blob = max(blobs, key = lambda x: x.density())
img.draw_rectangle(most_dense_blob.rect())
def get_mapped_centroid(b):
x, y, w, h = sensor.ioctl(sensor.IOCTL_GET_READOUT_WINDOW)
ratio = min(w / float(sensor.width()), h / float(sensor.height()))
mapped_cx = (b.cx() - (sensor.width() / 2.0)) * ratio
mapped_cx += (w - (sensor.width() * ratio)) / 2.0
mapped_cx += x + (sensor_w / 2.0)
mapped_cy = (b.cy() - (sensor.height() / 2.0)) * ratio
mapped_cy += (h - (sensor.height() * ratio)) / 2.0
mapped_cy += y + (sensor_h / 2.0)
return (mapped_cx, mapped_cy)
def center_on_blob(b, res):
mapped_cx, mapped_cy = get_mapped_centroid(b)
sensor.set_framesize(res)
x = int(mapped_cx - (sensor_w / 2.0))
y = int(mapped_cy - (sensor_h / 2.0))
w = sensor.width()
h = sensor.height()
sensor.ioctl(sensor.IOCTL_SET_READOUT_WINDOW, (x, y, w, h))
new_x, new_y, w, h = sensor.ioctl(sensor.IOCTL_GET_READOUT_WINDOW)
x_error = x - new_x
y_error = y - new_y
if x_error < 0: print("-X Limit Reached ", end="")
if x_error > 0: print("+X Limit Reached ", end="")
if y_error < 0: print("-Y Limit Reached ", end="")
if y_error > 0: print("+Y Limit Reached ", end="")
center_on_blob(most_dense_blob, TRACKING_RESOLUTION)
while(True):
clock.tick()
img = sensor.snapshot()
blobs = img.find_blobs(TRACKING_THRESHOLDS,
area_threshold=TRACKING_AREA_THRESHOLD,
pixels_threshold=TRACKING_PIXEL_THRESHOLD)
if not len(blobs):
sensor.set_framesize(SEARCHING_RESOLUTION)
sensor.ioctl(sensor.IOCTL_SET_READOUT_WINDOW, (sensor_w, sensor_h))
break
most_dense_blob = max(blobs, key = lambda x: x.density())
img.draw_rectangle(most_dense_blob.rect())
print(clock.fps(), "BLOB cx:%d, cy:%d" % get_mapped_centroid(most_dense_blob))
x_diff = most_dense_blob.cx() - (sensor.width() / 2.0)
y_diff = most_dense_blob.cy() - (sensor.height() / 2.0)
w_threshold = (sensor.width() / 2.0) * TRACKING_EDGE_TOLERANCE
h_threshold = (sensor.height() / 2.0) * TRACKING_EDGE_TOLERANCE
if abs(x_diff) > w_threshold or abs(y_diff) > h_threshold:
center_on_blob(most_dense_blob, TRACKING_RESOLUTION)
print(clock.fps())