예제 #1
0
class Morphology(Filter):

    def __init__(self):
        Filter.__init__(self)
        self.kernel_width = Param("Kernel Width", 3, min_v=1, max_v=256)
        self.kernel_height = Param("Kernel Height", 3, min_v=1, max_v=256)
        self.anchor_x = Param("Anchor X", -1)
        self.anchor_y = Param("Anchor Y", -1)
        self.iterations = Param("Iteration,", 1, min_v=1)
        self.configure()

    def configure(self):
        self._kernel = cv2.getStructuringElement(cv2.MORPH_RECT,
                                                 (self.kernel_width.get(),
                                                  self.kernel_height.get()),
                                                 (self.anchor_x.get(),
                                                  self.anchor_y.get()))

    def execute(self, image):
        morph = cv2.cvtColor(image, cv.CV_BGR2GRAY)
        cv2.morphologyEx(
            morph,
            cv2.MORPH_CLOSE,
            self._kernel,
            dst=morph,
            iterations=self.iterations.get())
        cv2.merge((morph, morph, morph), image)

        return image
예제 #2
0
    def __init__(self):
        Filter.__init__(self)
        self.area_min = Param("Area Min", 300, min_v=1, max_v=100000)
        self.area_max = Param("Area Max", 35000, min_v=1, max_v=100000)

        self._kernel = cv2.getStructuringElement(
            cv2.MORPH_RECT, (3, 3), (0, 0))
예제 #3
0
class ColorThreshold(Filter):

    """Apply a binary threshold on the three channels of the images
        Each channel have a minimum and a maximum value.
        Everything within this threshold is white (255, 255, 255)
        Everything else is black (0, 0, 0)"""

    def __init__(self):
        Filter.__init__(self)
        self.blue = Param("Blue", 20, min_v=1, max_v=256, thres_h=256)
        self.green = Param("Green", 20, min_v=1, max_v=256, thres_h=256)
        self.red = Param("Red", 20, min_v=1, max_v=256, thres_h=256)
        self._barray = None
        self._garray = None
        self._rarray = None
        self.configure()

    def configure(self):
        min_v, max_v = self.blue.get()
        self._barray = np.array([1.0 * (min_v <= x <= max_v)
                                 for x in range(0, 256)], dtype=np.float32)
        min_v, max_v = self.green.get()
        self._garray = np.array([1.0 * (min_v <= x <= max_v)
                                 for x in range(0, 256)], dtype=np.float32)
        min_v, max_v = self.red.get()
        self._rarray = np.array([1.0 * (min_v <= x <= max_v)
                                 for x in range(0, 256)], dtype=np.float32)

    def execute(self, image):
        image[:, :, 0] = image[:, :, 1] = image[:, :, 2] = (
            255 * self._barray[image[:, :, 0]] *
            self._garray[image[:, :, 1]] *
            self._rarray[image[:, :, 2]])
        return image
예제 #4
0
 def __init__(self):
     Filter.__init__(self)
     self.kernel_erode_height = Param(
         "Kernel Erode Height",
         3,
         min_v=1,
         max_v=255)
     self.kernel_erode_width = Param(
         "Kernel Dilate Width",
         3,
         min_v=1,
         max_v=255)
     self.kernel_dilate_height = Param(
         "Kernel Erode Height",
         5,
         min_v=1,
         max_v=255)
     self.kernel_dilate_width = Param(
         "Kernel Dilate Width",
         5,
         min_v=1,
         max_v=255)
     self.sections = Param("Sections", 5, min_v=1, max_v=10)
     self.min_area = Param("Minimum Area", 1000, min_v=1, max_v=65535)
     self.configure()
예제 #5
0
class ExePy1(Filter):
    """
    Python Example Test #1
    Convert BGR color to another color.
    """

    def __init__(self):
        Filter.__init__(self)

        self.convert_color = Param("Convert choice", 1, min_v=0, max_v=4)
        desc = "0 = original\n1 = BGR TO YUV\n2 = BGR TO HSV\n3 = BGR TO RGB\n\
        4 = BGR TO GRAY"
        self.convert_color.set_description(desc)

    def execute(self, image):
        convert_color = self.convert_color.get()

        if convert_color == 1:
            image = cv2.cvtColor(image, cv2.COLOR_BGR2YUV)
        elif convert_color == 2:
            image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
        elif convert_color == 3:
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        elif convert_color == 4:
            image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        return image
예제 #6
0
 def __init__(self):
     Filter.__init__(self)
     self.kernel_width = Param("Kernel Width", 3, min_v=1, max_v=256)
     self.kernel_height = Param("Kernel Height", 3, min_v=1, max_v=256)
     self.anchor_x = Param("Anchor X", -1)
     self.anchor_y = Param("Anchor Y", -1)
     self.iterations = Param("Iteration,", 1, min_v=1)
     self.configure()
예제 #7
0
 def __init__(self):
     Filter.__init__(self)
     self.blue = Param("Blue", 20, min_v=1, max_v=256, thres_h=256)
     self.green = Param("Green", 20, min_v=1, max_v=256, thres_h=256)
     self.red = Param("Red", 20, min_v=1, max_v=256, thres_h=256)
     self._barray = None
     self._garray = None
     self._rarray = None
     self.configure()
예제 #8
0
class LineOrientation(Filter):
    """Port of the old line detection code"""

    def __init__(self):
        Filter.__init__(self)
        self.area_min = Param("Area Min", 300, min_v=1, max_v=100000)
        self.area_max = Param("Area Max", 35000, min_v=1, max_v=100000)

        self._kernel = cv2.getStructuringElement(
            cv2.MORPH_RECT, (3, 3), (0, 0))

    def execute(self, image):
        image_threshold = cv2.split(image)[0]
        image_morphology = cv2.morphologyEx(
            image_threshold, cv2.MORPH_CLOSE, self._kernel, iterations=1)

        contours, _ = cv2.findContours(
            image_morphology,
            cv2.RETR_TREE,
            cv2.CHAIN_APPROX_SIMPLE)
        lines = self.find_lines(contours, image)
        self.draw_lines(lines, image)

        return image

    def draw_lines(self, lines, image):
        msg = "LineOrientation: x1=%s x2=%s y1=%s y2=%s \n"
        for l, t in lines:
            vx, vy, x, y = l
            point1 = (x - t * vx, y - t * vy)
            point2 = (x + t * vx, y + t * vy)
            to_send = msg % (int(point1[0][0]),
                             int(point1[1][0]),
                             int(point2[0][0]),
                             int(point2[1][0]))
            self.notify_output_observers(to_send)
            cv2.line(image, point1, point2, (0, 0, 255), 3, -1)
            cv2.circle(image, (x, y), 5, (0, 255, 0), -1)

        self.notify_output_observers("LineOrientation: \n")

    def find_lines(self, contours, image):
        lines = []
        for contour in contours:
            approx = cv2.approxPolyDP(contour, 0, False)
            area = np.abs(cv2.contourArea(contour))

            if self.area_min.get() < area < self.area_max.get():
                line_values = cv2.fitLine(approx, cv.CV_DIST_L2, 0, 0.01, 0.01)
                rect = cv2.boundingRect(approx)
                t = math.sqrt((rect[2] ** 2 + rect[3] ** 2) / 2.0)
                lines.append((line_values, t))
                cv2.drawContours(image, contour, -1, (255, 255, 0))

        return lines
예제 #9
0
 def _deserialize_param(params_ser):
     if type(params_ser) is dict:
         value = {}
         for name, param_ser in params_ser.items():
             param = Param(name, None, serialize=param_ser)
             value[param.get_name()] = param
         return value
     elif type(params_ser) is list:
         return [Param("temp", None, serialize=param_ser) for param_ser in
                 params_ser]
     return []
예제 #10
0
class Blur(Filter):
    """Smoothes an image using the normalized box filter"""
    def __init__(self):
        Filter.__init__(self)
        self.kernel_width = Param("width", 3, min_v=1, max_v=10)
        self.kernel_height = Param("height", 3, min_v=1, max_v=10)

    def execute(self, image):
        cv2.blur(image, (self.kernel_width.get(),
                        self.kernel_height.get()),
                        image)
        return image
예제 #11
0
class Rotate(Filter):
    """Draw a black rectangle on top of the image"""

    def __init__(self):
        Filter.__init__(self)
        self.enable = Param('enable', True)
        self.angle = Param('angle', 0, max_v=3, min_v=0)

    def execute(self, image):
        angle = self.angle.get() * 90
        if self.enable.get():
            return scipy.ndimage.interpolation.rotate(image, angle)
        return image
class BilateralFilter(Filter):
    """Applies the bilateral filter to an image."""

    def __init__(self):
        Filter.__init__(self)
        self.diameter = Param("Diameter", 10, min_v=0, max_v=255)
        self.sigma_color = Param("Sigma Color", 0, min_v=0, max_v=255)
        self.sigma_space = Param("Sigma Space", 0, min_v=0, max_v=255)

    def execute(self, image):
        return cv2.bilateralFilter(image,
                            self.diameter.get(),
                            self.sigma_color.get(),
                            self.sigma_space.get())
