コード例 #1
0
    def __init__(self, port_number, pin_number, base_degrees):
        self.base_degrees = base_degrees
        self.board = ArduinoNano("/dev/cu.usbserial-{0}".format(port_number))
        self.servo = self.board.get_pin("d:{0}:s".format(pin_number))
        self.moving = False

        self.stop()
コード例 #2
0
    def connect(self, port: str, board: str="uno") -> None:
        """
        Initialize a connection with the arduino.

        Parameters
        ----------
        port : str
            Computer serial port to which the arduino is connected
        board : str, optional
            | The type of the arduino that is being used:
            | ``uno``: `Arduino Uno <https://store.arduino.cc/products/arduino-uno-rev3>`_
            | ``mega``: `Arduino Mega <https://store.arduino.cc/products/arduino-mega-2560-rev3>`_
            | ``due``: `Arduino Due <https://store.arduino.cc/products/arduino-due>`_
            | ``nano``: `Arduino Nano <https://store.arduino.cc/products/arduino-nano>`_
        """
        self.port = port

        if board == "uno":
            self.board = Arduino(self.port)
        elif board == "mega":
            self.board = ArduinoMega(self.port)
        elif board == "due":
            self.board = ArduinoDue(self.port)
        elif board == "nano":
            self.board = ArduinoNano(self.port)
        else:
            raise UnknownBoardException("Unknown board " + board)
        self.it = util.Iterator(self.board)
        self.it.start()
コード例 #3
0
class Servo:
    # 0.4s, +10 is 90degrees.
    # At here, 0.15s, +10 is 3090degrees.
    delay_time = 0.15

    def __init__(self, port_number, pin_number, base_degrees):
        self.base_degrees = base_degrees
        self.board = ArduinoNano("/dev/cu.usbserial-{0}".format(port_number))
        self.servo = self.board.get_pin("d:{0}:s".format(pin_number))
        self.moving = False

        self.stop()

    def write(self, degrees):
        assert 0 <= degrees <= 180, "degrees should within 0~180."
        try:
            self.servo.write(degrees)
        except IOError as e:
            print("Error: {0}".format(e))
        except:
            print("SOMETHING??")

    def stop(self):
        self.write(self.base_degrees)

    def exit(self):
        self.board.exit()

    def turn_left(self):
        if self.moving:
            print("WAIT")
            return

        self.write(self.base_degrees + 10)
        print("TURN LEFT")
        self.moving = True
        threading.Timer(self.delay_time, self.__moving_and_stop).start()

    def turn_right(self):
        if self.moving:
            print("WAIT")
            return

        self.write(self.base_degrees - 10)
        print("TURN RIGHT")
        self.moving = True
        threading.Timer(self.delay_time, self.__moving_and_stop).start()

    def __moving_and_stop(self):
        self.moving = False
        self.stop()

    def rotate(self, degrees):
        # Rotate specified degrees.
        # Have to wait until rotate finish.
        # Use timer to cancel operation while rotating.
        pass
