Skip to content

Instantly share code, notes, and snippets.

@informramiz
Created September 24, 2019 14:02
Show Gist options
  • Select an option

  • Save informramiz/cb1844787678f3ef3c7e1a5341cdf51c to your computer and use it in GitHub Desktop.

Select an option

Save informramiz/cb1844787678f3ef3c7e1a5341cdf51c to your computer and use it in GitHub Desktop.
Use class to keep track of average lane lines
class Lane():
def __init__(self):
#everything starting with `self` keyword will be consider property/data memeber/field of this class
#here we declare all the fields/variables of this class that we need to keep track of data
#flag to represent whether first polynomial fitting has been done
#and now that data can be used for future
self.is_ready = False
#fitting done on current frame
self.current_fit = None
#fitting in meters per pixel
self.current_fit_m = None
#current curvature
self.current_curvature = None
#all curvatures
self.all_curvatures = []
#all fits
self.all_fits = []
#all fits_m
self.all_fits_m = []
#Iterations count to take avg
self.N = 10
def add_fit(self, fit):
self.current_fit = None
self.all_fits.append(fit)
self.is_ready = True
def avg_fit(self):
n_fits = self.all_fits[-self.N:]
if(len(self.all_fits_m) > self.N):
self.all_fits = n_fits
return np.mean(np.array(n_fits), axis=0)
#--------------Below code is outside of class. Remember, the indentation of this code is not under class but at the same
# level as class
#declaring Lane objects for left_lane and right lane
left_lane = Lane()
right_lane = Lane()
#for image : images/video
#left_fit = fit the left line
#left_lane.add_fit(left_fit)
#right_fit = fit the right line
#right_lane.add_fit(right_fit)
#now use average fit to draw on image, something like this
#drawn_img(undistorted_img, warped_binary, left_lane.avg_fit(), right_lane.avg_fit())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment