Ejemplo n.º 1
0
def pause(duration=1.0, verbose=True):
    """
    Pause the car's code for `duration` seconds.
    """
    if verbose:
        print("Pausing for {} seconds.".format(duration))
    time.sleep(duration)
Ejemplo n.º 2
0
def object_location(object_list, frame_shape, verbose=True):
    """
    Calculate the location of the largest object in `object_list`.

    Returns one of: 'frame_left', 'frame_right', 'frame_center', None
    """
    if not object_list:
        if verbose:
            print("Object location is None.")
        return None
    import numpy as np
    areas = [w * h for x, y, w, h in object_list]
    i = np.argmax(areas)
    nearest = object_list[i]
    x, y, w, h = nearest
    x_center = x + w / 2.
    if x_center < frame_shape[1] / 3.:
        location = 'frame_left'
    elif x_center < 2 * frame_shape[1] / 3.:
        location = 'frame_center'
    else:
        location = 'frame_right'
    if verbose:
        print("Object location is '{}'.".format(location))
    return location
Ejemplo n.º 3
0
def object_size(object_list, frame_shape, verbose=True):
    """
    Calculate the ratio of the nearest object's area to the frame's area.
    """
    if not object_list:
        if verbose:
            print("Object area is 0.")
        return 0.0
    areas = [w * h for x, y, w, h in object_list]
    ratio = max(areas) / (frame_shape[0] * frame_shape[1])
    if verbose:
        print("Object area is {}.".format(ratio))
    return ratio
Ejemplo n.º 4
0
def capture(num_frames=1, verbose=True):
    """
    Capture `num_frames` frames from the car's camera and return
    them as a numpy ndarray.
    """
    MAX_FRAMES = 4
    if num_frames > MAX_FRAMES:
        print(
            f"Warning: You may capture at most {MAX_FRAMES} frames with this function."
        )
        num_frames = MAX_FRAMES
    from auto import camera
    return camera.capture(num_frames, verbose)
Ejemplo n.º 5
0
def right(duration=1.0, verbose=True):
    """
    Drive the car forward and right for `duration` seconds.
    """
    from car import motors
    if duration > 5.0:
        print(
            "Error: The duration exceeds 5 seconds; will reset to 5 seconds.")
        duration = 5.0
    if verbose:
        print("Driving right for {} seconds.".format(duration))
    if duration <= 0.0:
        return
    motors.drive(-45.0, motors.CAR_THROTTLE_FORWARD_SAFE_SPEED, duration)
Ejemplo n.º 6
0
def left(duration=1.0, verbose=True):
    """
    Drive the car forward and left for `duration` seconds.
    """
    from car import motors
    if duration > 5.0:
        print(
            "Error: The duration exceeds 5 seconds; will reset to 5 seconds.")
        duration = 5.0
    if verbose:
        print("Driving left for {} seconds.".format(duration))
    if duration <= 0.0:
        return
    motors.left(duration)
Ejemplo n.º 7
0
def reverse(duration=1.0, verbose=True):
    """
    Drive the car in reverse for `duration` seconds.
    """
    from car import motors
    if duration > 5.0:
        print(
            "Error: The duration exceeds 5 seconds; will reset to 5 seconds.")
        duration = 5.0
    if verbose:
        print("Driving in reverse for {} seconds.".format(duration))
    if duration <= 0.0:
        return
    motors.straight(motors.CAR_THROTTLE_REVERSE_SAFE_SPEED,
                    duration,
                    invert_output=True)
Ejemplo n.º 8
0
def classify_color(frame, annotate=True, verbose=True):
    """
    Classify the center region of `frame` as having either primarily "red",
    "yellow", or "green, or none of those ("background").

    The `frame` parameter must be a numpy array containing an RGB image.

    Returns a string representing the color found in the center of the
    image, one of "red", "yellow", "green", or "background".
    """
    global COLORCLASSIFIER
    try:
        COLORCLASSIFIER
    except NameError:
        from auto.models import ColorClassifier
        COLORCLASSIFIER = ColorClassifier()
        if verbose:
            print("Instantiated a ColorClassifier object!")

    p1, p2, classific = COLORCLASSIFIER.classify(frame, annotate=annotate)
    if verbose:
        print("Classified color as '{}'.".format(classific))
    return classific
Ejemplo n.º 9
0
def detect_pedestrians(frame, annotate=True, verbose=True):
    """
    Detect pedestrians inside of `frame`, and annotate each pedestrian.

    The `frame` parameter must be an image as a numpy array either containing
    3-channel RGB values _or_ 1-channel gray values.

    Returns a list of rectangles, where each rectangle is a 4-tuple of:
        (x, y, width, height)
    """
    global PEDESTRIANDETECTOR
    try:
        PEDESTRIANDETECTOR
    except NameError:
        from auto.models import PedestrianDetector
        PEDESTRIANDETECTOR = PedestrianDetector()
        if verbose:
            print("Instantiated a PedestrianDetector object!")

    rects = PEDESTRIANDETECTOR.detect(frame, annotate=annotate)
    n = len(rects)
    if verbose:
        print("Found {} pedestrian{}.".format(n, 's' if n != 1 else ''))
    return rects
Ejemplo n.º 10
0
def detect_faces(frame, annotate=True, verbose=True):
    """
    Detect faces inside of `frame`, and annotate each face.

    The `frame` parameter must be an image as a numpy array either containing
    3-channel RGB values _or_ 1-channel gray values.

    Returns a list of rectangles, where each rectangle is a 4-tuple of:
        (x, y, width, height)
    """
    global FACEDETECTOR
    try:
        FACEDETECTOR
    except NameError:
        from auto.models import FaceDetector
        FACEDETECTOR = FaceDetector()
        if verbose:
            print("Instantiated a FaceDetector object!")

    faces = FACEDETECTOR.detect(frame, annotate=annotate)
    n = len(faces)
    if verbose:
        print("Found {} face{}.".format(n, 's' if n != 1 else ''))
    return faces