Пример #1
0
def main():
    # Create a video capture object
    cap = cv.VideoCapture(path.join(SCRIPT_DIR, VIDEO_FILE))
    # Initialize previous frame map variable
    prev_frame = None
    global INIT_CENTER
    # Get the height and weight for the search area
    H_W, H_H = WIDTH // 2, HEIGHT // 2
    while cap.isOpened():
        # Get the approximate center point for the object
        [I_X, I_Y] = INIT_CENTER
        # Read the current frame
        ret, frame = cap.read()
        # If the reading the frame failed break out of the loop
        if not ret:
            print("Failed to read frame")
            break
        # If the user pressed 'q' break out of the loop
        if cv.waitKey(15) == ord("q"):
            print("Stopped")
            break
        # Strip out the object search area using the initial
        # points and the height and width values
        sub_frame = frame[I_Y - H_H:I_Y + H_H, I_X - H_W:I_X + H_W]
        # If there was no previous frame then create one
        # and move onto the next frame
        if prev_frame is None:
            prev_frame = color_map(sub_frame, I_X - H_W, I_Y - H_H)
            continue
        # If there was a previous frame calculate centers
        curr_frame_map = color_map(sub_frame, I_X - H_W, I_Y - H_H)
        c_center = calculate_center(curr_frame_map)
        shape = calculate_shape(curr_frame_map, prev_frame)
        s_center = calculate_center(shape)
        motion = calculate_tracking(curr_frame_map, prev_frame)
        m_center = calculate_center(motion)
        # Find center of the object using the weighted sum of
        # all the object's calculated centers.
        n_r = int((c_center[0] * C_W) + (m_center[0] * M_W) +
                  (s_center[0] * S_W))
        n_c = int((c_center[1] * C_W) + (m_center[1] * M_W) +
                  (s_center[1] * S_W))
        # Set the current frame to the previous frame for the next loop
        prev_frame = curr_frame_map
        # Update the initial center value
        INIT_CENTER = (n_r, n_c)
        # Add the target around the tracked object
        frame = add_target(frame, (n_r, n_c))
        # Show the frame with the target
        cv.imshow("Video", frame)
    cap.release()
    cv.destroyAllWindows()
Пример #2
0
    def test_longestlength(self):
        points = []
        angle = 45
        distance = 10
        radius = distance

        start = Point(float(5), float(5))
        end = nextvertex(start, distance, angle)

        center = calculate_center(start, end, radius, distance)

        print start
        print end
        print center

        points.append(start)
        points.append(end)
        line_points = []
        line_points.append(start)
        line_points.append(end)
        arcpoints_clock = list(arc_points(start, end, distance, radius, 20))
        arcpoints_anti = list(arc_points(start, end, distance, radius, 20, direction='anit'))
        pyplot.plot(*zip(*points), marker='o', color='r', ls='')
        pyplot.plot(*zip(*arcpoints_anti), marker='o', color='y', ls='')
        pyplot.plot(*zip(*arcpoints_clock), marker='o', color='b', ls='')
        pyplot.plot(*zip(*line_points))
        c2 = pyplot.Circle(center, radius, color='0.75')
        fig = pyplot.gcf()
        fig.gca().add_artist(c2)
        pyplot.axis([0, 30, 0, 30])
        pyplot.show()
Пример #3
0
    def test_longestlength(self):
        points = []
        angle = 45
        distance = 10
        radius = distance

        start = Point(float(5), float(5))
        end = nextvertex(start, distance, angle)

        center = calculate_center(start, end, radius, distance)

        print start
        print end
        print center

        points.append(start)
        points.append(end)
        line_points = []
        line_points.append(start)
        line_points.append(end)
        arcpoints_clock = list(arc_points(start, end, distance, radius, 20))
        arcpoints_anti = list(
            arc_points(start, end, distance, radius, 20, direction='anit'))
        pyplot.plot(*zip(*points), marker='o', color='r', ls='')
        pyplot.plot(*zip(*arcpoints_anti), marker='o', color='y', ls='')
        pyplot.plot(*zip(*arcpoints_clock), marker='o', color='b', ls='')
        pyplot.plot(*zip(*line_points))
        c2 = pyplot.Circle(center, radius, color='0.75')
        fig = pyplot.gcf()
        fig.gca().add_artist(c2)
        pyplot.axis([0, 30, 0, 30])
        pyplot.show()
