Exemplo n.º 1
0
def main(cam, show, pseye):
    global processing
    if pseye == 1:
        cap = Camera([0], fps=60, resolution=Camera.RES_LARGE, colour=False)
        frame, timestamp = cap.read()
        # width, height = frame.shape
    else:
        cap = cv2.VideoCapture(cam)
    sz = 80
    run = True
    vals = {}

    tiles = []

    mz = cv2.imread("live_full_mz/live_full_mz.png")
    c = 1
    last_diff = 0

    # cv2.namedWindow("mz", cv2.WND_PROP_FULLSCREEN)
    # cv2.setWindowProperty("mz", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)

    if show == 0:
        window = False
    else:
        window = True
    while (run):
        try:
            if pseye == 1:
                frame, timestamp = cap.read()
            else:
                ret, frame = cap.read()
            small = cv2.resize(frame, (0, 0), fx=0.18, fy=0.18)
            width, height = small.shape
            ret, thr = cv2.threshold(small, 1, 255,
                                     cv2.THRESH_BINARY + cv2.THRESH_OTSU)
            kernel = np.ones((1, 1), np.uint8)
            opening = cv2.morphologyEx(thr,
                                       cv2.MORPH_OPEN,
                                       kernel,
                                       iterations=2)
            bg = cv2.dilate(opening, kernel, iterations=3)

            if not processing:
                processing = True
                tiles = processTiles(width, height, sz, bg)
                for tile in tiles:
                    tile = np.array(tile)

            if window:
                # cv2.imshow('cam', bg)
                for i, tile in enumerate(tiles):
                    cv2.imshow(('%s' % i), tile)

            k = cv2.waitKey(1)
            if k == ord('q'):
                run = False
            c += 1
        except Exception as e:
            print(e)
            pass
    # program closing, stop camera stream and close windows
    if pseye == 1:
        cap.end()
    else:
        cap.release()
    cv2.destroyAllWindows()
Exemplo n.º 2
0
def main(cam, show, pseye):
    if pseye == 1:
        cap = Camera([0], fps=60, resolution=Camera.RES_LARGE, colour=False)
        # frame, timestamp  = cap.read()
        # width, height = frame.shape
    else:
        cap = cv2.VideoCapture(cam)
        # width = floor(cap.get(3)) // 2
        # height = floor(cap.get(4)) // 2
    # sz = 100
    run = True
    # vals = {}
    mz = cv2.imread("live_full_mz/live_full_mz.png")
    # mz_sm =  cv2.resize(mz, (0,0), fx=0.3, fy=0.3)
    global processing
    c = 1
    # last_diff = 0

    cv2.namedWindow("mz", cv2.WND_PROP_FULLSCREEN)
    cv2.setWindowProperty("mz", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
    cv2.setWindowProperty("mz", cv2.WND_PROP_ASPECT_RATIO,
                          cv2.WINDOW_FREERATIO)

    if show == 0:
        window = False
    else:
        window = True
    while (run):
        try:
            if pseye == 1:
                frame, timestamp = cap.read()
                # width, height = frame.shape
            else:
                ret, frame = cap.read()

            frame = frame[100:320, 100:540]
            small = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
            # gray = cv2.cvtColor(small,cv2.COLOR_BGR2GRAY)
            ret, thr = cv2.threshold(small, 1, 255,
                                     cv2.THRESH_BINARY + cv2.THRESH_OTSU)
            kernel = np.ones((1, 1), np.uint8)
            opening = cv2.morphologyEx(thr,
                                       cv2.MORPH_OPEN,
                                       kernel,
                                       iterations=1)
            bg = cv2.dilate(opening, kernel, iterations=2)

            # if c < 50:
            if not processing:
                # diff = detect_diffs_two(vals, c, width, height, sz, bg)
                # print(diff - last_diff)
                # d_sum = diff - last_diff
                # if d_sum > 0.005 or d_sum < -0.005:
                processing = True
                mz = processFrame(bg)
                try:
                    mz = np.array(mz)
                    # mz = cv2.resize(mz, (0, 0), fx=4, fy=4)
                except Exception as e:
                    print(e)
                    pass
                    # mz = cv2.imread("live_full_mz/live_full_mz.png")

                    # last_diff = diff
                    # mz_sm =  cv2.resize(mz, (0,0), fx=0.3, fy=0.3)
                    # mz - cv2.imread("live_full/", 1)
                    #  detect_diffs(vals, c, width, height, sz, bg)
                    # print("----")

            if window:
                try:
                    # cv2.imshow('cam', bg)
                    cv2.imshow('mz', mz)
                except Exception as e:
                    print(e)
                    pass
            k = cv2.waitKey(1)
            # if k == ord('+'):
            #     mz = cv2.resize(mz, (0,0), fx=1.1, fy=1.1)
            # if k == ord('-'):
            #     mz = cv2.resize(mz, (0,0), fx=0.9, fy=0.9)
            if k == ord('q'):
                run = False
            c += 1
        except Exception as e:
            print(e)
            pass
    if pseye == 1:
        cap.end()
    else:
        cap.release()
    cv2.destroyAllWindows()