예제 #13
0
class Perspective(Filter):

    """Wrap perspective"""

    def __init__(self):
        Filter.__init__(self)
        self.topleftx = Param("Top Left X", 0, min_v=0, max_v=640)
        self.toplefty = Param("Top Left TY", 0, min_v=0, max_v=480)
        self.bottomleftx = Param("Bottom Left X", 100, min_v=0, max_v=640)
        self.bottomlefty = Param("Bottom Left Y", 480, min_v=0, max_v=480)
        self.toprightx = Param("Top Right X", 640, min_v=0, max_v=640)
        self.toprighty = Param("Top Right Y", 0, min_v=0, max_v=480)
        self.bottomrightx = Param("Bottom Right X", 540, min_v=0, max_v=640)
        self.bottomrighty = Param("Bottom Right Y", 480, min_v=0, max_v=480)

        self.mmat = None
        self.configure()

    def configure(self):
        c1 = np.array([[self.topleftx.get(), self.toplefty.get()],
                       [self.bottomleftx.get(), self.bottomlefty.get()],
                       [self.toprightx.get(), self.toprighty.get()],
                       [self.bottomrightx.get(), self.bottomrighty.get()]],
                      np.float32)
        c2 = np.array([[0, 0], [0, 480], [640, 0], [640, 480]], np.float32)
        self.mmat = cv2.getPerspectiveTransform(c2, c1)

    def execute(self, image):
        cv2.warpPerspective(image, self.mmat, (640, 480), image)
        return image
예제 #14
0
 def deserialize(self, data):
     if not data:
         return False
     for uno_data in data:
         if not uno_data:
             continue
         try:
             param = Param(None, None, serialize=uno_data)
             own_param = self.dct_params.get(param.get_name(), None)
             if own_param:
                 own_param.merge(param)
         except Exception as e:
             log.printerror_stacktrace(logger, e)
             return False
     return True
class RemoveObstacle(Filter):
    """Remove obstacles from an image"""

    def __init__(self):
        Filter.__init__(self)
        self.threshold = Param("Threshold", 12, min_v=0, max_v=255)
        #self.vertical_blur = Param("Vertical Blur", 18, min_v=0, max_v=255)
        #self.horizontal_blur = Param("Horizontal Blur", 3, min_v=0, max_v=255)

    def execute(self, image):
        # copy = cv2.cvtColor(image, cv.CV_BGR2HSV)
        copy = cv2.blur(image, (3, 3))
        h, _, _ = cv2.split(copy)
        h[h > self.threshold.get()] = 0
        contours, _ = cv2.findContours(
                                   h,
                                   cv2.RETR_TREE,
                                   cv2.CHAIN_APPROX_SIMPLE)
        for contour in contours:
            x, y, w, h = cv2.boundingRect(contour)
            miny = y - h
            if miny < 0:
                miny = 0
            maxy = y + h
            if maxy > image.shape[0]:
                maxy = image.shape[0]
            minx = x
            if minx < 0:
                minx = 0
            maxx = x + w
            if maxx > image.shape[1]:
                maxx = image.shape[1]
            image[miny:maxy, minx:maxx] = 0

        return image
예제 #16
0
class Canny(Filter):

    """Apply a canny filter to the image"""

    def __init__(self):
        Filter.__init__(self)
        self.threshold1 = Param("Threshold1", 10, min_v=0, max_v=255)
        self.threshold2 = Param("Threshold2", 100, min_v=0, max_v=255)

    def execute(self, image):
        gray = cv2.cvtColor(image, cv.CV_BGR2GRAY)

        cv2.Canny(gray, self.threshold1.get(), self.threshold2.get(), gray)
        cv2.merge((gray, gray, gray), image)

        return image
예제 #17
0
    def __init__(self):
        Filter.__init__(self)

        self.convert_color = Param("Convert choice", 1, min_v=0, max_v=4)
        desc = "0 = original\n1 = BGR TO YUV\n2 = BGR TO HSV\n3 = BGR TO RGB\n\
        4 = BGR TO GRAY"
        self.convert_color.set_description(desc)
예제 #18
0
    def _create_params(self):
        default_width = 800
        self.param_width = Param("width", default_width, min_v=1, max_v=1200)
        self.param_width.add_group("Resolution")
        self.param_width.set_description("Change width resolution.")

        default_height = 600
        self.param_height = Param("height", default_height, min_v=1,
                                  max_v=1200)
        self.param_height.add_group("Resolution")
        self.param_height.set_description("Change height resolution.")

        default_fps = 30
        self.param_fps = Param("fps", default_fps, min_v=1, max_v=100)
        self.param_fps.set_description("Change frame per second.")

        self.param_color_r = Param("color_r", 0, min_v=0, max_v=255)
        self.param_color_r.add_group("Color")
        self.param_color_r.set_description("Change red color.")

        self.param_color_g = Param("color_g", 0, min_v=0, max_v=255)
        self.param_color_g.add_group("Color")
        self.param_color_g.set_description("Change green color.")

        self.param_color_b = Param("color_b", 0, min_v=0, max_v=255)
        self.param_color_b.add_group("Color")
        self.param_color_b.set_description("Change blue color.")

        self.param_auto_color = Param("auto-change-color", False)
        self.param_auto_color.set_description(
            "Change the color automatically.")
        self.param_auto_color.add_group("Color")

        self.param_random_green = Param("pooling_green_random", False)
        self.param_random_green.set_description(
            "Active pooling update of green color with random value.")
        self.param_random_green.add_notify(self._active_green_pooling)
        self.param_random_green.add_group("Color")

        self.param_transpose_r_color = Param("Transpose red color", None)
        self.param_transpose_r_color.set_description(
            "Copy the red color on others color.")
        self.param_transpose_r_color.add_notify(self._transpose_red_color)
        self.param_transpose_r_color.add_group("Color")

        self.param_freeze = Param("freeze", False)
        self.param_freeze.set_description("Freeze the stream.")
예제 #19
0
class GaussianBlur(Filter):
    """Smoothes an image using a Gaussian filter"""
    def __init__(self):
        Filter.__init__(self)
        self.kernel_height = Param("Kernel Height", 3, min_v=1, max_v=256)
        self.kernel_width = Param("Kernel Width", 3, min_v=1, max_v=256)
        self.sigma_x = Param("Sigma X", 3, min_v=1, max_v=256)
        self.sigma_y = Param("Sigma Y", 3, min_v=1, max_v=256)

    def execute(self, image):
        cv2.GaussianBlur(image,
                         (self.kernel_height.get(),
                          self.kernel_width.get()),
                     sigmaX=self.sigma_x.get(),
                     sigmaY=self.sigma_y.get(),
                     dst=image)
        return image
예제 #20
0
class Rectangle(Filter):
    """Draw a black rectangle on top of the image"""

    def __init__(self):
        Filter.__init__(self)
        self.x1 = Param('x1', 0, max_v=65535, min_v=0)
        self.y1 = Param('y1', 0, max_v=65535, min_v=0)
        self.x2 = Param('x2', 100, max_v=65535, min_v=0)
        self.y2 = Param('y2', 100, max_v=65535, min_v=0)

    def execute(self, image):
        cv2.rectangle(image,
            (self.x1.get(), self.y1.get()),
            (self.x2.get(), self.y2.get()),
            (0, 0, 0),
            cv2.cv.CV_FILLED)
        return image
예제 #21
0
class RemoveGrass(Filter):

    """Remove grass from an image"""

    def __init__(self):
        Filter.__init__(self)
        self.threshold = Param("Threshold", 100, min_v=0, max_v=255)
        self.technique = Param("Technique", 0, min_v=0, max_v=2)

    def remove_green_from_blue(self, image):
        blue, green, red = cv2.split(image)
        new_blue = blue - green / 2
        black = blue < new_blue
        new_blue[black] = 0
        threshold = new_blue < self.threshold.get()
        blue[threshold] = 0
        green[threshold] = 0
        red[threshold] = 0
        cv2.merge((blue, green, red), image)
        return image

    def add_green_to_blue(self, image):
        blue, green, red = cv2.split(image)
        new_color = green + blue
        white = ((new_color < green) & (new_color < blue))
        new_color[white] = 255
        threshold = new_color > self.threshold.get()
        blue[threshold] = 0
        green[threshold] = 0
        red[threshold] = 0
        cv2.merge((blue, green, red), image)
        return image

    def enhance_grass(self, image):
        blue, green, _ = cv2.split(image)
        image[:, :, 0] = np.subtract(blue, green / 2)
        return image

    def execute(self, image):
        if self.technique.get() == 0:
            return self.add_green_to_blue(image)
        elif self.technique.get() == 1:
            return self.remove_green_from_blue(image)
        elif self.technique.get() == 2:
            return self.enhance_grass(image)
예제 #22
0
    def _create_params(self):
        default_resolution_name = "800x600"
        self.dct_resolution = {default_resolution_name: (800, 600),
                               "320x240": (320, 240),
                               "640x480": (640, 480),
                               "1024x768": (1024, 768),
                               "1280x960": (1280, 960),
                               "1280x1024": (1280, 1024)}
        self.param_resolution = Param(
            "resolution",
            default_resolution_name,
            lst_value=self.dct_resolution.keys())
        self.param_resolution.add_notify(self.reload)

        default_fps_name = "30"
        self.dct_fps = {default_fps_name: 30, "15": 15, "7.5": 7.5}
        self.param_fps = Param("fps", default_fps_name,
                               lst_value=self.dct_fps.keys())
        self.param_fps.add_notify(self.reload)