Пример #4
0
    def draw_final_image(self, image, warped, undist, ploty, left_fitx,
                         right_fitx, Minv, left_rad, right_rad):
        """
        Draw the different outputs into the original image using the params passed
        The image is modified by adding a polygon that fits between the lanes and information about the curvature and
        where the center of the lane is
        :param image: the original image
        :param warped:
        :param undist:
        :param ploty:
        :param left_fitx:
        :param right_fitx:
        :param Minv:
        :param left_rad:
        :param right_rad:
        :return:
        """
        gray = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
        # Create an image to draw the lines on
        warp_zero = np.zeros_like(gray).astype(np.uint8)
        color_warp = np.dstack((warp_zero, warp_zero, warp_zero))

        # Recast the x and y points into usable format for cv2.fillPoly()
        pts_left = np.array([np.transpose(np.vstack([left_fitx, ploty]))])
        pts_right = np.array(
            [np.flipud(np.transpose(np.vstack([right_fitx, ploty])))])
        pts = np.hstack((pts_left, pts_right))

        # Draw the lane onto the warped blank image
        cv2.fillPoly(color_warp, np.int_([pts]), (0, 255, 0))

        # Warp the blank back to original image space using inverse perspective matrix (Minv)
        newwarp = cv2.warpPerspective(color_warp, Minv,
                                      (image.shape[1], image.shape[0]))
        # Combine the result with the original image
        result = cv2.addWeighted(undist, 1, newwarp, 0.3, 0)

        font = cv2.FONT_HERSHEY_SIMPLEX
        off_center = calculate_center(left_fitx, right_fitx, image.shape)
        direction_str = 'left' if off_center < 0 else 'right'
        center_str = '{:.2f} m of center {}'.format(abs(off_center),
                                                    direction_str)
        cv2.putText(result, center_str, (430, 630), font, 1, (0, 0, 255), 2,
                    cv2.LINE_AA)
        if left_rad and right_rad:
            curvature = 0.5 * (round(right_rad / 1000, 1) +
                               round(left_rad / 1000, 1))
        else:
            curvature = 0
        str2 = 'Radius of curvature: {} km'.format(curvature)
        cv2.putText(result, str2, (430, 670), font, 1, (0, 0, 255), 2,
                    cv2.LINE_AA)

        if self.args.is_test:
            plt.imshow(result)
            plt.show()

        return result
Пример #5
0
def list(request, template='maps/locations.html'):

    data = {}
    locations_obj = request.user.location_set.all()

    data['locations'] = locations_obj
    data['username'] = request.user.username
    data['center'] = calculate_center(locations_obj)

    return render_to_response(template,
                              data,
                              context_instance=RequestContext(request))
Пример #6
0
def list(request, template='maps/locations.html'):
    
    data = {}
    locations_obj = request.user.location_set.all()
    
    data['locations'] = locations_obj
    data['username'] = request.user.username
    data['center'] = calculate_center(locations_obj)
    
    return render_to_response(template,
                  data,
                  context_instance=RequestContext(request)
                  )
Пример #7
0
    img_sum_av = []

    cv2.imshow('frame2', src)

    if counter - 8 is 0:
        print("a")
        for i in range(1, 3):
            crop_y_start = int(((i - 1) / 2) * size_y)
            crop_y_end = int((i / 2) * size_y)
            sliced_img = src_processed[crop_y_start:crop_y_end, 0:size_x]
            crop_img.append(sliced_img)
            img_sum_av.append(
                sig.savgol_filter(np.sum(sliced_img, axis=0), 101, 3))
            # img_sum_av.append(np.sum(sliced_img, axis=0))

        center_upper = calculate_center(img_sum_av[0])
        center_lower = calculate_center(img_sum_av[1])

        direction_upper, direction_lower = calculate_direction(
            [center_upper, center_lower], img_sum_av[0].size)

        mark_value = []

        plot.figure()
        plot.subplot(3, 1, 1)
        plot.title("threshold = {}".format(lowThreshold))
        plot.imshow(src_processed)
        plot.subplot(3, 1, 2)
        plot.title("Direction = {}".format(direction_upper))
        plot.plot(img_sum_av[0], "-D", markevery=[center_upper])
        plot.subplot(3, 1, 3)