Ejemplo n.º 1
0
    def run(self, inputs):
        img_hsv = inputs.img.hsv
        # cvw.bgr_to_hsv(inputs.img)
        diff_hsv = img_hsv.abs_diff(
            np.array(
                [self.settings.hue, self.settings.sat, self.settings.val],
                dtype=np.float,
            )
            [None],  # [None] adds a dimension to the ndarray object created by np.array() -
            # See https://stackoverflow.com/questions/37867354/in-numpy-what-does-selection-by-none-do
        )

        scaled_diff = np.multiply(
            diff_hsv,
            np.array(
                [
                    self.settings.hue_sensitivity,
                    self.settings.sat_sensitivity,
                    self.settings.val_sensitivity,
                ],
                dtype=np.uint16,
            ),
        ).astype(np.uint16)

        greyscale = Mat(scaled_diff).greyscale

        if self.settings.clamp_max:
            greyscale = Mat(
                np.minimum(greyscale.img,
                           self.settings.clamp_value).astype(np.uint8))
        else:
            greyscale = Mat(np.minimum(greyscale.img, 255).astype(np.uint8))

        return self.Outputs(img=greyscale)
Ejemplo n.º 2
0
    def run(self, inputs):
        def hue_dist(test: int, reference: int):
            return min(abs(reference - test), abs(reference + 360 - test))

        color_hue = (Mat(
            np.uint8([[[
                inputs.color.blue, inputs.color.green, inputs.color.red
            ]]])).hsv.img[0][0][0] * 2)

        hue_strings = {
            self.settings.red_hue: "R",
            self.settings.yellow_hue: "Y",
            self.settings.green_hue: "G",
            self.settings.blue_hue: "B",
        }

        output_str = ""

        min_dist = 360

        for hue in hue_strings.keys():
            dist = hue_dist(hue, color_hue)
            if dist < min_dist:
                min_dist = dist
                output_str = hue_strings[hue]

        return self.Outputs(color_string=output_str)
Ejemplo n.º 3
0
    def run(self, inputs):
        if len(inputs.contours.l) == 0:
            return self.Outputs(center=None, success=False, visual=inputs.img)
        res = inputs.contours.l[0].res
        center = inputs.contours.centroid_of_all

        if self.settings.draw:
            img = np.copy(inputs.img.mat.img)

            for contour in inputs.contours.l:
                cv2.circle(
                    img,
                    (int(contour.pixel_centroid[0]), int(contour.pixel_centroid[1])),
                    5,
                    (0, 0, 255),
                    3,
                )

            cv2.circle(img, (int(center.x), int(center.y)), 10, (255, 0, 0), 5)
            img = Mat(img)
        else:
            img = None

        scaled_center = Point(
            x=((center.x * 2) / res.x) - 1, y=((center.y * 2) / res.y) - 1
        )

        return self.Outputs(center=scaled_center, success=True, visual=img)
Ejemplo n.º 4
0
    def run(self, inputs):
        img = inputs.img.mat.img
        mask = inputs.mask.matBW.img

        out = Mat(cv2.bitwise_and(np.copy(img), img, mask))

        return self.Outputs(img=out)
Ejemplo n.º 5
0
    def run(self, inputs):
        draw = np.copy(inputs.img.mat.img)

        # Draw the outline of the contours
        cv2.drawContours(draw, inputs.contours.raw, -1, (255, 255, 0), 2)

        # Draw the non-rotated rectangle bounding each contour
        if self.settings.bounding_rect:
            for contour in inputs.contours.l:
                rect = contour.to_rect
                cv2.rectangle(
                    draw,
                    (int(rect.tl.x), int(rect.tl.y)),  # Top left coord
                    (
                        int(rect.tl.x + rect.dim.x),
                        int(rect.tl.y + rect.dim.y),
                    ),  # Bottom right coord
                    (0, 0, 255),
                    2,
                )

        # Draw the smallest possible (rotated) rectangle bounding each contour
        if self.settings.min_area_rect:
            for contour in inputs.contours.l:
                points = np.int0(contour.to_min_area_rect.box_points)
                cv2.drawContours(draw, [points], -1, (0, 255, 255), 2)

        draw = Mat(draw)
        return self.Outputs(img=draw)
