|  | 
|  | 1 | +import smtplib | 
|  | 2 | +from email.mime.multipart import MIMEMultipart | 
|  | 3 | +from email.mime.text import MIMEText | 
|  | 4 | +from threading import Thread | 
|  | 5 | + | 
|  | 6 | +import cv2 | 
|  | 7 | +from PyQt5.QtCore import * | 
|  | 8 | +from PyQt5.QtGui import * | 
|  | 9 | +from PyQt5.QtWidgets import * | 
|  | 10 | +from PyQt5.uic import loadUi | 
|  | 11 | +from zeep import Client | 
|  | 12 | + | 
|  | 13 | +from Speach import Speach | 
|  | 14 | +from WheelChair import WheelChair | 
|  | 15 | +from image_processors.BlinkDetector import BlinkDetector | 
|  | 16 | +from image_processors.FaceDetector import FaceDetector | 
|  | 17 | +from image_processors.GazeDetector import GazeDetector | 
|  | 18 | + | 
|  | 19 | + | 
|  | 20 | +class MainWindow(QMainWindow): | 
|  | 21 | +    def __init__(self): | 
|  | 22 | +        super(MainWindow, self).__init__() | 
|  | 23 | +        loadUi("./ui/MainWindow.ui", self) | 
|  | 24 | +        self.setWindowTitle("Eye Based Wheelchair Control & Task Manager") | 
|  | 25 | +        self.resetButton.clicked.connect(self.resetAll) | 
|  | 26 | + | 
|  | 27 | +        self.faceDetector = FaceDetector() | 
|  | 28 | + | 
|  | 29 | +        self.currentFocus = 0 | 
|  | 30 | +        self.__initialize_buttons() | 
|  | 31 | +        self.timer = QTimer(self) | 
|  | 32 | +        self.timer.timeout.connect(self.updateFrame) | 
|  | 33 | +        self.soundThread = None | 
|  | 34 | + | 
|  | 35 | +        self.chair = WheelChair() | 
|  | 36 | + | 
|  | 37 | +        self.current_subprecess = None | 
|  | 38 | + | 
|  | 39 | +        # mode 0 = not controlling wheel chair; controlling menu with eye-blink | 
|  | 40 | +        # mode 1 = controling wheel chair with eye-gaze and eye-blink | 
|  | 41 | +        # mode 2 = Speech mode | 
|  | 42 | +        # mode 3 = Face mode | 
|  | 43 | +        # mode 4 = Face mode wheel chair | 
|  | 44 | +        self.current_mode = 0 | 
|  | 45 | + | 
|  | 46 | +        self.cap = cv2.VideoCapture(0) | 
|  | 47 | + | 
|  | 48 | +        self.gazeDetector = GazeDetector() | 
|  | 49 | + | 
|  | 50 | +        self.blinkDetector = BlinkDetector() | 
|  | 51 | + | 
|  | 52 | +        self.speech = Speach() | 
|  | 53 | +        self.initialize_speech() | 
|  | 54 | + | 
|  | 55 | +        self.timer.start(10) | 
|  | 56 | + | 
|  | 57 | +        self.main_image_label.setScaledContents(True) | 
|  | 58 | + | 
|  | 59 | +    def resetAll(self): | 
|  | 60 | +        self.current_mode = 0 | 
|  | 61 | +        self.chair.stop() | 
|  | 62 | +        self.chair.is_going = False | 
|  | 63 | +        self.gazeDetector.reset() | 
|  | 64 | +        self.blinkDetector.reset() | 
|  | 65 | + | 
|  | 66 | +    def updateFrame(self): | 
|  | 67 | +        #print(self.current_mode) | 
|  | 68 | +	 | 
|  | 69 | +        info = {} | 
|  | 70 | +        if self.current_mode == 0 or self.current_mode == 1 or self.current_mode == 3 or self.current_mode == 4: | 
|  | 71 | +            _, img = self.cap.read() | 
|  | 72 | +            blink_dict = self.blinkDetector.run_blink_detector(img, self.eyeThreshold.value()) | 
|  | 73 | +            if self.current_mode != 4 or self.current_mode != 2: | 
|  | 74 | +                outImage = toQImage(blink_dict["image"]) | 
|  | 75 | +                outImage = outImage.rgbSwapped() | 
|  | 76 | +                self.main_image_label.setPixmap(QPixmap.fromImage(outImage)) | 
|  | 77 | +            info["left"] = blink_dict["leftTotal"] | 
|  | 78 | +            info["right"] = blink_dict["rightTotal"] | 
|  | 79 | +            info["both"] = blink_dict["bothTotal"] | 
|  | 80 | +            info["rightEAR"] = blink_dict["rightEAR"] | 
|  | 81 | +            info["leftEAR"] = blink_dict["leftEAR"] | 
|  | 82 | +            info["avgEAR"] = (blink_dict["rightEAR"] + blink_dict["leftEAR"]) / 2 | 
|  | 83 | +            flag = 0 | 
|  | 84 | +            if blink_dict["both"]: | 
|  | 85 | +                if self.current_subprecess is not None: | 
|  | 86 | +                    self.current_subprecess.terminate() | 
|  | 87 | +                    self.current_subprecess = None | 
|  | 88 | +                    flag = 1 | 
|  | 89 | +            if self.current_subprecess is not None: | 
|  | 90 | +                return | 
|  | 91 | +            if self.current_mode is 0: | 
|  | 92 | +                if blink_dict["left"]: | 
|  | 93 | +                    self.moveFocusLeft() | 
|  | 94 | +                if blink_dict["right"]: | 
|  | 95 | +                    self.moveFocusRight() | 
|  | 96 | +                if blink_dict["both"] and flag == 0: | 
|  | 97 | +                    self.pressFocused() | 
|  | 98 | + | 
|  | 99 | +            elif self.current_mode is 1: | 
|  | 100 | +                gazeDict = self.gazeDetector.get_processed_image(img) | 
|  | 101 | +                info["dir"] = gazeDict["direction"] | 
|  | 102 | +                if gazeDict["direction"] == "left": | 
|  | 103 | +                    self.chair.left() | 
|  | 104 | +                if gazeDict["direction"] == "right": | 
|  | 105 | +                    self.chair.right() | 
|  | 106 | +                if gazeDict["direction"] == "center": | 
|  | 107 | +                    self.chair.start() | 
|  | 108 | + | 
|  | 109 | +                if blink_dict["left"] or blink_dict["right"]: | 
|  | 110 | +                    self.chair.toggleStartStop() | 
|  | 111 | + | 
|  | 112 | +                elif blink_dict["both"]: | 
|  | 113 | +                    self.chair.stop() | 
|  | 114 | +                    self.chair.is_going = False | 
|  | 115 | +                    self.current_mode = 0 | 
|  | 116 | +                    self.gazeDetector.closeAll() | 
|  | 117 | + | 
|  | 118 | +            elif self.current_mode == 3: | 
|  | 119 | +                faceDict = self.faceDetector.get_processed_image(img) | 
|  | 120 | +                if blink_dict["right"]: | 
|  | 121 | +                    self.faceDetector.initPos(faceDict["face"]) | 
|  | 122 | + | 
|  | 123 | +                if blink_dict["both"] and flag == 0: | 
|  | 124 | +                    self.pressFocused() | 
|  | 125 | + | 
|  | 126 | +                if faceDict["direction"] == "right": | 
|  | 127 | +                    self.moveFocusRight() | 
|  | 128 | +                if faceDict["direction"] == "left": | 
|  | 129 | +                    self.moveFocusLeft() | 
|  | 130 | +                if faceDict["direction"] == "up": | 
|  | 131 | +                    self.moveFocusUp() | 
|  | 132 | +                if faceDict["direction"] == "down": | 
|  | 133 | +                    self.moveFocusDown() | 
|  | 134 | + | 
|  | 135 | +            elif self.current_mode == 4: | 
|  | 136 | +                self.chair.is_going = True | 
|  | 137 | +                faceDict = self.faceDetector.get_processed_image(img) | 
|  | 138 | +                self.main_image_label.setText("Chair wheel Mode" | 
|  | 139 | +                                              "\n\n Press right eye to initialize" | 
|  | 140 | +                                              "\n\n Press both eye for exit") | 
|  | 141 | +                if blink_dict["both"]: | 
|  | 142 | +                    self.chair.stop() | 
|  | 143 | +                    self.chair.is_going = False | 
|  | 144 | +                    self.current_mode = 3 | 
|  | 145 | + | 
|  | 146 | +                if faceDict["direction"] == "right": | 
|  | 147 | +                    self.chair.right() | 
|  | 148 | +                elif faceDict["direction"] == "left": | 
|  | 149 | +                    self.chair.left() | 
|  | 150 | +                elif faceDict["direction"] == "up": | 
|  | 151 | +                    self.chair.start() | 
|  | 152 | +                elif faceDict["direction"] == "down": | 
|  | 153 | +                    self.chair.stop() | 
|  | 154 | +                elif faceDict["direction"] == "center": | 
|  | 155 | +                     self.chair.stop() | 
|  | 156 | + | 
|  | 157 | +        elif self.current_mode == 2: | 
|  | 158 | +            if self.soundThread is None or not self.soundThread.is_alive(): | 
|  | 159 | +                self.soundThread = Thread(target=self.speech.recognize_speech_from_mic) | 
|  | 160 | +                self.soundThread.start() | 
|  | 161 | +                print("Started Listening") | 
|  | 162 | +            self.main_image_label.setText("Listening") | 
|  | 163 | + | 
|  | 164 | +        self.updateImageInfo(info) | 
|  | 165 | + | 
|  | 166 | +    def updateImageInfo(self, dict): | 
|  | 167 | +        val = "" | 
|  | 168 | +        for key, value in dict.items(): | 
|  | 169 | +            val = val + "\n" + str(key) + " : " + str(value) | 
|  | 170 | + | 
|  | 171 | +        self.image_info_textlabel.setText(val) | 
|  | 172 | + | 
|  | 173 | +    def initialize_speech(self): | 
|  | 174 | +        self.speech.commands["video"].append(self.playVideo) | 
|  | 175 | +        self.speech.commands["music"].append(self.playMusic) | 
|  | 176 | +        self.speech.commands["SMS"].append(self.playSMS) | 
|  | 177 | +        self.speech.commands["message"].append(self.playEmail) | 
|  | 178 | +        self.speech.commands["light"].append(self.playLight) | 
|  | 179 | +        self.speech.commands["ceiling fan"].append(self.playFan) | 
|  | 180 | +        self.speech.commands["news"].append(self.playBrowser) | 
|  | 181 | + | 
|  | 182 | +        self.speech.commands["start"].append(self.chair.start) | 
|  | 183 | +        self.speech.commands["stop"].append(self.chair.stop) | 
|  | 184 | +        self.speech.commands["right"].append(self.chair.right) | 
|  | 185 | +        self.speech.commands["left"].append(self.chair.left) | 
|  | 186 | + | 
|  | 187 | +        self.speech.commands["close"].append(self.stopCurrentSubprocess) | 
|  | 188 | + | 
|  | 189 | + | 
|  | 190 | +    def stopCurrentSubprocess(self): | 
|  | 191 | +        if self.current_subprecess is not None: | 
|  | 192 | +            self.current_subprecess.terminate() | 
|  | 193 | + | 
|  | 194 | +    def comboboxIndexChanged(self): | 
|  | 195 | +        if self.selectMethodComboBox.currentIndex() == 0: | 
|  | 196 | +            self.current_mode = 0 | 
|  | 197 | +        elif self.selectMethodComboBox.currentIndex() == 1: | 
|  | 198 | +            self.current_mode = 3 | 
|  | 199 | +            self.chair.is_going = False | 
|  | 200 | +        elif self.selectMethodComboBox.currentIndex() == 2: | 
|  | 201 | +            self.current_mode = 2 | 
|  | 202 | +            self.chair.is_going = True | 
|  | 203 | +        if self.selectMethodComboBox.currentIndex() != 2: | 
|  | 204 | +            self.soundThread = None | 
|  | 205 | + | 
|  | 206 | +    def __initialize_buttons(self): | 
|  | 207 | +        self.selectMethodComboBox.clear() | 
|  | 208 | +        self.selectMethodComboBox.addItems([ | 
|  | 209 | +            "Eye-Help", | 
|  | 210 | +            "Head-Help", | 
|  | 211 | +            "Voice-Help" | 
|  | 212 | +        ]) | 
|  | 213 | +        self.selectMethodComboBox.setCurrentIndex(0) | 
|  | 214 | +        self.selectMethodComboBox.currentIndexChanged.connect(self.comboboxIndexChanged) | 
|  | 215 | + | 
|  | 216 | +        self.buttons = [self.b1_1, self.b1_2, | 
|  | 217 | +                        self.b1_3, self.b2_1, | 
|  | 218 | +                        self.b2_2, self.b2_3, | 
|  | 219 | +                        self.b3_1, self.b3_2] | 
|  | 220 | +        for b in self.buttons: | 
|  | 221 | +            b.setAutoDefault(True) | 
|  | 222 | +        self.buttons[self.currentFocus].setFocus(True) | 
|  | 223 | + | 
|  | 224 | +        self.b1_1.clicked.connect(self.controlWheel) | 
|  | 225 | +        self.b1_2.clicked.connect(self.playSMS) | 
|  | 226 | +        self.b1_3.clicked.connect(self.playEmail) | 
|  | 227 | +        self.b2_1.clicked.connect(self.playVideo) | 
|  | 228 | +        self.b2_2.clicked.connect(self.playMusic) | 
|  | 229 | +        self.b2_3.clicked.connect(self.playBrowser) | 
|  | 230 | +        self.b3_1.clicked.connect(self.playLight) | 
|  | 231 | +        self.b3_2.clicked.connect(self.playFan) | 
|  | 232 | + | 
|  | 233 | +    def playSMS(self): | 
|  | 234 | +        try: | 
|  | 235 | +            url = 'https://api2.onnorokomsms.com/sendsms.asmx?WSDL' | 
|  | 236 | +            client = Client(url) | 
|  | 237 | +            userName = '01521313223' | 
|  | 238 | +            password = '90053' | 
|  | 239 | +            recipientNumber = '01521323429' | 
|  | 240 | +            smsText = 'Help Me' | 
|  | 241 | +            smsType = 'TEXT' | 
|  | 242 | +            maskName = '' | 
|  | 243 | +            campaignName = '' | 
|  | 244 | +            client.service.OneToOne(userName, password, recipientNumber, smsText, smsType, maskName, campaignName) | 
|  | 245 | +            print('SMS sent!') | 
|  | 246 | +        except Exception as e: | 
|  | 247 | +            print('SMS nor sent!') | 
|  | 248 | +            print(e) | 
|  | 249 | + | 
|  | 250 | +    def controlWheel(self): | 
|  | 251 | +        if self.current_mode == 3 or self.current_mode == 4: | 
|  | 252 | +            self.current_mode = 4 | 
|  | 253 | +        else: | 
|  | 254 | +            self.current_mode = 1 | 
|  | 255 | + | 
|  | 256 | +    def playEmail(self): | 
|  | 257 | +        try: | 
|  | 258 | +            fromaddr = 'eyegaze.kuet@gmail.com' | 
|  | 259 | +            toaddr = 'sakibreza1@gmail.com' | 
|  | 260 | +            msg = MIMEMultipart() | 
|  | 261 | +            msg['From'] = fromaddr | 
|  | 262 | +            msg['To'] = toaddr | 
|  | 263 | +            msg['Subject'] = 'Doctor Appointment' | 
|  | 264 | + | 
|  | 265 | +            body = 'I am facing problem.Please come to see me if you are free.' | 
|  | 266 | +            msg.attach(MIMEText(body, 'plain')) | 
|  | 267 | + | 
|  | 268 | +            server = smtplib.SMTP('smtp.gmail.com', 587) | 
|  | 269 | +            server.starttls() | 
|  | 270 | +            server.login(fromaddr, '060701cse') | 
|  | 271 | +            text = msg.as_string() | 
|  | 272 | +            server.sendmail(fromaddr, toaddr, text) | 
|  | 273 | +            server.quit() | 
|  | 274 | +            print('Email Sent Successfully') | 
|  | 275 | +        except Exception as e: | 
|  | 276 | +            print('Email not sent') | 
|  | 277 | +            print(e) | 
|  | 278 | + | 
|  | 279 | +    def playFan(self): | 
|  | 280 | +        print("playing Fan") | 
|  | 281 | +        self.chair.playFan() | 
|  | 282 | + | 
|  | 283 | +    def playLight(self): | 
|  | 284 | +        print("playing Light") | 
|  | 285 | +        self.chair.playLight() | 
|  | 286 | + | 
|  | 287 | +    def playBrowser(self): | 
|  | 288 | +        import webbrowser | 
|  | 289 | +        webbrowser.open_new_tab("http://epaperna.prothomalo.com/") | 
|  | 290 | + | 
|  | 291 | +    def playMusic(self): | 
|  | 292 | +        import os, random, subprocess | 
|  | 293 | +        randomfile = random.choice(os.listdir("F:\\music\\")) | 
|  | 294 | +        file = "F:\\music\\" + randomfile | 
|  | 295 | +        self.current_subprecess = subprocess.Popen(["C:\Program Files\Windows Media Player\wmplayer.exe", file]) | 
|  | 296 | + | 
|  | 297 | +    def playVideo(self): | 
|  | 298 | +        import os, random, subprocess | 
|  | 299 | +        randomfile = random.choice(os.listdir("F:\\video\\")) | 
|  | 300 | +        file = "F:\\video\\" + randomfile | 
|  | 301 | +        self.current_subprecess = subprocess.Popen(["C:\Program Files\Windows Media Player\wmplayer.exe", file]) | 
|  | 302 | + | 
|  | 303 | +    def moveFocusRight(self): | 
|  | 304 | +        if self.current_subprecess is None and self.current_mode != 1 and self.current_mode != 4: | 
|  | 305 | +            self.currentFocus = (self.currentFocus + 1) % 8 | 
|  | 306 | +            self.buttons[self.currentFocus].setFocus(True) | 
|  | 307 | + | 
|  | 308 | +    def moveFocusLeft(self): | 
|  | 309 | +        if self.current_subprecess is None and self.current_mode != 1 and self.current_mode != 4: | 
|  | 310 | +            self.currentFocus = (self.currentFocus - 1) % 8 | 
|  | 311 | +            self.buttons[self.currentFocus].setFocus(True) | 
|  | 312 | + | 
|  | 313 | +    def moveFocusUp(self): | 
|  | 314 | +        if  self.current_subprecess is None and self.current_mode != 1 and self.current_mode != 4: | 
|  | 315 | +            self.currentFocus = (self.currentFocus + 2) % 8 | 
|  | 316 | +            self.buttons[self.currentFocus].setFocus(True) | 
|  | 317 | + | 
|  | 318 | +    def moveFocusDown(self): | 
|  | 319 | +        if  self.current_subprecess is None and self.current_mode != 1 and self.current_mode != 4: | 
|  | 320 | +            self.currentFocus = (self.currentFocus - 2) % 8 | 
|  | 321 | +            self.buttons[self.currentFocus].setFocus(True) | 
|  | 322 | + | 
|  | 323 | +    def pressFocused(self): | 
|  | 324 | +        if self.current_subprecess is None and self.current_mode != 1 and self.current_mode != 4: | 
|  | 325 | +            self.buttons[self.currentFocus].animateClick() | 
|  | 326 | + | 
|  | 327 | + | 
|  | 328 | +def toQImage(raw_img): | 
|  | 329 | +    from numpy import copy | 
|  | 330 | +    img = copy(raw_img) | 
|  | 331 | +    qformat = QImage.Format_Indexed8 | 
|  | 332 | +    if len(img.shape) == 3: | 
|  | 333 | +        if img.shape[2] == 4: | 
|  | 334 | +            qformat = QImage.Format_RGBA8888 | 
|  | 335 | +        else: | 
|  | 336 | +            qformat = QImage.Format_RGB888 | 
|  | 337 | + | 
|  | 338 | +    outImg = QImage(img.tobytes(), img.shape[1], img.shape[0], img.strides[0], qformat) | 
|  | 339 | +    return outImg | 
0 commit comments