def main():
    cap = cv2.VideoCapture(0)
    # adjust streaming params, more can be found here:
    # https://stackoverflow.com/questions/11420748/setting-camera-parameters-in-opencv-python

    cap.set(3, 1280)  # Width of the frames in the video stream
    cap.set(4, 1024)  # Height of the frames in the video stream
    cap.set(15, -8.0)  # reduce exposure to limit noise light
    light_sequence = [0]
    pulse_instance = 0

    while True:
        _, frame = cap.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # apply blurring effect to reduce high frequency noise
        blurred = cv2.GaussianBlur(gray, (11, 11), 0)

        # apply threshold to convert images to binary
        thresh = cv2.threshold(blurred, 240, 255, cv2.THRESH_BINARY)[1]

        # apply erosions and dilations to reduce sparse spots
        thresh = cv2.erode(thresh, None, iterations=2)
        thresh = cv2.dilate(thresh, None, iterations=4)

        # find the curve of object's boundary
        cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)
        cnts = cnts[0] if len(cnts) == 2 else cnts[1]

        # create filter conditions for the object's shape and size
        min_area = 100
        total_light_area = 0

        for c in cnts:
            perimeter = cv2.arcLength(c, True)
            area = cv2.contourArea(c)
            circularity = 4 * math.pi * (area / (perimeter * perimeter))

            if area > min_area and 0.6 < circularity < 1.2:
                cv2.drawContours(frame, [c], -1, (128, 255, 0), 2)
                total_light_area += area

        # log the light pulses as positive int and dark periods as 0s
        if total_light_area > 0:
            light_sequence[-1] = light_sequence[-1] + 1
        else:
            light_sequence.append(0)

        # translate signal into text
        decoded = light_decoder(light_sequence)
        clean_morse = convertMorseToText(morse_cleaner(decoded))

        # annotate predicted texts on video stream
        cv2.putText(frame, "Message: " + clean_morse, (10, 470),
                    cv2.FONT_HERSHEY_DUPLEX, 0.7, (52, 152, 219), 2)

        cv2.imshow("Frame", frame)

        key = cv2.waitKey(1)
        if key == 27:
            break
    def calculate(self,frame):
        frame = imutils.resize(frame, width=640)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        subjects = self.detect(gray, 0)
        for subject in subjects:
            shape = self.predict(gray, subject)
            shape = face_utils.shape_to_np(shape)  # converting to NumPy Array
            leftEye = shape[self.lStart:self.lEnd]
            rightEye = shape[self.rStart:self.rEnd]
            leftEAR = self.eye_aspect_ratio(leftEye)
            rightEAR = self.eye_aspect_ratio(rightEye)
            ear = (leftEAR + rightEAR) / 2.0
            leftEyeHull = cv2.convexHull(leftEye)
            rightEyeHull = cv2.convexHull(rightEye)
            cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
            cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)
            if ear < self.thresh:  # closed eyes
                self.flag += 1
                self.pts.appendleft(self.flag)
                self.openEye = 0
            else:
                self.openEye += 1
                self.flag = 0
                self.pts.appendleft(self.flag)
            for i in range(1, len(self.pts)):
                if self.pts[i] > self.pts[i - 1]:
                    # print(pts[i - 1], pts[i])
                    if self.pts[i] > 30 and self.pts[i] < 60:
                        print("Eyes have been closed for 50 frames! - Print '-'")
                        log("Eyes have been closed for 50 frames!")
                        self.L.append("-")
                        self.pts = deque(maxlen=512)
                        break
                    elif self.pts[i] > 15 and self.pts[i] < 30:
                        print("Eyes have been closed for 20 frames!")
                        log("Eyes have been closed for 20 frames! - Print '.'")
                        self.L.append(".")
                        self.pts = deque(maxlen=512)
                        break

                    elif self.pts[i] > 60:
                        print("Eyes have been closed for 60 frames!")
                        log("Eyes have been closed for 60 frames! - Remove morse character")
                        self.L.pop()
                        self.pts = deque(maxlen=512)
                        break

        if (self.L != []):

            print(self.L)
        if self.openEye > 60:
            if (self.L != []):
                print(self.L)
            self.str = convertMorseToText(''.join(self.L))

            if self.str != None:
                print(self.str)
                self.finalString.append(self.str)
                self.final = ''.join(self.finalString)
            if self.str == None:
                self.L = []
            self.L = []
        cv2.putText(frame, "Predicted :  " + self.final, (10, 470),
                    cv2.FONT_HERSHEY_DUPLEX, 0.7, (52, 152, 219), 2)

        return frame