Ejemplo n.º 6
0
    def run(self, inputs):
        # Find the pixel coordinates to sample in the image
        height, width = inputs.img.img.shape[:2]

        sample_coords = (
            int(width * self.settings.x_pct / 100.0 + 10),
            int(height * self.settings.y_pct / 100.0 + 10),
        )
        color_bgr = inputs.img.img[sample_coords[1], sample_coords[0]]
        draw = None
        if self.settings.draw_color:
            draw = np.copy(inputs.img.mat.img)
            # Draw a small circle (of radius 5) to show the point.
            cv2.circle(draw, sample_coords, 5, (0, 0, 255), 3)

            # Find the color in HSV to make a contrasting color
            color_hsv = Mat(np.uint8([[color_bgr]])).img[0][0]

            color_hsv[0] *= 2  # Scale the hue value to be in a range of 0-359

            # Create a string to represent the color in either RGB or HSV
            if self.settings.draw_hsv:
                color_str = "H{} S{} V{}".format(*color_hsv)
            else:
                color_str = "B{} G{} R{}".format(*color_bgr)

            # Choose a (Hopefully) Contrasting color
            draw_color = (
                int(255 - color_bgr[0]),
                int(255 - color_bgr[1]),
                int(255 - color_bgr[2]),
            )

            cv2.putText(
                draw,
                color_str,
                (sample_coords[0] + 10, sample_coords[1] + 10),
                cv2.FONT_HERSHEY_SIMPLEX,
                0.5,
                draw_color,
                lineType=cv2.LINE_AA,
            )

            draw = Mat(draw)

        color = Color(color_bgr[2], color_bgr[1], color_bgr[0])
        return self.Outputs(color=color, img=draw)
Ejemplo n.º 7
0
    def run(self, inputs):
        if inputs.img is None:  # Do not write None values to NT
            return self.Outputs()

        self.gpu_blur.update_radius(self.settings.radius)

        processed = self.gpu_blur.apply(inputs.img.img)

        return self.Outputs(Mat(processed))
Ejemplo n.º 8
0
 def on_start(self):
     camNum = parse_cammode(self.settings.mode)[0]
     if not UndupeInstance.add(camNum):
         raise ValueError(f"Camera {camNum} already in use")
     self.cap = create_capture(self.settings)
     ret, frame = self.cap.read()  # test for errors
     try:
         Mat(frame)
     except Exception:
         raise ValueError(f"Unable to read picture from Camera {camNum}")
Ejemplo n.º 9
0
    def run(self, inputs):
        # If there are no circles return the input image
        if inputs.lines is None:
            return self.Outputs(img=inputs.img)

        draw = np.copy(inputs.img.mat.img)

        for line in inputs.lines:
            x1, y1, x2, y2 = line[0]
            cv2.line(draw, (x1, y1), (x2, y2), (255, 0, 0), 3)

        draw = Mat(draw)
        return self.Outputs(img=draw)
Ejemplo n.º 10
0
    def run(self, inputs):
        diff = inputs.img.abs_diff(
            np.array(
                [self.settings.blue, self.settings.green, self.settings.red],
                dtype=np.float,
            )[None], )

        if self.settings.to_greyscale:
            diff = diff.greyscale

        if self.settings.clamp_max:
            diff = Mat(np.minimum(diff.img, self.settings.clamp_value))

        return self.Outputs(img=diff)
Ejemplo n.º 11
0
 def run(self, inputs):
     fps_str = "{:.1f}".format(HookInstance.get_fps())
     draw = np.copy(inputs.img.mat.img)
     cv2.putText(
         draw,
         fps_str,
         (30, 30),
         cv2.FONT_HERSHEY_SIMPLEX,
         1.0,
         (255, 255, 255),
         lineType=cv2.LINE_AA,
     )
     draw = Mat(draw)
     return self.Outputs(img=draw)
Ejemplo n.º 12
0
    def run(self, inputs):

        # for maximum performance this should be:
        # for f in self.stream
        #    frame = Mat(f.array)
        #    self.capBuffer.turncate(0)
        # However this is infinite loop and needs different framework from OPSI

        frame = None
        if self.cap:
            self.cap.capture(self.capBuffer, format='bgr', use_video_port=True)
            frame = Mat(self.capBuffer.array)
            self.capBuffer.truncate(0)
        return self.Outputs(img=frame)
Ejemplo n.º 13
0
 def run(self, inputs):
     self.f.update()
     fps_str = str(round(self.f.fps(), 1))
     draw = np.copy(inputs.img.mat.img)
     cv2.putText(
         draw,
         fps_str,
         (30, 30),
         cv2.FONT_HERSHEY_SIMPLEX,
         1.0,
         (255, 255, 255),
         lineType=cv2.LINE_AA,
     )
     draw = Mat(draw)
     return self.Outputs(img=draw)
