Esempio n. 1
0
def processPathList(carState, typeBasedList):
    for movingCommand in typeBasedList:
        movingDir, relativeDir, dirCount, pathLength = movingCommand
        motorControl.turn(Directions.RELATIVE_VALUE[(carState[1], movingDir)] * 90)
        carState[1]=movingDir
        #TODO: add auto correction function to handle frontSensor[0]!=backSensor[0]
        #TODO: update carState[0]

        #relativeDir+1: if check left, -1+1=0, if check right, 1+1=2
        initialSideValue=sensor.getSensorReading()[relativeDir+1]
        if initialSideValue==0:
            wallCheckingState=wallCheckingStates.WallCheckingStates_AllNonWall(1)
        else:
            wallCheckingState=wallCheckingStates.WallCheckingStates_AllWall(0)
        motorControl.fastGoForward(pathLength)
        while wallCheckingState.dirCount < dirCount:
            frontSensors = sensor.getSensorReading()
            backSensors = sensor.getBackSensorReadings()
            if relativeDir == -1:
                frontValue=frontSensors[0]
                backValue=backSensors[0]
            else:
                frontValue=frontSensors[3]
                backValue=backSensors[2]
            wallCheckingState=wallCheckingState.nextState(frontValue,backValue)
        #Enough dirCount. Stop car
        motorControl.stop()
Esempio n. 2
0
def pan(frame, x, y, w, h):
    #note: frame is 640 x 480
    cx = x + int(w / 2)  #center x of face
    cy = y + int(h / 2)  #center y of face
    speedS = 0.5
    speedL = 0.75
    if (cx > 200 and cx < 480):
        if (cx < 300):
            # print('left s')
            motor.left(speedS)
        elif (cx > 380):
            # print('right s')
            motor.right(speedS)
        else:
            motor.stop()
            return True
    else:
        if (cx < 200):
            # print('left L')
            motor.left(speedL)
        elif (cx > 480):
            # print('right L')
            motor.right(speedL)

    return False
Esempio n. 3
0
def main():
    face_cascade = cv2.CascadeClassifier(
        'cascades/data/haarcascade_frontalface_alt2.xml')
    # profileface_cascade = cv2.CascadeClassifier('cascades/data/haarcascade_profileface.xml')
    test_cascade = cv2.CascadeClassifier('cascades/data/aGest.xml')
    cap = cv2.VideoCapture(0)
    cap.set(3, 640)  #adjusts width of video stream
    cap.set(4, 480)  #adjusts height of video stream
    cap.set(5, 15)  #adjusts frame rate of video stream
    motor.setAngle(90)

    while (True):
        ret, frame = cap.read()
        frame = cv2.flip(frame, -1)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray,
                                              scaleFactor=1.5,
                                              minNeighbors=3)

        # if(len(faces) <= 0):
        #     faces = profileface_cascade.detectMultiScale(cv2.flip(gray, 1) ,scaleFactor=1.5, minNeighbors=5)
        #     if(len(faces) > 0):
        #         faces[0] = 640 - faces[0]

        # if(len(faces) <= 0):
        #     faces = profileface_cascade.detectMultiScale(gray ,scaleFactor=1.5, minNeighbors=5)

        if (len(faces) <= 0):  #safety
            motor.stop()

        for (x, y, w, h) in faces:
            # print(x,y,w,h)

            #roi
            roi_gray = gray[y:y + h, x:x + w]
            roi_color = frame[y:y + h, x:x + w]

            color = (255, 0, 0)
            stroke = 2
            end_cord_x = x + w
            end_cord_y = y + h
            cv2.rectangle(frame, (x, y), (end_cord_x, end_cord_y), color,
                          stroke)

            checkTilt = tilt(frame, x, y, w, h)
            checkPan = pan(frame, x, y, w, h)
            if (checkTilt and checkPan):
                # print('okay')
                pass
            roi(frame, x, y, w, h)

        cv2.imshow('frame', frame)

        if cv2.waitKey(20) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()
def main():
    cap = cv2.VideoCapture(0)  #sets the camera
    cap.set(3, 640)  #adjusts width of video stream
    cap.set(4, 480)  #adjusts height of video stream
    cap.set(5, 30)  #adjusts frame rate of video stream
    motor.setAngle(40)

    while (True):  #infinite loop with break condition at bottom
        ret, frame = cap.read()  #creates the frame with the camera
        frame = cv2.flip(frame, -1)

        last = 0

        fullCont = findContour(frame)  #contour for the entire frame
        if (len(fullCont) > 0):
            c = max(fullCont, key=cv2.contourArea)  #finds the largest contour
            x, y, w, h = cv2.boundingRect(
                c
            )  #returns the coresponding values creating a rect around the contour
            cv2.rectangle(frame, (x, y), ((x + w), (y + h)), (0, 255, 0),
                          3)  #draws a box to bound the contour area

            small_box = cv2.minAreaRect(c)
            (x_min, y_min), (w_min, h_min), ang = small_box

            if (ang < -45):
                ang = 90 + ang
            if (w_min < h_min and ang > 0):
                ang = (90 - ang) * -1
            if (w_min > h_min and ang < 0):
                ang = 90 + ang

            ang = int(ang)
            box = cv2.boxPoints(small_box)
            box = np.int0(box)

            last = calcSteer(ang)

            #draws the angle and the box after the image if fully processed
            cv2.drawContours(frame, [box], 0, (0, 0, 255), 2)
            cv2.putText(frame,
                        str(ang) + ' deg', (10, 30), cv2.FONT_HERSHEY_SIMPLEX,
                        0.75, (0, 0, 255), 2)
        else:
            if (last == 1):
                motor.right(0.5)
            elif (last == -1):
                motor.left(0.5)
            else:
                motor.stop()

        cv2.imshow('frame', frame)

        if cv2.waitKey(20) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()
Esempio n. 5
0
def on_release(key):
    try:
        if key.char in ['w', 'a', 's', 'd']:
            motor.stop()
    except:
        k = key.name