Exemple #1
0
    def run(self):
        lm = LineMaker()
        try:
            s = get_scanner()
            if s is None:
                raise ValueError()
        except Exception as e:
            print("Unable to init scanner, not starting viewer.")
            self.running = False

        while self.running:
            img = s.cap.get(1)
            if settings.ROTATE:
                img = np.rot90(img, settings.ROTATE)
            if self.line_mode:
                lineprocessor = getattr(lm,
                                        'from_' + settings.SEGMENTATION_METHOD)
                s.laser_on(0)
                laser_image = s.cap.get(1)
                if settings.ROTATE:
                    laser_image = np.rot90(laser_image, settings.ROTATE)
                s.laser_off(0)
                points, processed = lineprocessor(laser_image,
                                                  laser_image[:, :, 0], img,
                                                  img[:, :, 0])
                img = processed
            else:
                grey = img[:, :, 1]
                found, corners = chess_detect(grey)
                if found:
                    img = chess_draw(grey, found, corners)
            gui.display(img, "live", resize=True)
Exemple #2
0
    async def viewer(self):
        lm = LineMaker()
        try:
            s = get_scanner()
            if s is None:
                raise ValueError()
        except Exception as e:
            print("Unable to init scanner, not starting viewer.")
            self.stop()
            return

        def process_image():
            img = s.cap.get(-1)
            if settings.ROTATE:
                img = np.ascontiguousarray(np.rot90(img, settings.ROTATE))
            if self.line_mode:
                lineprocessor = getattr(lm,
                                        'from_' + settings.SEGMENTATION_METHOD)
                s.lasers_on()
                laser_image = s.cap.get(1)
                if settings.ROTATE:
                    laser_image = np.ascontiguousarray(
                        np.rot90(laser_image, settings.ROTATE))
                s.lasers_off()
                points, processed = lineprocessor(laser_image,
                                                  laser_image[:, :, 0], img,
                                                  img[:, :, 0])
                if processed is None:
                    pass  # img = black picture ??
                else:
                    img = processed
            else:
                grey = img[:, :, 1]
                found, corners = chess_detect(grey)
                if found:
                    chess_draw(img, found, corners)
            return img

        while self.running:
            if self.visible:
                img = await run_in_thread(process_image)
                # process display
                gui.display(img, "live", resize=True)
            try:
                await self.wait_interval()
            except CancelledError:
                return
Exemple #3
0
 def disp(img, text):
     if settings.ROTATE:
         img = np.rot90(img, settings.ROTATE)
     gui.display(img, text=text, resize=True)
Exemple #4
0
def calibration(calibration_data, images):
    obj_points = []
    img_points = []
    found_nr = 0

    failed_serie = 0
    term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
    flags = cv2.CALIB_CB_FAST_CHECK
    pattern_points = settings.get_pattern_points()

    temp_calibration_data = defaultdict(lambda: {})

    for idx, fn in enumerate(images):
        gui.progress('Webcam calibration %s (%d found)... ' % (fn, found_nr), idx, len(images))
        img, hsv = imtools.imread(fn, format="full")
        grey = hsv[:,:,2]

        if img is None:
            print("Failed to load", fn)
            continue

        w, h = img.shape[:2]

        found, corners = chess_detect(grey, flags)

        if not found:
            if found_nr > 20 and failed_serie > 6:
                break
            failed_serie += 1
            continue

        failed_serie = 0
        found_nr += 1
        cv2.cornerSubPix(grey, corners, (11, 11), (-1, -1), term)
        temp_calibration_data[fn]['chess_corners'] = corners
        img_points.append(corners)
        obj_points.append(pattern_points)

        # compute mask coordinates
        p1 = corners[0][0]
        p2 = corners[settings.PATTERN_MATRIX_SIZE[0] - 1][0]
        p3 = corners[settings.PATTERN_MATRIX_SIZE[0] * (settings.PATTERN_MATRIX_SIZE[1] - 1)][0]
        p4 = corners[settings.PATTERN_MATRIX_SIZE[0] * settings.PATTERN_MATRIX_SIZE[1] - 1][0]
        temp_calibration_data[fn]['chess_contour'] = np.array([p1, p2, p4, p3], dtype='int32')

        if idx%settings.ui_base_i == 0:
            chess_draw(img, found, corners)
            gui.display(img, 'chess', resize=True)

    if settings.skip_calibration:
        print("\nskipping camera calibration...")
        try:
            settings.load_data(calibration_data)
        except Exception:
            # Data for Logitech C270
            calibration_data.camera_matrix = np.array(([1430.0, 0.0, 480.0], [0.0, 1430.0, 620.0], [0.0, 0.0, 1.0]))
            calibration_data.distortion_vector = np.array((0.0, 0.0, 0.0, 0.0, 0.0))
        return temp_calibration_data

    print("\nComputing camera calibration...")

    if not obj_points:
        raise ValueError("Unable to detect pattern on screen :(")

    rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, grey.shape, None, None)
    if rms:
        error = 0
        # Compute calibration error
        for i in range(len(obj_points)):
            imgpoints2, _ = cv2.projectPoints(obj_points[i], rvecs[i], tvecs[i], camera_matrix, dist_coefs)
            error += abs(
                    cv2.norm(img_points[i])
                    - cv2.norm(imgpoints2)
                    )
        error /= len(obj_points)
        print("Camera calibration error = %.4fmm"%error)

    calibration_data.camera_matrix = camera_matrix
    calibration_data.distortion_vector = dist_coefs.ravel()

    settings.save_data(calibration_data)
    return temp_calibration_data
Exemple #5
0
def iter_cloudify(calibration_data,
                  folder,
                  lasers,
                  sequence,
                  method=None,
                  camera=False,
                  interactive=False,
                  undistort=False):
    pure_images = settings.pure_mode
    lm = LineMaker()
    lineprocessor = getattr(lm, 'from_' + method)
    lm.calibration_data = calibration_data

    sliced_lines = defaultdict(lambda: [None, None])
    color_slices = defaultdict(lambda: [None, None])

    d_kern = np.ones((3, 3), np.uint8)

    RED = 2  # position of red layer

    for i, n in enumerate(sequence):
        yield

        fullcolor = imtools.imread(folder + '/color_%03d.%s' %
                                   (n, settings.FILEFORMAT),
                                   format="rgb",
                                   calibrated=undistort and calibration_data)
        if fullcolor is None:
            continue

        if pure_images:
            ref_grey = None
        else:
            ref_grey = fullcolor[:, :, RED]

        pictures_todisplay = []

        for laser in lasers:
            laser_image = imtools.imread(
                folder + '/laser%d_%03d.%s' % (laser, n, settings.FILEFORMAT),
                format="rgb",
                calibrated=undistort and calibration_data)

            if laser_image is None:
                continue

            laser_grey = laser_image[:, :, RED]

            gui.progress("analyse", i, len(sequence))
            points, processed = lineprocessor(
                laser_image,
                laser_grey,
                fullcolor,
                ref_grey,
                laser_nr=laser,
                mask=camera[i]['chess_contour'] if camera else None)

            # validate & store
            if points is not None and points[0].size:
                nosave = False
                if interactive:
                    disp = cv2.merge(
                        np.array((laser_grey, processed, processed)))
                    txt = "Esc=NOT OK, Enter=OK"
                    gui.display(disp, txt, resize=True)
                pictures_todisplay.append((processed, laser_grey))
                if interactive:
                    if not gui.ok_cancel(20):
                        nosave = True

                if not interactive or not nosave:
                    if camera:
                        sliced_lines[n][laser] = [points] + camera[i]['plane']
                    else:
                        sliced_lines[n][laser] = [np.deg2rad(n), points, laser]
                        if fullcolor is not None:
                            color_slices[n][laser] = np.fliplr(
                                fullcolor[(points[1], points[0])])

        # display
        if i % int(settings.ui_base_i * 2) == 0 and pictures_todisplay:
            if DEBUG:
                if len(pictures_todisplay) > 1:
                    pictures_todisplay = np.array(pictures_todisplay)
                    gref = cv2.addWeighted(pictures_todisplay[0, 1], 0.5,
                                           pictures_todisplay[1, 1], 0.5, 0)
                    nref = cv2.addWeighted(pictures_todisplay[0, 0], 0.5,
                                           pictures_todisplay[1, 0], 0.5, 0)
                else:
                    gref = pictures_todisplay[0][1]
                    nref = pictures_todisplay[0][0]

                nref = cv2.dilate(nref, d_kern).astype(np.uint8)
                r = cv2.bitwise_or(gref, nref)
                disp = cv2.merge(np.array((r, gref, r)))

                gui.display(disp,
                            "lasers" if len(lasers) > 1 else "laser %d" %
                            lasers[0],
                            resize=True)
            else:
                if len(pictures_todisplay) > 1:
                    gui.display(cv2.addWeighted(pictures_todisplay[1][1], 0.5,
                                                pictures_todisplay[0][1], 0.5,
                                                0),
                                "lasers" if len(lasers) > 1 else "laser %d" %
                                lasers[0],
                                resize=True)
                else:
                    gui.display(pictures_todisplay[0][1],
                                "lasers" if len(lasers) > 1 else "laser %d" %
                                lasers[0],
                                resize=True)
        else:
            gui.redraw()
    if len(sliced_lines) == 0:
        return None
    if camera:
        yield sliced_lines
    else:
        yield sliced_lines, color_slices
