def wrapped(image):
    frameWidth = 640
    frameHeight = 480
    c_img = utlis.undistort(img)
    imgThres, imgCanny, imgColor = utlis.thresholding(c_img)
    imgWarp = utlis.perspective_warp(imgColor,
                                     dst_size=(frameWidth, frameHeight),
                                     src=valTrackbars())
    return imgWarp
예제 #2
0
def getLaneCurve(img,display=2):
 
    imgCopy = img.copy()
    imgResult = img.copy()
    #### STEP 1
    imgThres = utlis.thresholding(img)
 
    #### STEP 2
    hT, wT, c = img.shape
    points = utlis.valTrackbars()
    imgWarp = utlis.warpImg(imgThres,points,wT,hT)
    imgWarpPoints = utlis.drawPoints(imgCopy,points)
 
    #### STEP 3
    middlePoint,imgHist = utlis.getHistogram(imgWarp,display=True,minPer=0.5,region=4)
    curveAveragePoint, imgHist = utlis.getHistogram(imgWarp, display=True, minPer=0.9)
    curveRaw = curveAveragePoint - middlePoint
 
    #### SETP 4
    curveList.append(curveRaw)
    if len(curveList)>avgVal:
        curveList.pop(0)
    curve = int(sum(curveList)/len(curveList))
 
    #### STEP 5
    if display != 0:
        imgInvWarp = utlis.warpImg(imgWarp, points, wT, hT, inv=True)
        imgInvWarp = cv2.cvtColor(imgInvWarp, cv2.COLOR_GRAY2BGR)
        imgInvWarp[0:hT // 3, 0:wT] = 0, 0, 0
        imgLaneColor = np.zeros_like(img)
        imgLaneColor[:] = 0, 255, 0
        imgLaneColor = cv2.bitwise_and(imgInvWarp, imgLaneColor)
        imgResult = cv2.addWeighted(imgResult, 1, imgLaneColor, 1, 0)
        midY = 450
        cv2.putText(imgResult, str(curve), (wT // 2 - 80, 85), cv2.FONT_HERSHEY_COMPLEX, 2, (255, 0, 255), 3)
        cv2.line(imgResult, (wT // 2, midY), (wT // 2 + (curve * 3), midY), (255, 0, 255), 5)
        cv2.line(imgResult, ((wT // 2 + (curve * 3)), midY - 25), (wT // 2 + (curve * 3), midY + 25), (0, 255, 0), 5)
        for x in range(-30, 30):
            w = wT // 20
            cv2.line(imgResult, (w * x + int(curve // 50), midY - 10),
                     (w * x + int(curve // 50), midY + 10), (0, 0, 255), 2)
        #fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);
        #cv2.putText(imgResult, 'FPS ' + str(int(fps)), (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (230, 50, 50), 3);
    if display == 2:
        imgStacked = utlis.stackImages(0.7, ([img, imgWarpPoints, imgWarp],
                                             [imgHist, imgLaneColor, imgResult]))
        cv2.imshow('ImageStack', imgStacked)
        cv2.imshow('Warp Points', imgWarpPoints)
    elif display == 1:
        cv2.imshow('Resutlt', imgResult)
 
    #### NORMALIZATION
    curve = curve/100
    if curve>1: curve ==1
    if curve<-1:curve == -1
 
    return curve
def lane_detect(img):
    frameWidth = w
    frameHeight = h
    intialTracbarVals = (38, 60, 12, 90)
    count = 0
    noOfArrayValues = 11
    global arrayCurve, arrayCounter
    arrayCounter = 0
    arrayCurve = np.zeros([noOfArrayValues])
    myVals = []
    #utlis.initializeTrackbars(intialTracbarVals)

    # imgWarpPoints = img.copy()
    imgFinal = img.copy()
    imgCanny = img.copy()
    try:

        # imgUndis = utlis.undistort(img)
        imgThres, imgCanny, imgColor = utlis.thresholding(img)
        src = utlis.valTrackbars()
        imgWarp = utlis.perspective_warp(imgThres,
                                         dst_size=(frameWidth, frameHeight),
                                         src=src)
        # imgWarpPoints = utlis.drawPoints(imgWarpPoints, src)
        imgSliding, curves, lanes, ploty = utlis.sliding_window(
            imgWarp, draw_windows=False)

        curverad = utlis.get_curve(imgFinal, curves[0], curves[1])
        lane_curve = np.mean([curverad[0], curverad[1]])
        imgFinal = utlis.draw_lanes(img,
                                    curves[0],
                                    curves[1],
                                    frameWidth,
                                    frameHeight,
                                    src=src)
        #currentCurve = lane_curve // 50
        # if  int(np.sum(arrayCurve)) == 0:averageCurve = currentCurve
        # else:
        #     averageCurve = np.sum(arrayCurve) // arrayCurve.shape[0]
        # if abs(averageCurve-currentCurve) >200: arrayCurve[arrayCounter] = averageCurve
        # else :arrayCurve[arrayCounter] = currentCurve
        # arrayCounter +=1
        # if arrayCounter >=noOfArrayValues : arrayCounter=0
        # cv2.putText(imgFinal, str(int(averageCurve)), (frameWidth//2-70, 70), cv2.FONT_HERSHEY_DUPLEX, 1.75, (0, 0, 255), 2, cv2.LINE_AA)
        # imgFinal= utlis.drawLines(imgFinal,lane_curve)
    except:
        # print("NO Lane Detected")
        imgFinal = img
        cv2.putText(imgFinal, "No Lane Detected", (10, 20),
                    cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 255), 1, cv2.LINE_AA)

    return imgFinal
def getLaneCurve(img):

    imgCopy = img.copy()

    #Step1
    imgThres = utlis.thresholding(img)

    #Step2
    h,w,c = img.shape
    points = utlis.valTrackbars()
    imgWarp = utlis.warpImg(img,points,w,h)
    imgWarpPoints  = utlis.drawPoints(imgCopy,points)

    cv2.imshow("Thres", imgThres)
    cv2.imshow("Warp", imgWarp)
    cv2.imshow("Warp Points", imgWarpPoints)

    return None
arrayCurve = np.zeros([noOfArrayValues])
myVals=[]
utlis.initializeTrackbars(intialTracbarVals)


while True:

    success, img = cap.read()
    #img = cv2.imread('test3.jpg')
    if cameraFeed== False:img = cv2.resize(img, (frameWidth, frameHeight), None)
    imgWarpPoints = img.copy()
    imgFinal = img.copy()
    imgCanny = img.copy()

    imgUndis = utlis.undistort(img)
    imgThres,imgCanny,imgColor = utlis.thresholding(imgUndis)
    src = utlis.valTrackbars()
    imgWarp = utlis.perspective_warp(imgThres, dst_size=(frameWidth, frameHeight), src=src)
    imgWarpPoints = utlis.drawPoints(imgWarpPoints, src)
    imgSliding, curves, lanes, ploty = utlis.sliding_window(imgWarp, draw_windows=True)

    try:
        curverad =utlis.get_curve(imgFinal, curves[0], curves[1])
        lane_curve = np.mean([curverad[0], curverad[1]])
        imgFinal = utlis.draw_lanes(img, curves[0], curves[1],frameWidth,frameHeight,src=src)

        # ## Average
        currentCurve = lane_curve // 50
        if  int(np.sum(arrayCurve)) == 0:averageCurve = currentCurve
        else:
            averageCurve = np.sum(arrayCurve) // arrayCurve.shape[0]
예제 #6
0
def getLaneCurve(img, display=2):

    imgCopy = img.copy()
    imgResult = img.copy()

    #### mask the image and get the white A4 paper
    imgThres = utlis.thresholding(img)

    #### Wrap the image to get Bird's-eye view
    hT, wT, c = img.shape
    points = [[128, 147], [352, 147], [49, 240], [431, 240]]
    imgWarp = utlis.warpImg(imgThres, points, wT, hT)

    # draw the points
    if display != 0:
        imgWarpPoints = utlis.drawPoints(imgCopy, points)

    #### get the middle point to calculate the curve
    middlePoint, imgHist = utlis.getHistogram(imgWarp,
                                              display=True,
                                              minPer=0.8,
                                              region=4)
    curveAveragePoint, imgHist = utlis.getHistogram(imgWarp,
                                                    display=True,
                                                    minPer=0.9)
    curveRaw = curveAveragePoint - middlePoint

    # average the last'10' curve
    curveList.append(curveRaw)
    if len(curveList) > avgVal:
        curveList.pop(0)
    curve = int(sum(curveList) / len(curveList))

    # displays
    if display != 0:
        imgInvWarp = utlis.warpImg(imgWarp, points, wT, hT, inv=True)
        imgInvWarp = cv2.cvtColor(imgInvWarp, cv2.COLOR_GRAY2BGR)
        imgInvWarp[0:hT // 3, 0:wT] = 0, 0, 0
        imgLaneColor = np.zeros_like(img)
        imgLaneColor[:] = 0, 255, 0
        imgLaneColor = cv2.bitwise_and(imgInvWarp, imgLaneColor)
        imgResult = cv2.addWeighted(imgResult, 1, imgLaneColor, 1, 0)
        midY = 450
        cv2.putText(imgResult, str(curve), (wT // 2 - 80, 85),
                    cv2.FONT_HERSHEY_COMPLEX, 2, (255, 0, 255), 3)
        cv2.line(imgResult, (wT // 2, midY), (wT // 2 + (curve * 3), midY),
                 (255, 0, 255), 5)
        cv2.line(imgResult, ((wT // 2 + (curve * 3)), midY - 25),
                 (wT // 2 + (curve * 3), midY + 25), (0, 255, 0), 5)
        for x in range(-30, 30):
            w = wT // 20
            cv2.line(imgResult, (w * x + int(curve // 50), midY - 10),
                     (w * x + int(curve // 50), midY + 10), (0, 0, 255), 2)
        # fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer)
        # cv2.putText(imgResult, 'FPS ' + str(int(fps)), (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (230, 50, 50), 3)
    if display == 2:
        imgStacked = utlis.stackImages(0.7,
                                       ([img, imgWarpPoints, imgWarp],
                                        [imgHist, imgLaneColor, imgResult]))
        cv2.imshow('ImageStack', imgStacked)
        cv2.waitKey(1)
    elif display == 1:
        cv2.imshow('Resutlt', imgResult)
        cv2.waitKey(1)

    # NORMALIZATION
    curve = curve / 100
    if curve > 1: curve == 1
    if curve < -1: curve == -1

    return curve