예제 #23
0
    def __init__(self):
        Filter.__init__(self)
        self.topleftx = Param("Top Left X", 0, min_v=0, max_v=640)
        self.toplefty = Param("Top Left TY", 0, min_v=0, max_v=480)
        self.bottomleftx = Param("Bottom Left X", 100, min_v=0, max_v=640)
        self.bottomlefty = Param("Bottom Left Y", 480, min_v=0, max_v=480)
        self.toprightx = Param("Top Right X", 640, min_v=0, max_v=640)
        self.toprighty = Param("Top Right Y", 0, min_v=0, max_v=480)
        self.bottomrightx = Param("Bottom Right X", 540, min_v=0, max_v=640)
        self.bottomrighty = Param("Bottom Right Y", 480, min_v=0, max_v=480)

        self.mmat = None
        self.configure()
예제 #24
0
class FaceDetection(Filter):
    """Detect faces and eyes"""

    def __init__(self):
        Filter.__init__(self)
        self.nb_face = 1
        # linux path
        path_frontal_face = os.path.join('/', 'usr', 'share', 'opencv',
                                         'haarcascades',
                                         'haarcascade_frontalface_alt.xml')
        self.face_detect_name = os.path.join(
            'data', 'facedetect', path_frontal_face)

        self.face_cascade = cv2.CascadeClassifier()
        self.face_cascade.load(self.face_detect_name)

        self.notify_filter = Param("notify", False)

    def execute(self, image):
        gray = cv2.cvtColor(image, cv.CV_BGR2GRAY)
        cv2.equalizeHist(gray, gray)
        faces = self.face_cascade.detectMultiScale(gray,
                                                   1.1,
                                                   2,
                                                   0 | cv.CV_HAAR_SCALE_IMAGE,
                                                   (30, 30))
        for face in faces:
            self.draw_rectangle(image, face, (0, 0, 255))
        return image

    def draw_rectangle(self, image, coord, color):
        x, y, w, h = coord
        miny = y
        if miny < 0:
            miny = 0
        max_y = y + h
        if max_y > image.shape[0]:
            max_y = image.shape[0]
        minx = x
        if minx < 0:
            minx = 0
        max_x = x + w
        if max_x > image.shape[1]:
            max_x = image.shape[1]

        cv2.rectangle(image, (minx, miny), (max_x, max_y), color, 3)

        c_x = (max_x - minx) / 2 + minx
        c_y = (max_y - miny) / 2 + miny
        if self.notify_filter.get():
            self.notify_output_observers("facedetect%d : x=%d, y=%d" %
                                         (self.nb_face, c_x, c_y))
        self.nb_face += 1
        return image[miny:max_y, minx:max_x]
예제 #25
0
 def __init__(self):
     Filter.__init__(self)
     self.nb_face = 1
     self.eye_detect_name = os.path.join('data', 'facedetect',
                                     'haarcascade_eye_tree_eyeglasses.xml')
     self.face_detect_name = os.path.join('data', 'facedetect',
                                          'haarcascade_frontalface_alt.xml')
     self.eye_cascade = cv2.CascadeClassifier()
     self.face_cascade = cv2.CascadeClassifier()
     self.eye_cascade.load(self.eye_detect_name)
     self.face_cascade.load(self.face_detect_name)
     self.notify_filter = Param("notify", False)
예제 #26
0
class ColorLevel(Filter):
    """Determine the value in % a color will have.
        0% = Nothing
        50% = Half the original value.
        100% = Original
        Example: With 50% Blue and the following pixel (100, 100, 100) give (50, 100, 100)"""

    def __init__(self):
        Filter.__init__(self)
        self.red = Param("red", 100, min_v=0, max_v=255)
        self.green = Param("green", 100, min_v=0, max_v=255)
        self.blue = Param("blue", 100, min_v=0, max_v=255)

    def execute(self, image):
        if self.red != 100:
            image[:, :, 2] *= self.red.get() / 100
        if self.green != 100:
            image[:, :, 1] *= self.green.get() / 100
        if self.blue != 100:
            image[:, :, 0] *= self.blue.get() / 100
        return image
예제 #27
0
    def __init__(self):
        Filter.__init__(self)
        self.case = 'test'
        self.resize = False

        self.strength = Param("strength", 0.0, min_v=0.0, max_v=3.0)
        self.naturalness = Param("naturalness", 10, min_v=0, max_v=10)
        # self.sub_lum = Param("sub_lum", 100, min_v=0, max_v=255)
        # self.shift_x = Param("shift_x", 0, min_v=-800, max_v=800)
        # self.shift_y = Param("shift_y", 0, min_v=-600, max_v=600)

        self.show_image = Param("show_images", 1, min_v=1, max_v=10)
        self.limit_image = Param("limit_image", 4, min_v=1, max_v=10)
        self.debug_show = Param(
            "show_debug",
            "show_normal",
            lst_value=[
                "show_normal",
                "show_sat",
                "show_con"])
        self.show_hdr = Param("show_hdr", False)
        self.index = 0
        self.images = []
        self.imgs = []
        self.first_time = True
예제 #28
0
class TestSeagoat(Filter):

    def __init__(self):
        Filter.__init__(self)
        self.param_str = Param("param_str", "")

    def configure(self):
        # This is called when param is modify
        pass

    def execute(self, image):
        self.notify_output_observers(self.param_str.get())
        return image
 def __init__(self):
     Filter.__init__(self)
     self.canny1 = Param("Canny1", 50, min_v=1, max_v=256)
     self.canny2 = Param("Canny2", 200, min_v=1, max_v=256)
     self.rho = Param("Rho", 1, min_v=1, max_v=256)
     self.theta = Param("Theta", 180, min_v=0, max_v=360)
     self.threshold = Param("Threshold", 100, min_v=1, max_v=256)
     self.line_size = Param("Line Size", 1000, min_v=1, max_v=2000)
예제 #30
0
    def __init__(self):
        Filter.__init__(self)
        self.nb_face = 1
        # linux path
        path_frontal_face = os.path.join('/', 'usr', 'share', 'opencv',
                                         'haarcascades',
                                         'haarcascade_frontalface_alt.xml')
        self.face_detect_name = os.path.join(
            'data', 'facedetect', path_frontal_face)

        self.face_cascade = cv2.CascadeClassifier()
        self.face_cascade.load(self.face_detect_name)

        self.notify_filter = Param("notify", False)
