Пример #1
0
def change(changePin):
    changePin = int(changePin)
    if changePin == 1:
        motor.turnLeft()
    elif changePin == 2:
        motor.forward()
    elif changePin == 3:
        motor.turnRight()
    elif changePin == 4:
        motor.backward()
    elif changePin == 5:
        motor.stop()
    else:
        print("Wrong command")

    return render_template("index.html")
Пример #2
0
def choice(input,conn):
    t=5
    start = t
    inc = 0.5
    try:
	if input == 'f':
            while t>0:
                if(sensor.willFrontCrash()):
                    print('stopped after ' + str(start-t))
                    break
                motor.forward(inc)
                t = t-inc
	    print('Move forward')
    	elif input == 'b':
            while t>0:
                if(sensor.willBackCrash()):
                    print('stopped after ' + str(start-t))
                    break
                motor.backward(inc)
                t = t-inc
	    print('Move backward')
    	elif input == 'r':
            motor.turnRight(t)
	    print('turn right')
    	elif input == 'l':	
            motor.turnLeft(t)
	    print('turn left')
	elif input == 's':
            reading = sensor.getAllSensor()
            print('Read all sensors')
            print(reading)
            result = " ".join(reading)
            conn.send(result)
    except KeyboardInterrupt:
	    print('key board interrupted!')
    finally:
            print('done')
    conn.send("\n")
    ret, img = cam.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ball = ball_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in ball:
        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
        full = w + h
        tengah = x + (w / 2)
        print("posisi x :", str(tengah))
        print("full" + str(full))
        if tengah < 270:
            motor.turnLeft()
            time.sleep(1.0)
            b += 1

        elif tengah > 370:
            motor.turnRight()
        else:
            motor.stop()

        if full > 470:
            motor.backward()
        elif full < 430:
            motor.forward()
        else:
            motor.stop()

    cv2.putText(img, "people : " + str(b), (10, 50), cv2.FONT_HERSHEY_SIMPLEX,
                1.0, (255, 0, 0), 2, cv2.LINE_AA)
    cv2.imshow('frame', img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
Пример #4
0

while True:

    distance = [sensor.distance(i) for i in xrange(6)]
    for i in xrange(6):
        if distance[i] is None:
            distance[i] = 300

    if distance[front] > clearance and distance[
            frontLeft] > clearance and distance[frontRight] > clearance:
        motor.front(m1, m2)

    elif distance[frontRight] > distance[frontLeft]:
        if distance[frontRight] > clearance:
            motor.turnRight(m1, m2)
            sleep(2.5)
            motor.front(m1, m2)

    elif distance[frontLeft] > clearance:
        motor.turnLeft(m1, m2)
        sleep(2.5)
        motor.front(m1, m2)

    else:
        motor.back(m1, m2)
        sleep(5)
        if (distance[backRight] >
                distance[backLeft]) and distance[backRight] > clearance:
            motor.turnLeft(m1, m2)
            sleep(2.5)
Пример #5
0
 def Act(self):
     MotorControl.turnLeft(0.25)
     MotorControl.turnRight(0.5)
     MotorControl.turnLeft(0.25)
     exit()
Пример #6
0
def gen():
    """Video streaming generator function."""
    cap = cv2.VideoCapture(0)
    faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    rec = cv2.face.LBPHFaceRecognizer_create()
    rec.read("recognizer/training_data.yml")
    i = 0
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter('output' + str(i) + '.avi', fourcc, 10.0, (640, 480))
    engine = pyttsx3.init()
    engine.setProperty(
        'voice',
        "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0"
    )  #set voice type
    engine.setProperty('rate', 100)  ### 10 adalah kecepatan baca
    #cap.set(cv2.CAP_PROP_FRAME_WIDTH, 160)
    #cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 120)
    # Read until video is completed
    while (cap.isOpened()):
        ret, frame = cap.read()  # import image
        if not ret:  #if vid finish repeat
            cap = cv2.VideoCapture(0)
            continue
        if ret:  # if there is a frame continue with code
            faceDetect = cv2.CascadeClassifier(
                'haarcascade_frontalface_default.xml')
            gray = cv2.cvtColor(frame,
                                cv2.COLOR_BGR2GRAY)  # converts image to gray
            faces = faceDetect.detectMultiScale(gray, 1.3, 5)
            cv2.putText(frame, str(datetime.now()), (10, 50),
                        cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2,
                        cv2.LINE_AA)
            for (x, y, w, h) in faces:
                cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255),
                              4)  #cv2.imshow("countours", image)
                id, conf = rec.predict(gray[y:y + h, x:x + w])
                if (id == 1):
                    text = "ILHAM"
                    engine.say(text)
                    engine.runAndWait()
                else:
                    text = "UNKNOWN"
                    engine.say(text)
                    engine.runAndWait()
                cv2.putText(frame, text, (10, 80), cv2.FONT_HERSHEY_SIMPLEX,
                            1.0, (0, 0, 255), 2, cv2.LINE_AA)
                if os.path.exists("output" + str(i) + ".avi"):
                    i += 1
                out.write(frame)
                full = w + h
                tengah = x + (w / 2)
                print("posisi x :", str(tengah))
                print("full" + str(full))
                if tengah < 270:
                    motor.turnLeft()
                elif tengah > 370:
                    motor.turnRight()
                else:
                    motor.stop()

                if full > 470:
                    motor.backward()
                elif full < 430:
                    motor.forward()
                else:
                    motor.stop()

        frame = cv2.imencode('.jpg', frame)[1].tobytes()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
        #time.sleep(0.1)
        key = cv2.waitKey(20)
        if key == 27:
            break
    motor.cleanup()