def fit_polynomial(self):
    ploty = self.ploty
    fit = np.polyfit(self.ally, self.allx, 2)
    self.current_fit = fit
    if (len(self.recent_fits) == 0):
        self.diffs = [0, 0, 0]
    else:
        new = np.array(self.current_fit)
        old = np.array(self.recent_fits[(- 1)])
        self.diffs = (new - old)
    if (len(self.recent_fits) < self.n):
        self.recent_fits.append(self.current_fit)
    elif (len(self.recent_fits) == self.n):
        self.recent_fits.pop(0)
        self.recent_fits.append(self.current_fit)
    else:
        self.recent_fits.append(self.current_fit)
        self.recent_fits = self.recent_fits[(- self.n):]
    self.average_fit = np.array(self.recent_fits).mean(axis=0)
    try:
        x_fitted = (((self.average_fit[0] * (ploty ** 2)) + (self.average_fit[1] * ploty)) + self.average_fit[2])
    except TypeError:
        print('The function failed to fit a line!')
        x_fitted = ((1 * (ploty ** 2)) + (1 * ploty))
        self.detected = False
    else:
        self.detected = True
    self.current_fitted_x = x_fitted
    if (len(self.recent_fitted_xs) < self.n):
        self.recent_fitted_xs.append(self.current_fitted_x)
    elif (len(self.recent_fitted_xs) == self.n):
        self.recent_fitted_xs.pop(0)
        self.recent_fitted_xs.append(self.current_fitted_x)
    else:
        self.recent_fitted_xs.append(self.current_fitted_x)
        self.recent_fitted_xs = self.recent_fitted_xs[(- self.n):]
    self.average_fitted_x = np.array(self.recent_fitted_xs).mean(axis=0)
    return self.average_fitted_x
def fit_polynomial(self):
    ploty = self.ploty
    fit = np.polyfit(self.ally, self.allx, 2)
    self.current_fit = fit
    if (len(self.recent_fits) == 0):
        self.diffs = [0, 0, 0]
    else:
        new = np.array(self.current_fit)
        old = np.array(self.recent_fits[(- 1)])
        self.diffs = (new - old)
    if (len(self.recent_fits) < self.n):
        self.recent_fits.append(self.current_fit)
    elif (len(self.recent_fits) == self.n):
        self.recent_fits.pop(0)
        self.recent_fits.append(self.current_fit)
    else:
        self.recent_fits.append(self.current_fit)
        self.recent_fits = self.recent_fits[(- self.n):]
    self.average_fit = np.array(self.recent_fits).mean(axis=0)
    try:
        x_fitted = (((self.average_fit[0] * (ploty ** 2)) + (self.average_fit[1] * ploty)) + self.average_fit[2])
    except TypeError:
        print('The function failed to fit a line!')
        x_fitted = ((1 * (ploty ** 2)) + (1 * ploty))
        self.detected = False
    else:
        self.detected = True
    self.current_fitted_x = x_fitted
    if (len(self.recent_fitted_xs) < self.n):
        self.recent_fitted_xs.append(self.current_fitted_x)
    elif (len(self.recent_fitted_xs) == self.n):
        self.recent_fitted_xs.pop(0)
        self.recent_fitted_xs.append(self.current_fitted_x)
    else:
        self.recent_fitted_xs.append(self.current_fitted_x)
        self.recent_fitted_xs = self.recent_fitted_xs[(- self.n):]
    self.average_fitted_x = np.array(self.recent_fitted_xs).mean(axis=0)
    return self.average_fitted_x