예제 #31
0
class Firewire(MediaStreaming):
    """Return images from a Firewire device."""

    def __init__(self, config):
        # Go into configuration/template_media for more information
        super(Firewire, self).__init__()
        self.config = Configuration()
        self.camera = None
        self.is_streaming = False
        self.loop_try_open_camera = False
        self.call_stop = False
        self.sem_closed = threading.Semaphore()

        self.cam_guid = config.guid
        self.cam_no = config.no
        # the id is guid or no, writing into open_camera
        self.id = ""
        self.key_auto_param = "-auto"
        self.reference_param = {"power": self._power,
                                "transmission": self._transmission}

        fps = 15
        self.sleep_time = 1 / 15.0
        self.media_name = config.name
        self.last_timestamp = -1
        self.actual_timestamp = -1
        self.count_not_receive = 0
        self.max_not_receive = fps * 2
        self.buffer_last_timestamp = False
        self.own_config = config
        self.is_rgb = config.is_rgb
        self.is_mono = config.is_mono
        self.is_format_7 = config.is_format7
        self.is_yuv = config.is_yuv
        self.actual_image = None
        self.shape = (800, 600)
        self.count_no_image = 0
        self.max_no_image = 120

        self.lst_param_shutter = []
        self.lst_param_whitebalance = []

        if not self.try_open_camera(repeat_loop=3, sleep_time=1):
            return

        self._create_params()

        self.deserialize(self.config.read_media(self.get_name()))
        self.update_all_property()

    def is_opened(self):
        return self.camera is not None

    def initialize(self):
        logger.debug("initialize camera %s" % self.get_name())
        if not self.camera:
            return False
        try:
            init = self.camera.initialize
            init(reset_bus=True, mode=self.own_config.mode,
                 framerate=self.own_config.framerate,
                 iso_speed=self.own_config.iso_speed,
                 operation_mode=self.own_config.operation_mode)
        except:
            return False
        return True

    def try_open_camera(
            self, open_streaming=False, repeat_loop=-1, sleep_time=1):
        # param :
        # int repeat_loop - if -1, it's an infinite loop, \
        # else it's the number loop
        # bool open_streaming - if true, try to start the streaming \
        # of seagoat and the firewire
        # can be use in threading or in init

        self.loop_try_open_camera = True
        while self.loop_try_open_camera:
            # need to wait 1 second if camera just shutdown, else it's crash
            time.sleep(sleep_time)
            if self.call_stop:
                return False
            # check if can access to the camera
            if self.open_camera():
                time.sleep(2)
                if self.initialize():
                    time.sleep(2)
                    if open_streaming:
                        time.sleep(2)
                        if self.open():
                            logger.debug(
                                "Open with success %s" %
                                self.get_name())
                            self.loop_try_open_camera = False
                            return True
                    else:
                        logger.debug("Finish with initialize")
                        self.loop_try_open_camera = False
                        return True
            # check if need to continue the loop
            if not repeat_loop:
                self.loop_try_open_camera = False
                return False
            if repeat_loop > 0:
                repeat_loop -= 1
            log.print_function(
                logger.error, "Cannot open the camera %s" %
                self.get_name())

    def open_camera(self):
        logger.debug("open camera %s" % self.get_name())
        try:
            ctx = video1394.DC1394Context()
        except:
            log.print_function(logger.error, "Libdc1394 is not supported.")
            return False

        if self.cam_guid:
            self.camera = ctx.createCamera(guid=self.cam_guid)
            self.id = "guid %s" % str(self.cam_guid)
        else:
            self.camera = ctx.createCamera(cid=self.cam_no)
            self.id = "no %s" % str(self.cam_no)

        if self.camera is not None:
            return True
        else:
            log.print_function(
                logger.warning,
                "No Firewire camera detected - %s." %
                self.id)
        return False

    def open(self):
        logger.debug("open firewire %s" % self.get_name())
        self.call_stop = False
        if not self.camera:
            # try to open the camera
            # caution, can cause an infinite loop
            return self.try_open_camera(repeat_loop=3, open_streaming=True,
                                        sleep_time=1)

        self.camera.initEvent.addObserver(self.camera_init)
        self.camera.grabEvent.addObserver(self.camera_observer)
        # self.camera.stopEvent.addObserver(self.camera_closed)
        try:
            logger.debug("camera %s start." % self.get_name())
            self.camera.start(force_rgb8=True)
            self.param_transmission.set(True)
            logger.debug("camera %s start terminated." % self.get_name())
        except BaseException as e:
            logger.error(e)
            self.camera.stop()
            # something crash, restart the camera
            return self.try_open_camera(repeat_loop=1, open_streaming=True,
                                        sleep_time=1)
        return True

    def camera_init(self):
        MediaStreaming.open(self)
        self.is_streaming = True

    def camera_observer(self, im, timestamp):
        if self.is_rgb or not self.is_mono:
            image = Image.fromarray(im, "RGB")
            image2 = np.asarray(image, dtype="uint8")
            # transform it to BGR
            cv2.cvtColor(np.copy(image), cv.CV_BGR2RGB, image2)
        elif self.is_mono:
            image2 = im
        self.actual_image = image2
        self.last_timestamp = timestamp

    def camera_closed(self):
        self.sem_closed.acquire()
        if not self.camera or not self.is_streaming:
            # we already close the camera
            return
        # anormal close, do something!
        logger.error(
            "Receive events camera close %s, retry to reopen it." % self.id)
        # clean camera
        self.camera.grabEvent.removeObserver(self.camera_observer)
        # self.camera.stopEvent.removeObserver(self.camera_closed)
        self.actual_image = None
        # self.camera.safe_clean(free_camera=False)
        self.camera = None
        self.is_streaming = False
        # reopen the camera
        kwargs = {"open_streaming": True}
        # TODO how using kwargs???
        if not self.call_stop:
            thread.start_new_thread(self.try_open_camera, (True,))
            time.sleep(2)
        self.sem_closed.release()

    def next(self):
        if not self.camera or not self.is_streaming:
            return

        diff_time = self.last_timestamp - self.actual_timestamp
        # logger.debug("actual time %s, last time %s, diff %s" %
        # (self.actual_timestamp, self.last_timestamp, diff_time))
        self.actual_timestamp = self.last_timestamp
        if self.last_timestamp == -1:
            if not self.buffer_last_timestamp:
                self.buffer_last_timestamp = True
                return
                log.print_function(
                    logger.warning,
                    "No image receive from %s" % self.get_name())
            self.count_no_image += 1
            if self.count_no_image > self.max_no_image:
                self.count_no_image = 0
                self.camera_closed()
            return
        if not diff_time:
            self.count_not_receive += 1
            if self.count_not_receive >= self.max_not_receive:
                # logger.error("Didn't receive since %d images. Restart the
                # camera %s??" % (self.count_not_receive, self.id))
                logger.error(
                    "Didn't receive since %d images on camera %s" %
                    (self.count_not_receive, self.get_name()))
                self.actual_timestamp = self.last_timestamp = -1
                self.count_not_receive = 0
            # ignore if only missing one image
            if not self.buffer_last_timestamp:
                self.buffer_last_timestamp = True
                return self.actual_image
            else:
                # logger.warning(
                #    "Receive no more image from %s, timestamp %d" %
                #    (self.get_name(), self.actual_timestamp))
                return
        # reinitilize all protection
        self.buffer_last_timestamp = False
        self.count_no_image = 0
        self.count_not_receive = 0
        return self.actual_image

    def close(self):
        # Only the manager can call this close or the reload on media.py
        MediaStreaming.close(self)
        self.call_stop = True
        self.loop_try_open_camera = False
        self.is_streaming = False
        if self.camera:
            self.param_transmission.set(False)
            self.camera.stop()
            self.camera.initEvent.removeObserver(self.camera_init)
            self.camera.grabEvent.removeObserver(self.camera_observer)
            # self.camera.stopEvent.removeObserver(self.camera_closed)
            self.camera.safe_clean()
            self.camera = None
            return True
        else:
            logger.warning("Camera %s already close." % self.get_name())
        return False

    # PARAMS

    def _create_params(self):
        if not self.camera:
            return
        group_name_color = "Color"
        group_name_shutter = "Shutter"
        lst_ignore_prop = ["Trigger"]
        dct_prop = self.camera.get_dict_available_features()
        for name, value in dct_prop.items():
            if name in lst_ignore_prop:
                continue
            try:
                if name == "White Balance":
                    # add auto white balance
                    param = Param("%s%s" % (name, self.key_auto_param), False)
                    param.add_notify(self.update_property_param)
                    param.add_group(group_name_color)
                    param.add_notify(self._trig_auto_whitebalance)
                    self.add_param(param)
                    # add specific color of white balance
                    param = Param(
                        "RV_value",
                        value["RV_value"],
                        min_v=value["min"],
                        max_v=value["max"])
                    param.set_description("%s-red" % name)
                    param.add_notify(self.update_property_param)
                    param.add_group(group_name_color)
                    self.lst_param_whitebalance.append(param)
                    self.add_param(param)

                    param = Param(
                        "BU_value",
                        value["BU_value"],
                        min_v=value["min"],
                        max_v=value["max"])
                    param.set_description("%s-blue" % name)
                    param.add_notify(self.update_property_param)
                    self.lst_param_whitebalance.append(param)
                    param.add_group(group_name_color)
                    self.add_param(param)
                    continue

                param = Param(
                    name,
                    value["value"],
                    min_v=value["min"],
                    max_v=value["max"])
                param.add_notify(self.update_property_param)
                self.add_param(param)

                if name == "Shutter":
                    self.lst_param_shutter.append(param)
                    param.add_group(group_name_shutter)
                    # add auto param
                    param = Param("%s%s" % (name, self.key_auto_param), False)
                    param.add_notify(self._trig_auto_shutter)
                    param.add_notify(self.update_property_param)
                    param.add_group(group_name_shutter)
                    self.add_param(param)
            except BaseException as e:
                log.printerror_stacktrace(
                    logger, "%s - name: %s, value: %s" % (e, name, value))

        # add operational param
        group_operation = "operation"
        self.param_power = Param("Power", True)
        self.param_power.add_notify(self._power)
        self.param_power.add_group(group_operation)

        self.param_transmission = Param("Transmission", False)
        self.param_transmission.add_notify(self._transmission)
        self.param_transmission.add_group(group_operation)

        self.sync_params()

    def _trig_auto(self, param, lst_param, cb):
        if not self.camera:
            return False
        is_active = bool(param.get())
        for param in lst_param:
            # lock/unlock and start/stop pooling
            param.set_lock(is_active)
            if is_active:
                param.start_pooling(cb)
            else:
                param.stop_pooling()
        return True

    def _trig_auto_shutter(self, param):
        return self._trig_auto(param, self.lst_param_shutter, self._get_cam_property)

    def _trig_auto_whitebalance(self, param):
        return self._trig_auto(param, self.lst_param_whitebalance, self._get_cam_whitebalance_property)

    def _get_cam_property(self, param):
        return self.camera.get_property(param.get_name())

    def _get_cam_whitebalance_property(self, param):
        blue, red = self.camera.get_whitebalance()
        if "RV" in param.get_name():
            return red
        return blue

    def update_all_property(self):
        # If property is auto, don't apply manual parameter
        lst_auto = [value[:-(len(self.key_auto_param))]
                    for value in self.get_params().keys()
                    if self.key_auto_param in value]
        lst_auto = [value for value in lst_auto
                    if self.get_params("%s%s" %
                                       (value, self.key_auto_param)).get()]

        for key, param in self.get_params().items():
            contain_auto_variable = False
            # search active auto
            for active_key in lst_auto:
                if active_key in key:
                    contain_auto_variable = True
                    if self.key_auto_param in key:
                        self.update_property_param(param,
                                                   update_object_param=False)
            if contain_auto_variable:
                continue
            # find auto key disable and cancel it
            if self.key_auto_param in key:
                continue
            self.update_property_param(param, update_object_param=False)

    def update_property_param(self, param, update_object_param=True):
        if not self.camera or param.get_is_lock():
            return False
        param_name = param.get_name()
        value = param.get()

        if update_object_param:
            param.set(value)

        logger.debug(
            "Camera %s param_name %s and value %s",
            self.get_name(),
            param_name,
            value)
        if param_name.lower() in self.reference_param.keys():
            self.reference_param[param_name.lower()](param)
            return True

        if self.key_auto_param in param_name:
            new_param_name = param_name[:-len(self.key_auto_param)]
            self.camera.set_property_auto(new_param_name, value)
        elif "RV" in param_name:
            self.camera.set_whitebalance(RV_value=value)
        elif "BU" in param_name:
            self.camera.set_whitebalance(BU_value=value)
        else:
            self.camera.set_property(param_name, value)
        return True

    def _power(self, param):
        value = param.get()
        self.camera.power = int(bool(value))

    def _transmission(self, param):
        value = param.get()
        self.camera.transmission = int(bool(value))
