Beispiel #1
0
    def cycle_(self):
        # NOTE: enable this to see if your multiprocess is alive
        self.logger.debug("cycle_ starts")
        index, meta = self.client.pullFrame()
        if (index is None):
            self.logger.debug("cycle_ : client timed out..")
            return

        self.logger.debug("cycle_ : client index = %s", index)
        if meta.size < 1:
            return

        data = self.client.shmem_list[index][0:meta.size]
        img = data.reshape((meta.height, meta.width, 3))
        """ # WARNING: the x-server doesn't like this, i.e., we're creating a window from a separate python multiprocess, so the program will crash
        print(self.pre,"Visualizing with OpenCV")
        cv2.imshow("openCV_window",img)
        cv2.waitKey(1)
        """
        self.logger.debug("cycle_ : got frame %s", img.shape)
        result = self.analyzer(img)

        img_ = img.copy()

        if self.parameters:
            # what we have in parameters, depends on the
            # analyzer video widget class, i.e. on the widget that
            # interacts with the user for defining the machine vision parameters
            # the class is defined in the class member "analyzer_video_widget_class"
            # and instantiated by MVisionContainer
            # here we're using
            # valkka.live.qt.widget.LineCrossingVideoWidget
            if "line" in self.parameters:
                line = self.parameters["line"]
                # print("line",line)
                start = (int(line[0][0] * img_.shape[1]),
                         int((line[0][1]) * img_.shape[0]))
                end = (int(line[1][0] * img_.shape[1]),
                       int((line[1][1]) * img_.shape[0]))
                # print("start", start)
                # print("end",end)
                # # cross-check the line defined in the interactive qt widget
                cv2.line(img_, start, end, (0, 255, 0), 8)

        if self.qt_server is not None:
            self.logger.info("cycle_ : pushing frame to server")
            # print(">>", img_[0:10])
            self.qt_server.pushFrame(img_, meta.slot, meta.mstimestamp)

        # NOTE: you could use and combine several analyzers here, say first see if there is movement and then do the rest
        # print(self.pre,">>>",data[0:10])
        if (result == MovementDetector.state_same):
            pass
        elif (result == MovementDetector.state_start):
            self.send_out__(MessageObject("start_move"))
        elif (result == MovementDetector.state_stop):
            self.send_out__(MessageObject("stop_move"))
 def activate(self, **kwargs):
     # save values to front-end also:
     self.shmem_name = kwargs["shmem_name"]
     self.n_buffer = kwargs["n_buffer"]
     self.image_dimensions = kwargs["image_dimensions"]
     # this sets the values at back-end:
     self.sendMessageToBack(MessageObject("activate", **kwargs))