コード例 #4
0
    def onClick(self):
        def progressValue(stt, current, total=10):
            self.lbStatus.setText(stt)
            self.progressBar.setValue((current / total) * 100)

        def sound_alarm():
            playsound.playsound('alarm.wav')

        def sound_confirm():
            playsound.playsound('confirm.mp3')

        def eye_aspect_ratio(eye):
            A = dist.euclidean(eye[1], eye[5])
            B = dist.euclidean(eye[2], eye[4])
            C = dist.euclidean(eye[0], eye[3])
            return (A + B) / (2.0 * C)

        def mouth_ratio(mouth):
            AB = dist.euclidean(mouth[12], mouth[16])
            CD = dist.euclidean(mouth[13], mouth[19])
            EF = dist.euclidean(mouth[14], mouth[18])
            GH = dist.euclidean(mouth[15], mouth[17])
            return (CD + EF + GH) / (3.0 * AB)

        progressValue('Initial Constant Values', 1)

        ledRED = 2
        ledYELLOW = 3
        ledGREEN = 4

        # EYE_THRESH = self.spinEye.value()
        # MOUTH_THRESH = self.spinMouth.value()
        # EYE_CONTINOUS_FRAMES = self.slideEye.value()
        # MOUTH_CONTINOUS_FRAMES = self.slideMouth.value()

        COUNTER_EYES = COUNTER_MOUTH = COUNTER_COMMAND = 0
        ALARM_ON = CONFIRM_ON = False

        progressValue('Loading face detector', 2)
        # print("[INFO] loading facial landmark predictor...")
        detector = dlib.get_frontal_face_detector()
        progressValue('Loading facial landmark predictor', 3)
        predictor = dlib.shape_predictor(
            'shape_predictor_68_face_landmarks.dat')

        progressValue('Loading facial coordinates', 5)
        (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
        (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
        (mouthStart, mouthEnd) = face_utils.FACIAL_LANDMARKS_IDXS["mouth"]

        progressValue('Opening serial port', 6)
        # print("[INFO] opening serial port...")
        # board = ArduinoNano('COM11')
        board = ArduinoNano('/dev/' + self.comboPort.currentText())

        progressValue('Starting video stream thread', 7)
        # print("[INFO] starting video stream thread...")
        vs = VideoStream(src=0).start()
        progressValue('FPS sclaler', 8)
        fps = FPS().start()
        progressValue('Sleep in 1 second', 9)
        time.sleep(1.0)
        progressValue('Done', 10)

        while True:
            frame = vs.read()
            frame = imutils.resize(frame, self.spinSize.value())
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            rects = detector(gray, 0)

            for rect in rects:
                shape = predictor(gray, rect)
                shape = face_utils.shape_to_np(shape)

                leftEye = shape[lStart:lEnd]
                rightEye = shape[rStart:rEnd]
                mouth = shape[mouthStart:mouthEnd]

                avr_eyes = (eye_aspect_ratio(leftEye) +
                            eye_aspect_ratio(rightEye)) / 2.0
                avr_mouth = mouth_ratio(mouth)

                for item in (leftEye):
                    cv2.circle(frame, tuple(item), 2, (0, 255, 0), -1)
                for item in (rightEye):
                    cv2.circle(frame, tuple(item), 2, (0, 255, 0), -1)
                for item in (mouth):
                    cv2.circle(frame, tuple(item), 2, (0, 255, 0), -1)

                # if avr_eyes < EYE_THRESH:
                if avr_eyes < self.spinEye.value():
                    COUNTER_EYES += 1

                    # if COUNTER_EYES >= EYE_CONTINOUS_FRAMES:
                    if COUNTER_EYES >= self.slideEye.value():

                        if not ALARM_ON:
                            ALARM_ON = True
                            COUNTER_COMMAND += 1
                            t = Thread(target=sound_alarm)
                            t.deamon = True
                            t.start()
                        # cv2.putText(frame, "DROWSINESS ALERT!", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
                else:
                    # open eyes to reset these values
                    COUNTER_EYES = 0
                    ALARM_ON = False

                # if avr_mouth > MOUTH_THRESH:
                if avr_mouth > self.spinMouth.value():
                    COUNTER_MOUTH += 1

                    # if COUNTER_MOUTH >= MOUTH_CONTINOUS_FRAMES:
                    if COUNTER_MOUTH >= self.slideMouth.value():
                        if not CONFIRM_ON:
                            CONFIRM_ON = True

                            # arduino
                            if COUNTER_COMMAND == 1:
                                board.digital[ledRED].write(1)
                            elif COUNTER_COMMAND == 2:
                                board.digital[ledYELLOW].write(1)
                            elif COUNTER_COMMAND == 3:
                                board.digital[ledGREEN].write(1)

                            COUNTER_COMMAND = 0

                            t = Thread(target=sound_confirm)
                            t.deamon = True
                            t.start()
                        cv2.putText(frame, "CONFIRM COMMAND!",
                                    (frame.shape[0] - 30, 30),
                                    cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255),
                                    2)
                else:
                    # open eyes to reset these values
                    COUNTER_MOUTH = 0
                    CONFIRM_ON = False

                cv2.putText(frame, "Average Eyes: {:.2f}".format(avr_eyes),
                            (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
                            (0, 0, 255), 2)
                cv2.putText(frame, "Average Mouth: {:.2f}".format(avr_mouth),
                            (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
                            (0, 0, 255), 2)
                cv2.putText(frame, "Command: {}".format(COUNTER_COMMAND),
                            (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
                            (0, 100, 255), 2)

            if (self.checkShow.isChecked()):
                cv2.imshow("Frame", frame)

            if cv2.waitKey(1) == 27:
                break

            fps.update()

        fps.stop()
        print("FPS trung binh: {:.2f}".format(fps.fps()))
        cv2.destroyAllWindows()
        vs.stop()
コード例 #5
0
MOUTH_CONTINOUS_FRAMES = 15

COUNTER_EYES = COUNTER_MOUTH = COUNTER_COMMAND = 0
ALARM_ON = CONFIRM_ON = False

print("[INFO] loading facial landmark predictor...")
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')

(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
(mouthStart, mouthEnd) = face_utils.FACIAL_LANDMARKS_IDXS["mouth"]

print("[INFO] opening serial port...")
# board = ArduinoNano('COM11')
board = ArduinoNano(serial_ports())

print("[INFO] starting video stream thread...")
vs = VideoStream(src=0).start()
fps = FPS().start()
time.sleep(1.0)

while True:
    frame = vs.read()
    frame = imutils.resize(frame, width=450)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    rects = detector(gray, 0)

    for rect in rects:
        shape = predictor(gray, rect)
コード例 #6
0
    def onClick(self):
        def serial_ports():
            for port in glob.glob('/dev/tty[A-Za-z]*'):
                try:
                    s = serial.Serial(port)
                    s.close()
                    return port
                except (OSError, serial.SerialException):
                    pass
            print('Connecting Error ...')

        def sound_alarm():
            playsound.playsound('alarm.wav')

        def sound_confirm():
            playsound.playsound('confirm.mp3')

        def eye_aspect_ratio(eye):
            A = dist.euclidean(eye[1], eye[5])
            B = dist.euclidean(eye[2], eye[4])
            C = dist.euclidean(eye[0], eye[3])
            return (A + B) / (2.0 * C)

        def mouth_ratio(mouth):
            AB = dist.euclidean(mouth[12], mouth[16])
            CD = dist.euclidean(mouth[13], mouth[19])
            EF = dist.euclidean(mouth[14], mouth[18])
            GH = dist.euclidean(mouth[15], mouth[17])
            return (CD + EF + GH) / (3.0 * AB)

        ledRED = 2
        ledYELLOW = 3
        ledGREEN = 4

        EYE_THRESH = 0.24
        MOUTH_THRESH = 0.27
        EYE_CONTINOUS_FRAMES = 15
        MOUTH_CONTINOUS_FRAMES = 15

        COUNTER_EYES = COUNTER_MOUTH = COUNTER_COMMAND = 0
        ALARM_ON = CONFIRM_ON = False

        print("[INFO] loading facial landmark predictor...")
        detector = dlib.get_frontal_face_detector()
        predictor = dlib.shape_predictor(
            'shape_predictor_68_face_landmarks.dat')

        (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
        (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
        (mouthStart, mouthEnd) = face_utils.FACIAL_LANDMARKS_IDXS["mouth"]

        print("[INFO] opening serial port...")
        # board = ArduinoNano('COM11')
        board = ArduinoNano('/dev/' + self.comboBox.currentText())

        print("[INFO] starting video stream thread...")
        vs = VideoStream(src=2).start()
        fps = FPS().start()
        time.sleep(1.0)

        while True:
            frame = vs.read()
            frame = imutils.resize(frame, width=900)
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            rects = detector(gray, 0)

            for rect in rects:
                shape = predictor(gray, rect)
                shape = face_utils.shape_to_np(shape)

                leftEye = shape[lStart:lEnd]
                rightEye = shape[rStart:rEnd]
                mouth = shape[mouthStart:mouthEnd]

                avr_eyes = (eye_aspect_ratio(leftEye) +
                            eye_aspect_ratio(rightEye)) / 2.0
                avr_mouth = mouth_ratio(mouth)

                for item in (leftEye):
                    cv2.circle(frame, tuple(item), 2, (0, 255, 0), -1)
                for item in (rightEye):
                    cv2.circle(frame, tuple(item), 2, (0, 255, 0), -1)
                for item in (mouth):
                    cv2.circle(frame, tuple(item), 2, (0, 255, 0), -1)

                if avr_eyes < EYE_THRESH:
                    COUNTER_EYES += 1

                    if COUNTER_EYES >= EYE_CONTINOUS_FRAMES:
                        if not ALARM_ON:
                            ALARM_ON = True
                            COUNTER_COMMAND += 1
                            t = Thread(target=sound_alarm)
                            t.deamon = True
                            t.start()
                        # cv2.putText(frame, "DROWSINESS ALERT!", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
                else:
                    # open eyes to reset these values
                    COUNTER_EYES = 0
                    ALARM_ON = False

                if avr_mouth > MOUTH_THRESH:
                    COUNTER_MOUTH += 1

                    if COUNTER_MOUTH >= MOUTH_CONTINOUS_FRAMES:
                        if not CONFIRM_ON:
                            CONFIRM_ON = True

                            # arduino
                            if COUNTER_COMMAND == 1:
                                board.digital[ledRED].write(1)
                            elif COUNTER_COMMAND == 2:
                                board.digital[ledYELLOW].write(1)
                            elif COUNTER_COMMAND == 3:
                                board.digital[ledGREEN].write(1)

                            COUNTER_COMMAND = 0

                            t = Thread(target=sound_confirm)
                            t.deamon = True
                            t.start()
                        cv2.putText(frame, "CONFIRM COMMAND!",
                                    (frame.shape[0] - 30, 30),
                                    cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255),
                                    2)
                else:
                    # open eyes to reset these values
                    COUNTER_MOUTH = 0
                    CONFIRM_ON = False

                cv2.putText(frame, "Average Eyes: {:.2f}".format(avr_eyes),
                            (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
                            (0, 0, 255), 2)
                cv2.putText(frame, "Average Mouth: {:.2f}".format(avr_mouth),
                            (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
                            (0, 0, 255), 2)
                cv2.putText(frame, "Command: {}".format(COUNTER_COMMAND),
                            (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
                            (0, 100, 255), 2)

            fps.update()

            cv2.imshow("Frame", frame)

            if cv2.waitKey(1) == 27:
                break

        fps.stop()
        print("FPS trung binh: {:.2f}".format(fps.fps()))
        cv2.destroyAllWindows()
        vs.stop()
コード例 #7
0
from flask_migrate import Migrate, MigrateCommand
from sqlalchemy.orm import relationship, backref
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
from flask_security import UserMixin, RoleMixin, current_user
from flask_admin.contrib import sqla
from flask_admin import helpers as admin_helpers
import click
import code
import io
import time
from pyfirmata import ArduinoNano, util
import datetime
import time

board = ArduinoNano('/dev/cu.usbserial-1410')
led_blue = board.get_pin('d:4:o')
led_blue.write(0)
moisture = board.get_pin('a:7:i')

app = Flask(__name__)
app.config['DEBUG'] = True
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.config['SECRET_KEY'] = "hadf38746"
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
app.config['SECURITY_PASSWORD_SALT'] = '93iu4hkejfndm,'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
toolbar = DebugToolbarExtension(app)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
manager = Manager(app)
コード例 #8
0
from pyfirmata import pyfirmata,ArduinoNano,util
import time
# check line number 490 in StandardFirmata_edited.ino file , for the code we have edited
board=ArduinoNano('COM3')
iter = util.Iterator(board)
iter.start()
time.sleep(1)
def receive(*args, **kwargs):
    print(args)
    print(kwargs)
board.add_cmd_handler(0x02,receive)
board.send_sysex(0x01,[0x06,0x05,0x11,0x02,0x06])
while 1:
    pass

	#print util.two_byte_iter_to_str(args)

コード例 #9
0
class BaseArduinoDriver(PyroArduino):
    """
    A base class providing pin read/write access for common Arduino boards.
    """

    def connect(self, port: str, board: str="uno") -> None:
        """
        Initialize a connection with the arduino.

        Parameters
        ----------
        port : str
            Computer serial port to which the arduino is connected
        board : str, optional
            | The type of the arduino that is being used:
            | ``uno``: `Arduino Uno <https://store.arduino.cc/products/arduino-uno-rev3>`_
            | ``mega``: `Arduino Mega <https://store.arduino.cc/products/arduino-mega-2560-rev3>`_
            | ``due``: `Arduino Due <https://store.arduino.cc/products/arduino-due>`_
            | ``nano``: `Arduino Nano <https://store.arduino.cc/products/arduino-nano>`_
        """
        self.port = port

        if board == "uno":
            self.board = Arduino(self.port)
        elif board == "mega":
            self.board = ArduinoMega(self.port)
        elif board == "due":
            self.board = ArduinoDue(self.port)
        elif board == "nano":
            self.board = ArduinoNano(self.port)
        else:
            raise UnknownBoardException("Unknown board " + board)
        self.it = util.Iterator(self.board)
        self.it.start()


    def digital_write(self, pin: int, value: int) -> None:
        """
        Tell the arduino to turn a pin digitally to the inputed value.

        Parameters
        ----------
        pin : int
            Integer that represents the digital out pin number on an arduino
        value : int
            | The value of the digital pin to be set
            | 0: LOW
            | 1: HIGH
        """
        self.board.digital[pin].mode = OUTPUT
        pin = self.board.digital[pin].write(value)
    
    def pwm_write(self, pin: int, value: float) -> None:
        """
        Tell the arduino to produce a pwm signal on a pin.

        Parameters
        ----------
        pin : int
            Integer that represents the digital out pin number on an arduino
        value : float
            The duty cycle of the pwm to be set (0 - 1.0)
        """
        self.board.digital[pin].mode = PWM
        self.board.digital[pin].write(value)
    
    def servo_write(self, pin: int, value: int) -> None:
        """
        Tell the arduino to configure a pin to drive a servo to
        the inputed angle.

        Parameters
        ----------
        pin : int
            Integer that represents the digital out pin number on an arduino
        value : int
            The angle in degrees to move the servo to
        """
        self.board.digital[pin].mode = SERVO
        self.board.digital[pin].write(value)
    
    def digital_read(self, pin: int) -> int:
        """
        Tell the arduino to read a value from a digital pin.

        Parameters
        ----------
        pin : int
            Integer that represents the digital in pin number on an arduino
        
        Returns
        -------
        int
            The value read by the digital pin, 0 (LOW) or 1 (HIGH)
        """
        self.board.digital[pin].mode = INPUT
        return self.board.digital[pin].read()
    
    def analog_read(self, pin: int) -> float:
        """
        Tell the arduino to read a value from an analog pin.

        Parameters
        ----------
        pin : int
            Integer that represents the analog in pin number on an arduino
        
        Returns
        -------
        float
            The value read by the analog pin (0 - 1.0)
        """
        # self.board.iterate()
        self.board.analog[pin].mode = INPUT
        self.board.analog[pin].enable_reporting()
        while True:
            value = self.board.analog[pin].read()
            if value is not None:
                break
        return value

    def close(self) -> None:
        """
        Close the connection with the arduino.
        """
        self.board.exit()