예제 #32
0
class IPC(MediaStreaming):
    """
    Return image from IPC socket with ZeroMQ
    This media is a subscriber of ZeroMQ
    """
    key_ipc_name = "ipc name"

    def __init__(self, config):
        # Go into configuration/template_media for more information
        super(IPC, self).__init__()
        self.config = Configuration()
        self.own_config = config
        self.media_name = config.name
        if config.device:
            self.device_name = config.device
        else:
            self.device_name = "/tmp/seagoatvision_media.ipc"
        self._is_opened = True
        self.run = True
        self.video = None

        self.context = zmq.Context()
        self.subscriber = None
        self.message = None

        self._create_params()
        self.deserialize(self.config.read_media(self.get_name()))

    def _create_params(self):
        default_ipc_name = "ipc://%s" % self.device_name
        self.param_ipc_name = Param(self.key_ipc_name, default_ipc_name)
        self.param_ipc_name.add_notify(self.reload)

    def open(self):
        self.subscriber = self.context.socket(zmq.SUB)
        self.subscriber.setsockopt(zmq.SUBSCRIBE, b'')
        device_name = self.param_ipc_name.get()
        logger.info("Open media device %s" % device_name)
        self.subscriber.connect(device_name)
        thread.start_new_thread(self.fill_message, tuple())
        # call open when video is ready
        return MediaStreaming.open(self)

    def next(self):
        if not self.subscriber or not self.message:
            return
        image = None
        message = self.message[:]
        self.message = None
        lst_pixel = list(bytearray(message))
        # the first 2 bytes is width of image
        len_message = len(lst_pixel) - 2
        if len_message:
            width = (lst_pixel[0] << 8) + lst_pixel[1]
            if not width:
                return
            image = np.array(lst_pixel[2:])
            # check if missing pixel and replace by zero
            diff = len_message % width
            if diff:
                image += [0] * (width - diff)
            image = image.reshape((-1, width))
        shape = image.shape
        if len(shape) < 3 or 3 > shape[2]:
            # convert in 3 depth, bgr picture
            image_float = np.array(image, dtype=np.float32)
            vis2 = cv2.cvtColor(image_float, cv2.COLOR_GRAY2BGR)
            image = np.array(vis2, dtype=np.uint8)
        return image

    def close(self):
        MediaStreaming.close(self)
        # TODO need to debug, closing socket create errors and \
        # context.term freeze
        # self.subscriber.close()
        # self.context.term()
        self.subscriber = None
        return True

    def fill_message(self):
        try:
            while self.subscriber:
                self.message = self.subscriber.recv()
        except zmq.ContextTerminated:
            pass
        finally:
            self.message = None
예제 #33
0
    def _create_params(self):
        if not self.camera:
            return
        group_name_color = "Color"
        group_name_shutter = "Shutter"
        lst_ignore_prop = ["Trigger"]
        dct_prop = self.camera.get_dict_available_features()
        for name, value in dct_prop.items():
            if name in lst_ignore_prop:
                continue
            try:
                if name == "White Balance":
                    # add auto white balance
                    param = Param("%s%s" % (name, self.key_auto_param), False)
                    param.add_notify(self.update_property_param)
                    param.add_group(group_name_color)
                    param.add_notify(self._trig_auto_whitebalance)
                    self.add_param(param)
                    # add specific color of white balance
                    param = Param(
                        "RV_value",
                        value["RV_value"],
                        min_v=value["min"],
                        max_v=value["max"])
                    param.set_description("%s-red" % name)
                    param.add_notify(self.update_property_param)
                    param.add_group(group_name_color)
                    self.lst_param_whitebalance.append(param)
                    self.add_param(param)

                    param = Param(
                        "BU_value",
                        value["BU_value"],
                        min_v=value["min"],
                        max_v=value["max"])
                    param.set_description("%s-blue" % name)
                    param.add_notify(self.update_property_param)
                    self.lst_param_whitebalance.append(param)
                    param.add_group(group_name_color)
                    self.add_param(param)
                    continue

                param = Param(
                    name,
                    value["value"],
                    min_v=value["min"],
                    max_v=value["max"])
                param.add_notify(self.update_property_param)
                self.add_param(param)

                if name == "Shutter":
                    self.lst_param_shutter.append(param)
                    param.add_group(group_name_shutter)
                    # add auto param
                    param = Param("%s%s" % (name, self.key_auto_param), False)
                    param.add_notify(self._trig_auto_shutter)
                    param.add_notify(self.update_property_param)
                    param.add_group(group_name_shutter)
                    self.add_param(param)
            except BaseException as e:
                log.printerror_stacktrace(
                    logger, "%s - name: %s, value: %s" % (e, name, value))

        # add operational param
        group_operation = "operation"
        self.param_power = Param("Power", True)
        self.param_power.add_notify(self._power)
        self.param_power.add_group(group_operation)

        self.param_transmission = Param("Transmission", False)
        self.param_transmission.add_notify(self._transmission)
        self.param_transmission.add_group(group_operation)

        self.sync_params()
예제 #34
0
 def __init__(self):
     Filter.__init__(self)
     self.threshold = Param("Threshold", 12, min_v=0, max_v=255)
예제 #35
0
    def _create_params(self):
        default_width = 800
        self.param_width = Param("width", default_width, min_v=1, max_v=1200)
        self.param_width.add_group("Resolution")
        self.param_width.set_description("Change width resolution.")

        default_height = 600
        self.param_height = Param("height",
                                  default_height,
                                  min_v=1,
                                  max_v=1200)
        self.param_height.add_group("Resolution")
        self.param_height.set_description("Change height resolution.")

        default_fps = 30
        self.param_fps = Param("fps", default_fps, min_v=1, max_v=100)
        self.param_fps.set_description("Change frame per second.")

        self.param_color_r = Param("color_r", 0, min_v=0, max_v=255)
        self.param_color_r.add_group("Color")
        self.param_color_r.set_description("Change red color.")

        self.param_color_g = Param("color_g", 0, min_v=0, max_v=255)
        self.param_color_g.add_group("Color")
        self.param_color_g.set_description("Change green color.")

        self.param_color_b = Param("color_b", 0, min_v=0, max_v=255)
        self.param_color_b.add_group("Color")
        self.param_color_b.set_description("Change blue color.")

        self.param_auto_color = Param("auto-change-color", False)
        self.param_auto_color.set_description(
            "Change the color automatically.")
        self.param_auto_color.add_group("Color")

        self.param_random_green = Param("pooling_green_random", False)
        self.param_random_green.set_description(
            "Active pooling update of green color with random value.")
        self.param_random_green.add_notify(self._active_green_pooling)
        self.param_random_green.add_group("Color")

        self.param_transpose_r_color = Param("Transpose red color", None)
        self.param_transpose_r_color.set_description(
            "Copy the red color on others color.")
        self.param_transpose_r_color.add_notify(self._transpose_red_color)
        self.param_transpose_r_color.add_group("Color")

        self.param_freeze = Param("freeze", False)
        self.param_freeze.set_description("Freeze the stream.")