示例#3
0
    def calculate(self, frame):
        frame = imutils.resize(
            frame, width=640)  #resize of image to the corresponding value
        gray = cv2.cvtColor(
            frame, cv2.COLOR_BGR2GRAY
        )  #Converts an image from one color space to another
        subjects = self.detect(gray, 0)
        for subject in subjects:
            shape = self.predict(gray, subject)
            shape = face_utils.shape_to_np(shape)  # converting to NumPy Array
            leftEye = shape[self.lStart:self.lEnd]
            rightEye = shape[self.rStart:self.rEnd]
            leftEAR = self.eye_aspect_ratio(leftEye)
            rightEAR = self.eye_aspect_ratio(rightEye)
            ear = (leftEAR + rightEAR) / 2.0
            leftEyeHull = cv2.convexHull(
                leftEye)  #draw the hull around the eye
            rightEyeHull = cv2.convexHull(rightEye)
            cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0),
                             1)  #contour drawing around the left and right eye
            cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)
            if ear < self.thresh:  # closed eyes
                self.flag += 1
                self.pts.appendleft(self.flag)
                self.openEye = 0
            else:
                self.openEye += 1
                self.flag = 0
                self.pts.appendleft(self.flag)
            for i in range(1, len(self.pts)):
                if self.pts[i] > self.pts[i - 1]:
                    # print(pts[i - 1], pts[i])
                    # if self.pts[i] > 30 and self.pts[i] < 60:
                    if self.pts[i] > 26 and self.pts[i] < 40:
                        print(
                            "Eyes have been closed for 30 frames! - Print '-'")
                        log("Eyes have been closed for 30 frames!")
                        self.L.append("-")
                        self.pts = deque(maxlen=512)
                        break
                    # elif self.pts[i] > 15 and self.pts[i] < 30:
                    elif self.pts[i] > 10 and self.pts[i] < 25:
                        print("Eyes have been closed for 15 frames!")
                        log("Eyes have been closed for 15 frames! - Print '.'")
                        self.L.append(".")
                        self.pts = deque(maxlen=512)
                        break

                    # elif self.pts[i] > 60:
                    elif self.pts[i] > 40:
                        print("Eyes have been closed for 40 frames!")
                        log("Eyes have been closed for 40 frames! - Remove morse character"
                            )
                        if len(self.L) > 0:
                            self.L.pop()
                        else:
                            print("Nothing to remove")
                            log("Nothing to remove")
                        self.pts = deque(maxlen=512)
                        break

        if (self.L != []):

            print(self.L)
        print("ADARSH")
        if self.openEye > 50:
            if (self.L != []):
                print(self.L)
            self.str = convertMorseToText(''.join(self.L))

            if self.str != None:
                print(self.str)
                self.finalString.append(self.str)
                self.final = ''.join(self.finalString)
            if self.str == None:
                self.L = []
            self.L = []
        cv2.putText(frame, "Predicted :  " + self.final, (10, 470),
                    cv2.FONT_HERSHEY_DUPLEX, 0.7, (52, 152, 219), 2)

        return frame