Exemple #6
0
def iter_cloudify(calibration_data,
                  folder,
                  lasers,
                  sequence,
                  method=None,
                  camera=False,
                  interactive=False,
                  undistort=False):
    print('H-10')  ###
    pure_images = settings.pure_mode
    lm = LineMaker()
    lineprocessor = getattr(lm, 'from_' + method)
    lm.calibration_data = calibration_data

    print('H-11')  ###
    sliced_lines = defaultdict(lambda: [None, None])
    color_slices = defaultdict(lambda: [None, None])

    d_kern = np.ones((3, 3), np.uint8)

    RED = 2  # position of red layer

    print('H-12')  ###
    for i, n in enumerate(sequence):
        print('H-10')  ###
        yield

        print(folder + '/color_%03d.%s' % (n, settings.FILEFORMAT))

        fullcolor = imtools.imread(folder + '/color_%03d.%s' %
                                   (n, settings.FILEFORMAT),
                                   format="rgb",
                                   calibrated=undistort and calibration_data)
        if fullcolor is None:
            continue

        print('H-13')  ###
        if pure_images:
            ref_grey = None
        else:
            ref_grey = fullcolor[:, :, RED]

        pictures_todisplay = []

        print('H-14')  ###
        for laser in lasers:
            print('H-14a')  ###
            print(undistort)
            print(calibration_data)
            print(undistort and calibration_data)
            laser_image = imtools.imread(
                folder + '/laser%d_%03d.%s' % (laser, n, settings.FILEFORMAT),
                format="rgb",
                calibrated=undistort and calibration_data)
            print('H-14b')  ###

            if laser_image is None:
                print('H-14c')  ###
                continue

            laser_grey = laser_image[:, :, RED]
            print('H-14d')  ###

            gui.progress("analyse", i, len(sequence))
            print('H-14e')  ###
            points, processed = lineprocessor(
                laser_image,
                laser_grey,
                fullcolor,
                ref_grey,
                laser_nr=laser,
                mask=camera[i]['chess_contour'] if camera else None)
            print('H-14f')  ###

            # validate & store
            print('H-14g')  ###
            if points is not None and points[0].size:
                print('H-14h')  ###
                nosave = False
                if interactive:
                    print('H-14i')  ###
                    disp = cv2.merge(
                        np.array((laser_grey, processed, processed)))
                    txt = "Esc=SKIP, Space=OK"
                    gui.display(disp, txt, resize=True)
                pictures_todisplay.append((processed, laser_grey))
                if interactive:
                    print('H-14j')  ###
                    # detect the chess board
                    term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER,
                            200, 0.001)
                    found, corners = cv2.findChessboardCorners(
                        ref_grey, settings.PATTERN_MATRIX_SIZE)
                    if found:
                        if not gui.ok_cancel(20):
                            nosave = True
                    else:
                        nosave = True

                if not interactive or not nosave:
                    print('H-14k')  ###
                    if camera:
                        print('H-14l')  ###
                        sliced_lines[n][laser] = [points] + camera[i]['plane']
                    else:
                        print('H-14m')  ###
                        sliced_lines[n][laser] = [np.deg2rad(n), points, laser]
                        if fullcolor is not None:
                            print('H-14n')  ###
                            color_slices[n][laser] = np.fliplr(
                                fullcolor[(points[1], points[0])])

        # display
        print('H-15')  ###
        if i % int(settings.ui_base_i * 2) == 0 and pictures_todisplay:
            if DEBUG:
                if len(pictures_todisplay) > 1:
                    pictures_todisplay = np.array(pictures_todisplay)
                    gref = cv2.addWeighted(pictures_todisplay[0, 1], 0.5,
                                           pictures_todisplay[1, 1], 0.5, 0)
                    nref = cv2.addWeighted(pictures_todisplay[0, 0], 0.5,
                                           pictures_todisplay[1, 0], 0.5, 0)
                else:
                    gref = pictures_todisplay[0][1]
                    nref = pictures_todisplay[0][0]

                nref = cv2.dilate(nref, d_kern).astype(np.uint8)
                r = cv2.bitwise_or(gref, nref)
                disp = cv2.merge(np.array((r, gref, r)))

                gui.display(disp,
                            "lasers" if len(lasers) > 1 else "laser %d" %
                            lasers[0],
                            resize=True)
            else:
                if len(pictures_todisplay) > 1:
                    gui.display(cv2.addWeighted(pictures_todisplay[1][1], 0.5,
                                                pictures_todisplay[0][1], 0.5,
                                                0),
                                "lasers" if len(lasers) > 1 else "laser %d" %
                                lasers[0],
                                resize=True)
                else:
                    gui.display(pictures_todisplay[0][1],
                                "lasers" if len(lasers) > 1 else "laser %d" %
                                lasers[0],
                                resize=True)
        else:
            gui.redraw()
    print('H-16')  ###
    if len(sliced_lines) == 0:
        print('H-17')  ###
        raise AnalyseError("Unable to recognize lines in picture")
    if camera:
        yield sliced_lines
    else:
        yield sliced_lines, color_slices