예제 #36
0
class Filter(PoolParam):
    def __init__(self, name=None):
        super(Filter, self).__init__()
        self._output_observers = list()
        self.original_image = None
        self.name = name
        self.dct_global_param = {}
        self.dct_media_param = {}
        self.execution_name = None
        self._publisher = None
        self._publish_key = None

        # add generic param
        self._active_param = Param("_active_filter", True)
        self._active_param.set_description("Enable filter in filterchain.")
        self._active_param.add_group("Generic")

    def serialize(self, is_config=False, is_info=False):
        if is_info:
            return {"name": self.name, "doc": self.__doc__}
        lst_param = super(Filter, self).serialize(is_config=is_config)
        return {"filter_name": self.__class__.__name__, "lst_param": lst_param}

    def deserialize(self, value):
        return super(Filter, self).deserialize(value.get("lst_param"))

    def get_name(self):
        return self.name

    def get_code_name(self):
        key = "-"
        if key in self.name:
            return self.name[:self.name.rfind("-")]
        return self.name

    def set_name(self, name):
        self.name = name

    def get_is_active(self):
        return bool(self._active_param.get())

    def destroy(self):
        # edit me
        # It's called just before to be destroyed
        pass

    def configure(self):
        # edit me
        pass

    def execute(self, image):
        # edit me
        return image

    def set_global_params(self, dct_global_param):
        # complete the list and point on it
        for key, param in self.dct_global_param.items():
            if key in dct_global_param:
                log.print_function(logger.error,
                                   "Duplicate key on dct_global_param : %s",
                                   key)
                continue
            dct_global_param[key] = param
        self.dct_global_param = dct_global_param
        self.set_global_params_cpp(self.dct_global_param)

    def set_global_params_cpp(self, dct_global_param):
        pass

    def set_media_param(self, dct_media_param):
        self.dct_media_param = dct_media_param

    def set_execution_name(self, execution_name):
        self.execution_name = execution_name

    def get_media_param(self, param_name):
        return self.dct_media_param.get(param_name, None)

    def set_original_image(self, image):
        self.original_image = image

    def get_original_image(self):
        return self.original_image

    def notify_output_observers(self, data):
        for obs in self._output_observers:
            obs(data)

    def get_list_output_observer(self):
        return self._output_observers

    def add_output_observer(self, observer):
        self._output_observers.append(observer)

    def remove_output_observer(self, observer):
        self._output_observers.remove(observer)

    def set_publisher(self, publisher):
        self._publisher = publisher
        # create publisher key
        execution_name = self.execution_name
        filter_name = self.name
        key = keys.create_unique_exec_filter_name(execution_name, filter_name)
        self._publish_key = key
        self._publisher.register(key)
        # create callback publisher
        self._cb_publish = self._get_cb_publisher()

    def _add_notification_param(self, param):
        # send from publisher
        if not self._publisher:
            return
        data = {
            "execution": self.execution_name,
            "filter": self.name,
            "param": param.serialize()
        }
        json_data = json.dumps(data)
        self._publisher.publish(keys.get_key_filter_param(), json_data)

    def _get_cb_publisher(self):
        if not self._publisher:
            return
        return self._publisher.get_callback_publish(self._publish_key)

    def get_media(self, name):
        from resource import Resource

        resource = Resource()
        return resource.get_media(name)
예제 #37
0
class ExePy2(Filter):
    """
    Python Example Test #2
    Example filter to test params.
    Show rectangle on each detected face.
    """

    def __init__(self):
        Filter.__init__(self)
        self.dct_color_choose = {"red": (0, 0, 255), "green": (0, 255, 0),
                                 "blue": (255, 0, 0)}
        self.color_rect = self.dct_color_choose["red"]
        self.i_text_size = 1.0
        # add params
        self.show_output = Param("enable_output", True)
        self.show_output.set_description("Enable to show rectangle.")

        self.color_rectangle = Param("color_rectangle", "red",
                                     lst_value=self.dct_color_choose.keys())
        self.color_rectangle.set_description(
            "Change the RGB color of the rectangle.")
        self.color_rectangle.add_group("rectangle")

        self.show_rectangle = Param("show_rectangle", True)
        self.show_rectangle.set_description(
            "Colorize a rectangle around the face.")
        self.show_rectangle.add_group("rectangle")

        self.border_rec_size = Param("border_rec_size", 3, min_v=1, max_v=9)
        self.border_rec_size.set_description(
            "Change the border size of the rectangle.")
        self.border_rec_size.add_group("rectangle")

        self.show_text = Param("enable_text", True)
        self.show_text.set_description("Show text upper the rectangle.")
        self.show_text.add_group("message")

        self.text_face = Param("text_face", "")
        self.text_face.set_description("The text to write on the rectangle.")
        self.text_face.add_group("message")

        self.text_size = Param("text_size", self.i_text_size, min_v=0.1,
                               max_v=4.9)
        self.text_size.set_description("Change the text size.")
        self.text_size.add_group("message")

        self.nb_face = 1
        # linux path
        path_frontal_face = os.path.join('/', 'usr', 'share', 'opencv',
                                         'haarcascades',
                                         'haarcascade_frontalface_alt.xml')
        self.face_detect_name = os.path.join('data', 'facedetect',
                                             path_frontal_face)
        self.face_cascade = cv2.CascadeClassifier()
        self.face_cascade.load(self.face_detect_name)

    def configure(self):
        self.color_rect = self.dct_color_choose[self.color_rectangle.get()]
        self.i_text_size = self.text_size.get()

    def execute(self, image):
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        cv2.equalizeHist(gray, gray)
        faces = self.face_cascade.detectMultiScale(gray, 1.1, 2,
                                                   0 | cv.CV_HAAR_SCALE_IMAGE,
                                                   (30, 30))
        for face in faces:
            self.draw_rectangle(image, face, self.color_rect, self.i_text_size)
            self.nb_face = 1

        return image

    def draw_rectangle(self, image, coord, color, txt_size):
        x, y, w, h = coord
        min_y = y
        if min_y < 0:
            min_y = 0
        min_face_y = min_y - 10
        if min_face_y < 0:
            min_face_y = 0
        max_y = y + h
        if max_y > image.shape[0]:
            max_y = image.shape[0]
        min_x = x
        if min_x < 0:
            min_x = 0
        max_x = x + w
        if max_x > image.shape[1]:
            max_x = image.shape[1]

        if self.show_rectangle.get():
            cv2.rectangle(image, (min_x, min_y), (max_x, max_y), color,
                          thickness=self.border_rec_size.get())
        if self.show_text.get():
            text = "%s.%s" % (self.nb_face, self.text_face.get())
            cv2.putText(image, text, (min_x, min_face_y),
                        cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, txt_size,
                        color)

        # note: >> 2 == / 2
        c_x = (max_x - min_x) >> 2 + min_x
        c_y = (max_y - min_y) >> 2 + min_y
        if self.show_output.get():
            self.notify_output_observers(
                "face detect no %d : x=%d, y=%d" % (self.nb_face, c_x, c_y))
        self.nb_face += 1
예제 #38
0
class Webcam(MediaStreaming):
    """Return images from the webcam."""
    def __init__(self, config):
        # Go into configuration/template_media for more information
        self.config = Configuration()
        self.own_config = config
        super(Webcam, self).__init__()
        self.media_name = config.name
        self.run = True
        self.video = None
        video = cv2.VideoCapture(config.no)
        if video.isOpened():
            self._is_opened = True
            video.release()

        self._create_params()

        self.deserialize(self.config.read_media(self.get_name()))

    def _create_params(self):
        self.dct_params = {}

        default_resolution_name = "800x600"
        self.dct_resolution = {
            default_resolution_name: (800, 600),
            "320x240": (320, 240),
            "640x480": (640, 480),
            "1024x768": (1024, 768),
            "1280x960": (1280, 960),
            "1280x1024": (1280, 1024)
        }
        self.param_resolution = Param("resolution",
                                      default_resolution_name,
                                      lst_value=self.dct_resolution.keys())
        self.param_resolution.add_notify(self.reload)

        default_fps_name = "30"
        self.dct_fps = {default_fps_name: 30, "15": 15, "7.5": 7.5}
        self.param_fps = Param("fps",
                               default_fps_name,
                               lst_value=self.dct_fps.keys())
        self.param_fps.add_notify(self.reload)

    def open(self):
        try:
            shape = self.dct_resolution[self.param_resolution.get()]
            fps = self.dct_fps[self.param_fps.get()]

            # TODO check argument video capture
            self.video = cv2.VideoCapture(self.own_config.no)
            self.video.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, shape[0])
            self.video.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, shape[1])
            self.video.set(cv2.cv.CV_CAP_PROP_FPS, fps)
        except BaseException as e:
            log.printerror_stacktrace(
                logger, "Open camera %s: %s" % (self.get_name(), e))
            return False
        # call open when video is ready
        return MediaStreaming.open(self)

    def next(self):
        run, image = self.video.read()
        if not run:
            raise StopIteration
        return image

    def close(self):
        MediaStreaming.close(self)
        if self.video:
            self.video.release()
        self._is_opened = False
        return True
예제 #39
0
class PygameCam(MediaStreaming):
    """Return images from the webcam."""
    def __init__(self, config):
        # Go into configuration/template_media for more information
        self.config = Configuration()
        self.own_config = config
        super(PygameCam, self).__init__()
        self.media_name = config.name
        self.run = True
        self.video = None
        self.thread_image = None
        pygame.init()
        pygame.camera.init()

        self._create_params()
        self.deserialize(self.config.read_media(self.get_name()))
        self.cam = None
        self._is_opened = True
        self.image = None

    def _create_params(self):
        default_resolution_name = "800x600"
        self.dct_resolution = {
            default_resolution_name: (800, 600),
            "320x240": (320, 240),
            "640x480": (640, 480),
            "1024x768": (1024, 768),
            "1280x960": (1280, 960),
            "1280x1024": (1280, 1024)
        }
        self.param_resolution = Param("resolution",
                                      default_resolution_name,
                                      lst_value=self.dct_resolution.keys())
        self.param_resolution.add_notify(self.reload)

        default_fps_name = "30"
        self.dct_fps = {default_fps_name: 30, "15": 15, "7.5": 7.5}
        self.param_fps = Param("fps",
                               default_fps_name,
                               lst_value=self.dct_fps.keys())
        self.param_fps.add_notify(self.reload)

    def open(self):
        try:
            shape = self.dct_resolution[self.param_resolution.get()]
            fps = self.dct_fps[self.param_fps.get()]
            self.video = pygame.camera.Camera(self.own_config.path, shape)
            self.video.start()
            self.thread_image = True
            thread.start_new_thread(self.update_image, ())
        except BaseException as e:
            log.printerror_stacktrace(
                logger, "Open camera %s: %s" % (self.get_name(), e))
            return False
        # call open when video is ready
        return MediaStreaming.open(self)

    def update_image(self):
        while self.thread_image:
            image_surface = self.video.get_image()
            image = pygame.surfarray.pixels3d(image_surface)
            image = np.rot90(image, 3)
            copy_image = np.zeros(image.shape, np.float32)
            copy_image = cv2.cvtColor(image, cv2.cv.CV_BGR2RGB, copy_image)
            self.image = copy_image

    def next(self):
        return self.image

    def close(self):
        MediaStreaming.close(self)
        self.thread_image = False
        # TODO add semaphore?
        self.video.stop()
        self._is_opened = False
        return True