Ejemplo n.º 14
0
    def run(self, inputs):
        # If there are no circles return the input image
        if inputs.corners is None:
            return self.Outputs(img=inputs.img)
        img = np.copy(inputs.img.mat.img)

        for corner in inputs.corners:
            cv2.circle(
                img,
                (int(corner.x), int(corner.y)),
                5,
                (0, 0, 255),
                3,
            )
        img = Mat(img)

        return self.Outputs(img=img)
Ejemplo n.º 15
0
 def run(self, inputs):
     draw = np.copy(inputs.img.mat.img)
     height, width = draw.shape[:2]
     text_coords = (
         int(width * self.settings.x_pct / 100.0),
         int(height * self.settings.y_pct / 100.0),
     )
     cv2.putText(
         draw,
         str(inputs.text),
         text_coords,
         cv2.FONT_HERSHEY_SIMPLEX,
         0.5,
         (255, 255, 255),
         lineType=cv2.LINE_AA,
     )
     draw = Mat(draw)
     return self.Outputs(img=draw)
Ejemplo n.º 16
0
    def run(self, inputs):
        # If there are no circles return the input image
        if inputs.circles is None:
            return self.Outputs(img=inputs.img)

        draw = np.copy(inputs.img.mat.img)

        int_circles = np.uint16(np.around(inputs.circles))

        for pt in int_circles[0, :]:
            a, b, r = pt[0], pt[1], pt[2]

            # Draw the circumference of the circle.
            cv2.circle(draw, (a, b), r, (0, 255, 0), 2)

            # Draw a small circle (of radius 1) to show the center.
            cv2.circle(draw, (a, b), 1, (0, 0, 255), 3)

        draw = Mat(draw)
        return self.Outputs(img=draw)
Ejemplo n.º 17
0
 def run(self, inputs):
     frame = None
     if self.cap:
         ret, frame = self.cap.read()
         frame = Mat(frame)
     return self.Outputs(img=frame)
Ejemplo n.º 18
0
    def run(self, inputs):
        if inputs.pose is None:
            return self.Outputs(img=inputs.img)

        draw = np.copy(inputs.img.mat.img)

        # Draw the inner or outer target
        if (
            self.settings.draw_target == "Outer port"
            or self.settings.draw_target == "Inner port"
        ):
            if self.settings.draw_target == "Outer port":
                target_img_points = inputs.pose.object_to_image_points(
                    target_points_outer.astype(np.float),
                    self.camera_matrix,
                    self.distortion_coefficients,
                )
            else:
                target_img_points = inputs.pose.object_to_image_points(
                    target_points_inner.astype(np.float),
                    self.camera_matrix,
                    self.distortion_coefficients,
                )

            cv2.line(
                draw,
                tuple(target_img_points[0].ravel()),
                tuple(target_img_points[1].ravel()),
                (0, 255, 255),
                2,
            )
            cv2.line(
                draw,
                tuple(target_img_points[1].ravel()),
                tuple(target_img_points[3].ravel()),
                (0, 255, 255),
                2,
            )
            cv2.line(
                draw,
                tuple(target_img_points[3].ravel()),
                tuple(target_img_points[2].ravel()),
                (0, 255, 255),
                2,
            )
            cv2.line(
                draw,
                tuple(target_img_points[2].ravel()),
                tuple(target_img_points[0].ravel()),
                (0, 255, 255),
                2,
            )
        # Draw axes
        axes_img_points = inputs.pose.object_to_image_points(
            axes_points.astype(np.float),
            self.camera_matrix,
            self.distortion_coefficients,
        )

        cv2.line(
            draw,
            tuple(axes_img_points[0].ravel()),
            tuple(axes_img_points[1].ravel()),
            (0, 0, 255),
            2,
        )
        cv2.line(
            draw,
            tuple(axes_img_points[0].ravel()),
            tuple(axes_img_points[2].ravel()),
            (0, 255, 0),
            2,
        )
        cv2.line(
            draw,
            tuple(axes_img_points[0].ravel()),
            tuple(axes_img_points[3].ravel()),
            (255, 0, 0),
            2,
        )

        draw = Mat(draw)
        return self.Outputs(img=draw)