Beispiel #3
0
    def cycle_(self):
        # NOTE: enable this to see if your multiprocess is alive
        self.logger.info("cycle_ starts")
        """
        index, isize = self.client.pull()
        if (index is None):
            self.logger.info("Client timed out..")
            pass
        else:
            self.logger.info("Client index, size =",index, isize)
            data = self.client.shmem_list[index]
            img = data.reshape(
                (self.image_dimensions[1], self.image_dimensions[0], 3))
            result = self.analyzer(img)
        """
        index, meta = self.client.pullFrame()
        if (index is None):
            self.logger.debug("Client timed out..")
            return

        self.logger.debug("Client index = %s", index)
        if meta.size < 1:
            return

        data = self.client.shmem_list[index][0:meta.size]
        img = data.reshape((meta.height, meta.width, 3))
        self.logger.debug("got frame %s", img.shape)
        result = self.analyzer(img)  # does something .. returns something ..

        if self.qt_server is not None:
            self.logger.info("pushing frame to server")
            self.qt_server.pushFrame(img, meta.slot, meta.mstimestamp)

        if (result != ""):
            self.send_out__(MessageObject("text", message=result))
 def unregisterClient(self, **kwargs):
     self.n_clients -= 1
     if self.n_clients < 1:
         self.n_clients = 0
     self.sendMessageToBack(MessageObject("unregisterClient", **kwargs))
     self.logger.debug(
         "unregisterClient: frontend: number of clients is %s",
         self.n_clients)
    def unsetMasterProcess(self):  # , master_process):
        if self.master_process is None:
            self.logger.warning("unsetMasterProcess: none set")
            return

        self.sendMessageToBack(MessageObject("unsetMasterProcess"))
        self.master_process.unregisterClient(ipc_index=self.ipc_index)
        singleton.ipc.release(self.ipc_index)
        self.ipc_index = None
        self.master_process = None
 def registerClient(self, **kwargs):
     # keep the books on number of clients at frontend as well
     self.n_clients += 1
     if self.n_clients > self.max_clients:
         self.logger.warning("no more clients available: max is %s",
                             self.max_clients)
         self.n_clients = self.max_clients
         return
     self.sendMessageToBack(MessageObject("registerClient", **kwargs))
     self.logger.debug("registerClient: frontend: number of clients is %s",
                       self.n_clients)
    def setMasterProcess(self, master_process=None):
        # ipc_index, n_buffer, image_dimensions, shmem_name # TODO
        self.master_process = master_process
        self.ipc_index = singleton.ipc.reserve()

        # first, create the server
        self.sendMessageToBack(
            MessageObject("setMasterProcess", ipc_index=self.ipc_index))

        # this will create the client:
        # self.n_buffer etc. have been set by call to self.activate
        master_process.registerClient(ipc_index=self.ipc_index,
                                      n_buffer=self.n_buffer,
                                      image_dimensions=self.image_dimensions,
                                      shmem_name=self.shmem_name_server)
 def c__requestQtShmemServer(self, **kwargs):
     self.logger.debug("shmem server requested")
     # should be called only after the shmem client has been instantiated
     self.shmem_name_qt_server = self.shmem_name + "_qt_server"
     self.qt_server = ShmemRGBServer(
         name=self.shmem_name_qt_server,
         n_ringbuffer=self.n_buffer,  # size of ring buffer
         width=self.image_dimensions[0],
         height=self.image_dimensions[1],
         verbose=self.shmem_verbose)
     # inform frontend & widgets that the shmem server is available:
     self.send_out__(
         MessageObject(
             "shmem_server",  # this is routed to the qt signal with the same name
             shmem_name=self.shmem_name_qt_server,
             shmem_n_buffer=self.n_buffer,
             width=self.image_dimensions[0],
             height=self.image_dimensions[1],
             verbose=self.shmem_verbose))
 def releaseQtShmemServer(self):
     kwargs = {}
     self.sendMessageToBack(MessageObject("releaseQtShmemServer", **kwargs))
 def updateAnalyzerParameters(self, kwargs):
     self.parameters = kwargs
     self.logger.debug("updateAnalyzerParameters %s", kwargs)
     self.sendMessageToBack(
         MessageObject("updateAnalyzerParameters", **kwargs))
 def c__ping(self, message=""):
     print("c__ping:", message)
     self.send_out__(MessageObject("pong", lis=[1, 2, 3]))
 def ping(self, message=""):
     """Test call
     """
     self.sendMessageToBack(MessageObject("ping", message=message))
 def deactivate(self):
     self.sendMessageToBack(MessageObject("deactivate"))