示例#4
0
    def calculate(self, frame):

        decoded = cv2.imdecode(np.frombuffer(frame, np.uint8), -1)
        frame = imutils.resize(decoded, width=640)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        subjects = self.detect(gray, 0)
        for subject in subjects:
            shape = self.predict(gray, subject)
            shape = face_utils.shape_to_np(shape)  # converting to NumPy Array
            leftEye = shape[self.lStart:self.lEnd]
            rightEye = shape[self.rStart:self.rEnd]
            leftEAR = self.eye_aspect_ratio(leftEye)
            rightEAR = self.eye_aspect_ratio(rightEye)
            ear = (leftEAR + rightEAR) / 2.0
            leftEyeHull = cv2.convexHull(leftEye)
            rightEyeHull = cv2.convexHull(rightEye)
            cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
            cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)
            if ear < self.thresh:  # closed eyes
                self.flag += 1
                self.pts.appendleft(self.flag)
                self.openEye = 0
            else:
                self.openEye += 1
                self.flag = 0
                self.pts.appendleft(self.flag)
            for i in range(1, len(self.pts)):
                if self.pts[i] > self.pts[i - 1]:
                    # print(pts[i - 1], pts[i])
                    if self.pts[i] > 30 and self.pts[i] < 60:
                        print(
                            "Eyes have been closed for 50 frames! - Print '-'")
                        log("Eyes have been closed for 50 frames!")
                        self.L.append("-")
                        self.pts = deque(maxlen=512)
                        break
                    elif self.pts[i] > 15 and self.pts[i] < 30:
                        print("Eyes have been closed for 20 frames!")
                        log("Eyes have been closed for 20 frames! - Print '.'")
                        self.L.append(".")
                        self.pts = deque(maxlen=512)
                        break

                    elif self.pts[i] > 60:
                        print("Eyes have been closed for 60 frames!")
                        log("Eyes have been closed for 60 frames! - Remove morse character"
                            )
                        self.L.pop()
                        self.pts = deque(maxlen=512)
                        break

        if (self.L != []):

            print(self.L)
        if self.openEye > 60:
            if (self.L != []):
                print(self.L)
            self.str = convertMorseToText(''.join(self.L))

            if self.str != None:
                print(self.str)
                self.finalString.append(self.str)
                self.final = ''.join(self.finalString)
            if self.str == None:
                self.L = []
            self.L = []
        cv2.putText(frame, "Predicted :  " + self.final, (10, 470),
                    cv2.FONT_HERSHEY_DUPLEX, 0.7, (52, 152, 219), 2)
        ret, png = cv2.imencode('.png', frame)
        return png, self.L
示例#5
0
def main():
    cap = cv2.VideoCapture(0)
    flag = 0
    openEye = 0
    final = ''
    str = ''
    finalString = []
    L = []
    closed = False
    timer = 0
    pts = deque(maxlen=512)

    while True:
        ret, frame = cap.read()
        #cam1 = Camera()
        #frame = cam1.get_frame()
        frame = imutils.resize(frame, width=640)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        subjects = detect(gray, 0)
        for subject in subjects:
            shape = predict(gray, subject)
            shape = face_utils.shape_to_np(shape)  # converting to NumPy Array
            leftEye = shape[lStart:lEnd]
            rightEye = shape[rStart:rEnd]
            leftEAR = eye_aspect_ratio(leftEye)
            rightEAR = eye_aspect_ratio(rightEye)
            ear = (leftEAR + rightEAR) / 2.0
            leftEyeHull = cv2.convexHull(leftEye)
            rightEyeHull = cv2.convexHull(rightEye)
            cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
            cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)
            if ear < thresh:  #closed eyes
                flag += 1
                pts.appendleft(flag)
                openEye = 0
            else:
                openEye += 1
                flag = 0
                pts.appendleft(flag)
            for i in range(1, len(pts)):
                if pts[i] > pts[i - 1]:
                    #print(pts[i - 1], pts[i])
                    if pts[i] > 30 and pts[i] < 60:
                        print("Eyes have been closed for 50 frames!")
                        L.append("-")
                        pts = deque(maxlen=512)
                        break
                    elif pts[i] > 15 and pts[i] < 30:
                        print("Eyes have been closed for 20 frames!")
                        L.append(".")
                        pts = deque(maxlen=512)
                        break

                    elif pts[i] > 60:
                        print("Eyes have been closed for 60 frames!")
                        L.pop()
                        pts = deque(maxlen=512)
                        break

        if (L != []):
            print(L)
        if openEye > 60:
            if (L != []):
                print(L)
            str = convertMorseToText(''.join(L))

            if str != None:
                print(str)
                finalString.append(str)
                final = (''.join(finalString))
            if str == None:
                L = []
            L = []
        cv2.putText(frame, "Predicted :  " + final, (10, 470),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
        cv2.imshow("Frame", frame)
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break
    cv2.destroyAllWindows()
    cap.stop()