예제 #40
0
 def __init__(self):
     Filter.__init__(self)
     self.threshold = Param("Threshold", 100, min_v=0, max_v=255)
     self.technique = Param("Technique", 0, min_v=0, max_v=2)
예제 #41
0
 def __init__(self):
     Filter.__init__(self)
     self.kernel_height = Param("Kernel Height", 10, min_v=1, max_v=256)
     self.kernel_width = Param("Kernel Width", 10, min_v=1, max_v=256)
     self.area_min = Param("Area Min", 3200, min_v=1)
     self.configure()
예제 #42
0
 def get_param_filterchain(self, execution_name, filter_name, param_name):
     param_ser = self.rpc.get_param_filterchain(execution_name, filter_name,
                                                param_name)
     return Param("temp", None, serialize=param_ser)
예제 #43
0
 def get_param_media(self, media_name, param_name):
     param_ser = self.rpc.get_param_media(media_name, param_name)
     return Param("temp", None, serialize=param_ser)
예제 #44
0
 def __init__(self):
     Filter.__init__(self)
     self.canny1 = Param("Canny1", 50, min_v=1, max_v=256)
     self.canny2 = Param("Canny2", 200, min_v=1, max_v=256)
     self.rho = Param("Rho", 1, min_v=1, max_v=256)
     self.theta = Param("Theta", 180, min_v=0, max_v=360)
     self.threshold = Param("Threshold", 100, min_v=1, max_v=256)
     self.line_size = Param("Line Size", 1000, min_v=1, max_v=2000)
예제 #45
0
class FaceDetection(Filter):
    """Detect faces and eyes"""
    def __init__(self):
        Filter.__init__(self)
        self.nb_face = 1
        eye_xml = 'haarcascade_eye_tree_eyeglasses.xml'
        self.eye_detect_name = os.path.join('/', 'usr', 'share', 'opencv',
                                            'haarcascades', eye_xml)
        self.face_detect_nam = os.path.join('/', 'usr', 'share', 'opencv',
                                            'haarcascades',
                                            'haarcascade_frontalface_alt.xml')
        self.eye_cascade = cv2.CascadeClassifier()
        self.face_cascade = cv2.CascadeClassifier()
        self.eye_cascade.load(self.eye_detect_name)
        self.face_cascade.load(self.face_detect_name)
        self.show_rectangle = Param("show_rectangle", True)

        # To share parameter between filter, create it with :
        self.add_shared_param(Param("width", 3, min_v=1, max_v=10))
        # On the execution, use it like this :
        # param = self.get_shared_params("width")

    def configure(self):
        # This is called when param is modify
        pass

    def execute(self, image):
        gray = cv2.cvtColor(image, cv.CV_BGR2GRAY)
        cv2.equalizeHist(gray, gray)
        faces = self.face_cascade.detectMultiScale(gray, 1.1, 2,
                                                   0 | cv.CV_HAAR_SCALE_IMAGE,
                                                   (30, 30))
        for face in faces:
            faceimg = self.draw_rectangle(image, face, (0, 0, 255))
            self.nb_face = 1

        return image

    def draw_rectangle(self, image, coord, color):
        x, y, w, h = coord
        miny = y
        if miny < 0:
            miny = 0
        maxy = y + h
        if maxy > image.shape[0]:
            maxy = image.shape[0]
        minx = x
        if minx < 0:
            minx = 0
        maxx = x + w
        if maxx > image.shape[1]:
            maxx = image.shape[1]

        if self.show_rectangle.get():
            cv2.rectangle(image, (minx, miny), (maxx, maxy), color, 3)

        c_x = (maxx - minx) / 2 + minx
        c_y = (maxy - miny) / 2 + miny
        self.notify_output_observers("facedetect%d : x=%d, y=%d" %
                                     (self.nb_face, c_x, c_y))
        self.nb_face += 1
        return image[miny:maxy, minx:maxx]
예제 #46
0
 def __init__(self):
     Filter.__init__(self)
     self.angle = Param('angle', 0, max_v=3, min_v=0)
예제 #47
0
 def __init__(self):
     Filter.__init__(self)
     self.red = Param("red", 100, min_v=0, max_v=255)
     self.green = Param("green", 100, min_v=0, max_v=255)
     self.blue = Param("blue", 100, min_v=0, max_v=255)
예제 #48
0
 def __init__(self):
     Filter.__init__(self)
     self.param_str = Param("param_str", "")
예제 #49
0
    def __init__(self):
        Filter.__init__(self)
        self.topleftx = Param("Top Left X", 0, min_v=0, max_v=640)
        self.toplefty = Param("Top Left TY", 0, min_v=0, max_v=480)
        self.bottomleftx = Param("Bottom Left X", 100, min_v=0, max_v=640)
        self.bottomlefty = Param("Bottom Left Y", 480, min_v=0, max_v=480)
        self.toprightx = Param("Top Right X", 640, min_v=0, max_v=640)
        self.toprighty = Param("Top Right Y", 0, min_v=0, max_v=480)
        self.bottomrightx = Param("Bottom Right X", 540, min_v=0, max_v=640)
        self.bottomrighty = Param("Bottom Right Y", 480, min_v=0, max_v=480)

        self.mmat = None
        self.configure()
예제 #50
0
 def __init__(self):
     Filter.__init__(self)
     self.diameter = Param("Diameter", 10, min_v=0, max_v=255)
     self.sigma_color = Param("Sigma Color", 0, min_v=0, max_v=255)
     self.sigma_space = Param("Sigma Space", 0, min_v=0, max_v=255)
예제 #51
0
 def __init__(self):
     Filter.__init__(self)
     self.kernel_width = Param("width", 3, min_v=1, max_v=10)
     self.kernel_height = Param("height", 3, min_v=1, max_v=10)
     self.kernel_height.set_description("kernel's height")
     self.kernel_width.set_description("kernel's width")
예제 #52
0
class ImageGenerator(MediaStreaming):
    """Return a generate image."""
    def __init__(self, config):
        # Go into configuration/template_media for more information
        self.config = Configuration()
        self.own_config = config
        super(ImageGenerator, self).__init__()
        self.media_name = config.name
        self.run = True
        self._is_opened = True

        self._create_params()

        self.deserialize(self.config.read_media(self.get_name()))

    def _create_params(self):
        default_width = 800
        self.param_width = Param("width", default_width, min_v=1, max_v=1200)
        self.param_width.add_group("Resolution")
        self.param_width.set_description("Change width resolution.")

        default_height = 600
        self.param_height = Param("height",
                                  default_height,
                                  min_v=1,
                                  max_v=1200)
        self.param_height.add_group("Resolution")
        self.param_height.set_description("Change height resolution.")

        default_fps = 30
        self.param_fps = Param("fps", default_fps, min_v=1, max_v=100)
        self.param_fps.set_description("Change frame per second.")

        self.param_color_r = Param("color_r", 0, min_v=0, max_v=255)
        self.param_color_r.add_group("Color")
        self.param_color_r.set_description("Change red color.")

        self.param_color_g = Param("color_g", 0, min_v=0, max_v=255)
        self.param_color_g.add_group("Color")
        self.param_color_g.set_description("Change green color.")

        self.param_color_b = Param("color_b", 0, min_v=0, max_v=255)
        self.param_color_b.add_group("Color")
        self.param_color_b.set_description("Change blue color.")

        self.param_auto_color = Param("auto-change-color", False)
        self.param_auto_color.set_description(
            "Change the color automatically.")
        self.param_auto_color.add_group("Color")

        self.param_random_green = Param("pooling_green_random", False)
        self.param_random_green.set_description(
            "Active pooling update of green color with random value.")
        self.param_random_green.add_notify(self._active_green_pooling)
        self.param_random_green.add_group("Color")

        self.param_transpose_r_color = Param("Transpose red color", None)
        self.param_transpose_r_color.set_description(
            "Copy the red color on others color.")
        self.param_transpose_r_color.add_notify(self._transpose_red_color)
        self.param_transpose_r_color.add_group("Color")

        self.param_freeze = Param("freeze", False)
        self.param_freeze.set_description("Freeze the stream.")

    def next(self):
        if self.param_freeze.get():
            return

        width = self.param_width.get()
        height = self.param_height.get()
        color_r = self.param_color_r.get()
        color_g = self.param_color_g.get()
        color_b = self.param_color_b.get()

        if self.param_auto_color.get():
            color_r += 1
            if color_r > 255:
                color_r = 0
            color_g += 2
            if color_g > 255:
                color_g = 0
            color_b += 3
            if color_b > 255:
                color_b = 0

            self.param_color_r.set(color_r)
            self.param_color_r.set_lock(True)
            self.param_color_g.set(color_g)
            self.param_color_g.set_lock(True)
            self.param_color_b.set(color_b)
            self.param_color_b.set_lock(True)
        else:
            self.param_color_r.set_lock(False)
            self.param_color_g.set_lock(False)
            self.param_color_b.set_lock(False)

        image = np.zeros((height, width, 3), dtype=np.uint8)

        image[:, :, 0] += color_b
        image[:, :, 1] += color_g
        image[:, :, 2] += color_r
        return image

    def _transpose_red_color(self, param):
        color_r = self.param_color_r.get()
        self.param_color_g.set(color_r)
        self.param_color_b.set(color_r)

    def _active_green_pooling(self, param):
        if param.get():
            self.param_color_g.start_pooling(self._pool_random_green)
        else:
            self.param_color_g.stop_pooling()

    def _pool_random_green(self, param):
        return randrange(255)