Beispiel #14
0
    def cycle_(self):
        lis=[]
        self.logger.debug("cycle_ starts")
        """ old API
        index, isize = self.client.pull()
        if (index is None):
            self.logger.debug("Client timed out..")
            pass
        else:
            self.logger.debug("Client index, size = %s, %s", index, isize)
            data = self.client.shmem_list[index]
            img = data.reshape(
                (self.image_dimensions[1], self.image_dimensions[0], 3))
        """
        index, meta = self.client.pullFrame()
        if (index is None):
            self.logger.debug("Client timed out..")
            return
        
        self.logger.debug("Client index = %s", index)
        if (meta.size < 1) or (self.analyzer is None):
            return

        data = self.client.shmem_list[index][0:meta.size]
        img = data.reshape(
            (meta.height, meta.width, 3))
        lis = self.analyzer(img)

        if self.qt_server is not None:
            self.logger.info("pushing frame to server")
            self.qt_server.pushFrame(
                img,
                meta.slot,
                meta.mstimestamp
            )

        """
        print("img.shape=",img.shape)
        for l in lis:
            print(l)
        """
        
        """ # list looks like this:
        [ ('dog', 99, 134, 313, 214, 542), 
        ('truck', 91, 476, 684, 81, 168),
        ('bicycle', 99, 99, 589, 124, 447)
        ]
        """
        
        object_list=[]
        bbox_list=[]
        for l in lis:
            object_list.append(l[0])
            # """
            bbox_list.append((
                l[2]/img.shape[1],  # from pixels to fractional coordinates
                l[3]/img.shape[1],
                l[4]/img.shape[0],
                l[5]/img.shape[0]
            ))
            # """
            
        if (hasattr(self, "warning_message")):
            object_list.append(self.warning_message)
  
        # print("YoloV3",bbox_list)
        #if (len(lis)>0):
        # self.sendSignal_(name="objects", object_list=object_list)
        # self.sendSignal_(name="bboxes",  bbox_list=bbox_list)
        self.send_out__(MessageObject("objects", object_list = object_list))
        self.send_out__(MessageObject("bboxes", bbox_list = bbox_list))
Beispiel #15
0
    def cycle_(self):
        lis = []
        self.logger.debug("cycle_ starts")
        index, meta = self.client.pullFrame()
        if (index is None):
            self.logger.debug("Client timed out..")
            return

        self.logger.debug("Client index = %s", index)
        if meta.size < 1:
            return

        data = self.client.shmem_list[index][0:meta.size]
        img = data.reshape((meta.height, meta.width, 3))

        scale = numpy.array([meta.height, meta.width])
        self.logger.debug("cycle_: got frame %s", img.shape)

        img_ = img.copy()

        if self.server is not None:
            self.logger.debug("cycle_ : pushing to server")
            self.server.pushFrame(img, meta.slot, meta.mstimestamp)
            # receive results from master process
            replies = self.master_pipe.recv()
            self.logger.debug("reply from master process: %s", replies)
            if replies is not None:
                object_list = []
                bbox_list = []
                for reply in replies:
                    if isinstance(reply, str):
                        object_list.append(reply)
                    else:
                        tag = reply[0]
                        x0 = reply[1]
                        x1 = reply[2]
                        y0 = reply[3]
                        y1 = reply[4]

                        object_list.append(tag)
                        bbox_list.append((x0, x1, y0, y1))
                        # yolo: origo at left lower corner
                        y0 = 1 - y0  # numpy / opencv: origo at left upper corner
                        y1 = 1 - y1

                        # start: lower left corner of the box
                        start = (int(x0 * meta.width), int(y0 * meta.height))
                        # end: upper right corner of the box
                        end = (int(x1 * meta.width), int(y1 * meta.height))

                        linew = 3  # object box linewidth
                        label = (int(x0 * meta.width), int(y1 * meta.height) +
                                 self.baseline + linew + 2
                                 )  # object label coordinates
                        """
                        print(">", x0, x1, y0, y1)
                        print("width, height", meta.width, meta.height)
                        print("start", start)
                        print("end", end)
                        """
                        color = (255, 0, 0)
                        img_ = cv2.rectangle(img_, start, end, color, linew)
                        cv2.putText(img_, tag, label, cv2.FONT_HERSHEY_SIMPLEX,
                                    1, color, 2, cv2.LINE_AA)

                self.send_out__(
                    MessageObject("objects", object_list=object_list))
                self.send_out__(MessageObject("bboxes", bbox_list=bbox_list))
        """
        reply can be:
        
        - None
        - A list
            - a tuple
                (nametag, x, y, w, h)
            - string
        """
        if self.qt_server is not None:
            self.logger.info("pushing frame to server")
            self.qt_server.pushFrame(img_, meta.slot, meta.mstimestamp)
Beispiel #16
0
 def resetAnalyzerState(self):
     print(">resetAnalyzerState")
     self.sendMessageToBack(MessageObject("resetAnalyzerState", **{}))