예제 #53
0
def py_init_global_param(self, name, value, min=None, max=None):
    param = Param(name, value, min_v=min, max_v=max)
    self.dct_global_param[name] = param
예제 #54
0
 def __init__(self):
     Filter.__init__(self)
     self.x1 = Param('x1', 0, max_v=65535, min_v=0)
     self.y1 = Param('y1', 0, max_v=65535, min_v=0)
     self.x2 = Param('x2', 100, max_v=65535, min_v=0)
     self.y2 = Param('y2', 100, max_v=65535, min_v=0)
예제 #55
0
 def __init__(self):
     Filter.__init__(self)
     self.kernel_height = Param("Kernel Height", 3, min_v=1, max_v=256)
     self.kernel_width = Param("Kernel Width", 3, min_v=1, max_v=256)
     self.sigma_x = Param("Sigma X", 3, min_v=1, max_v=256)
     self.sigma_y = Param("Sigma Y", 3, min_v=1, max_v=256)
예제 #56
0
 def __init__(self):
     Filter.__init__(self)
     self.kernel_erode_height = Param("Kernel Erode Height",
                                      3,
                                      min_v=1,
                                      max_v=255)
     self.kernel_erode_width = Param("Kernel Dilate Width",
                                     3,
                                     min_v=1,
                                     max_v=255)
     self.kernel_dilate_height = Param("Kernel Erode Height",
                                       5,
                                       min_v=1,
                                       max_v=255)
     self.kernel_dilate_width = Param("Kernel Dilate Width",
                                      5,
                                      min_v=1,
                                      max_v=255)
     self.sections = Param("Sections", 5, min_v=1, max_v=10)
     self.min_area = Param("Minimum Area", 1000, min_v=1, max_v=65535)
     self.configure()
예제 #57
0
class Media(PoolParam):
    def __init__(self):
        super(Media, self).__init__()
        # TODO change sleep_time dependant of real fps desire
        self.fps = 30.0
        self.sleep_time = 1 / self.fps
        self.lst_observer = []
        self.thread = None
        self.media_name = None
        self.active_loop = True
        self.is_client_manager = False
        # set publisher
        self._publisher = None
        self.status = None
        self.set_status(MediaStatus.close)
        # add generic param
        self._rotate_param = Param('angle', 0, max_v=3, min_v=0)
        desc = "Rotate the picture. 0 - 90 - 180 - 270"
        self._rotate_param.set_description(desc)
        self._rotate_param.add_group("Generic")

    def destroy(self):
        self.destroy_param()

    def set_publisher(self, publisher):
        self._publisher = publisher
        publisher.register("media.%s" % self.media_name)
        self._cb_publish = self._get_cb_publisher()

    def set_is_client_manager(self, is_client_manager):
        self.is_client_manager = is_client_manager

    def is_media_streaming(self):
        # complete it into media_streaming and media_video
        pass

    def is_media_video(self):
        # complete it into media_streaming and media_video
        pass

    def get_type_media(self):
        # complete it into media_streaming and media_video
        # type is Video or Streaming
        pass

    def get_name(self):
        return self.media_name

    def get_status(self):
        return self.status

    def set_status(self, status):
        if not status in MediaStatus.lst_status:
            msg = "Status %s in media %s not supported." % (status,
                                                            self.get_name())
            logger.error(msg)
            self.status = MediaStatus.close
        if self.status != status and self._cb_publish:
            self.status = status
            self._cb_publish({"status": status})

    def __iter__(self):
        return self

    def get_total_frames(self):
        return -1

    def get_info(self):
        fps = int(1 / self.sleep_time) if self.thread else -1
        return {
            "fps": fps,
            "nb_frame": self.get_total_frames(),
            "status": self.get_status()
        }

    def serialize(self, is_config=False):
        return super(Media, self).serialize(is_config=is_config)

    def deserialize(self, data):
        return super(Media, self).deserialize(data)

    def get_real_fps(self):
        if self.thread:
            return self.thread.get_fps()
        return -1

    def open(self):
        # IMPORTANT, if inherit, call this at the end
        # the thread need to be start when device is ready
        logger.info("Open media %s" % self.get_name())
        if self.is_client_manager:
            return True
        if self.thread:
            return False
        self.thread = ThreadMedia(self, self._cb_publish, self._rotate_param)
        self.thread.start()
        return True

    def next(self):
        # edit me in child
        pass

    def reset(self):
        # restore the media
        pass

    def close(self):
        logger.info("Close media %s" % self.get_name())
        self._remove_cb_publisher()
        self.stop_pooling_all_param()
        if self.is_client_manager:
            return True
        if not self.thread:
            return False
        self.thread.stop()
        self.thread = None
        self.status = MediaStatus.close
        return True

    def initialize(self):
        pass

    def reload(self, param=None):
        # TODO do observer and check parameter
        # ignore param_name and value, it's parameter from pool_param
        if not self.thread:
            return True
        status = self.close()
        if not status:
            return False
        # TODO force re-init filterchain
        self.initialize()
        return self.open()

    def change_sleep_time(self, sleep_time):
        self.sleep_time = sleep_time

    def add_observer(self, observer):
        start_media = False
        if not self.lst_observer:
            start_media = True
        self.lst_observer.append(observer)
        if start_media:
            self.open()

    def remove_observer(self, observer):
        if observer in self.lst_observer:
            self.lst_observer.remove(observer)
        else:
            logger.warning("Observer missing into media %s" % self.get_name())
        if not self.lst_observer:
            self.close()

    def notify_observer(self, image):
        # be sure the image is different for all observer
        for observer in self.lst_observer:
            observer(np.copy(image))

    def _add_notification_param(self, param):
        # send from publisher
        if not self._publisher:
            return
        data = {"media": self.media_name, "param": param.serialize()}
        json_data = json.dumps(data)
        self._publisher.publish(keys.get_key_media_param(), json_data)

    def set_loop_enable(self, enable):
        self.active_loop = enable

    def _get_cb_publisher(self):
        if not self._publisher:
            return
        key = self.get_name()
        return self._publisher.get_callback_publish("media.%s" % key)

    def _remove_cb_publisher(self):
        if not self._publisher:
            return
예제 #58
0
 def __init__(self):
     Filter.__init__(self)
     self.threshold1 = Param("Threshold1", 10, min_v=0, max_v=255)
     self.threshold2 = Param("Threshold2", 100, min_v=0, max_v=255)
예제 #59
0
    def __init__(self):
        Filter.__init__(self)
        self.dct_color_choose = {"red": (0, 0, 255), "green": (0, 255, 0),
                                 "blue": (255, 0, 0)}
        self.color_rect = self.dct_color_choose["red"]
        self.i_text_size = 1.0
        # add params
        self.show_output = Param("enable_output", True)
        self.show_output.set_description("Enable to show rectangle.")

        self.color_rectangle = Param("color_rectangle", "red",
                                     lst_value=self.dct_color_choose.keys())
        self.color_rectangle.set_description(
            "Change the RGB color of the rectangle.")
        self.color_rectangle.add_group("rectangle")

        self.show_rectangle = Param("show_rectangle", True)
        self.show_rectangle.set_description(
            "Colorize a rectangle around the face.")
        self.show_rectangle.add_group("rectangle")

        self.border_rec_size = Param("border_rec_size", 3, min_v=1, max_v=9)
        self.border_rec_size.set_description(
            "Change the border size of the rectangle.")
        self.border_rec_size.add_group("rectangle")

        self.show_text = Param("enable_text", True)
        self.show_text.set_description("Show text upper the rectangle.")
        self.show_text.add_group("message")

        self.text_face = Param("text_face", "")
        self.text_face.set_description("The text to write on the rectangle.")
        self.text_face.add_group("message")

        self.text_size = Param("text_size", self.i_text_size, min_v=0.1,
                               max_v=4.9)
        self.text_size.set_description("Change the text size.")
        self.text_size.add_group("message")

        self.nb_face = 1
        # linux path
        path_frontal_face = os.path.join('/', 'usr', 'share', 'opencv',
                                         'haarcascades',
                                         'haarcascade_frontalface_alt.xml')
        self.face_detect_name = os.path.join('data', 'facedetect',
                                             path_frontal_face)
        self.face_cascade = cv2.CascadeClassifier()
        self.face_cascade.load(self.face_detect_name)
예제 #60
0
 def _create_params(self):
     default_ipc_name = "ipc://%s" % self.device_name
     self.param_ipc_name = Param(self.key_ipc_name, default_ipc_name)
     self.param_ipc_name.add_notify(self.reload)