Example #1
0
def process(address, port, cam_num, verbose=False):
    """Image processing loop

    Args:
        address (string): ip address to send stream frames
        port (string): ip port to send stream frames
        cam_num (integer): camera number to stream
        verbose (bool, optional): [description]. Defaults to False.
    """
    print(f'PROCESS = {address}, {port}, {cam_num}')
    with ImageCapture(int(cam_num)) as cam:
        cam.start()

        context = zmq.Context()
        socket = context.socket(zmq.PUB)
        socket.connect('tcp://' + address + ':' + port)
        socket.set_hwm(1)

        stream = True

        running = True

        fps = Rate()
        fps.start()

        lastframecount = 0

        frame = Frame()
        frame.srcid = cam.src

        next_time = time.perf_counter() + 1.0

        while running:
            frame.count, frame.img, frame.timestamp = cam.read()
            frame.camfps = cam.fps.fps()

            if time.perf_counter() > next_time:
                next_time += 1.0
                if verbose:
                    print(f'FPS = {frame.camfps}')

            if verbose and frame.img is not None:
                cv2.imshow(f'CAM {frame.srcid}', frame.img)
                cv2.waitKey(1)

            if frame.count is not None:
                if frame.count != lastframecount:
                    lastframecount = frame.count
                    if stream:
                        socket.send_pyobj(frame)

                    fps.update()
                    frame.streamfps = fps.fps()
Example #2
0
    count += 1
    camfps.update()

    # TODO: Pass image to processor and get previous meta
    # If the image processor is busy it will simply ignore this image
    # and return the previous meta
    (procCount, meta) = processor.process(img, count)
    processor.overlay(meta, img)

    timestamp_string = datetime.datetime.fromtimestamp(
        timestamp.timestamp(), datetime.timezone.utc).isoformat()
    cv2.putText(img, timestamp_string, (0, 20), cv2.FONT_HERSHEY_PLAIN, 1,
                (0, 255, 0), 1)

    cv2.putText(
        img, "CamFPS : {:.1f}".format(camfps.fps()) + " Frame: " + str(count),
        (0, 40), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0), 1)
    cv2.putText(
        img, "ProcFPS: {:.1f}".format(processor.fps.fps()) + " Frame: " +
        str(procCount) + " (" + str(procCount - count) + ")", (0, 60),
        cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0), 1)

    if stream == True:
        encoded, buffer = cv2.imencode('.jpg', img)
        socket.send(buffer)

    if record == True:
        videofile.write(img)

    cv2.imshow('main', img)
Example #3
0
    def startMain(self):

        #set  queue size
        self.cam_queue = mp.Queue(maxsize=7)

        self.stopbit = mp.Event()
        self.camProcess = vs.StreamCapture(self.camlink, self.stopbit,
                                           self.cam_queue, self.framerate)
        self.t = Thread(target=self.camProcess.run)
        self.t.setDaemon = True
        self.t.start()

        context = zmq.Context()
        socket = context.socket(zmq.PUB)
        socket.connect('tcp://' + self.address + ':' + self.port)
        socket.set_hwm(1)

        fps = Rate()
        fps.start()

        lastframecount = 0

        frame = Frame()
        frame.srcid = self.src

        next_time = time.perf_counter() + 1.0

        try:
            self.fps.start()
            while True:

                if not self.cam_queue.empty():
                    # print('Got frame')
                    cmd, val = self.cam_queue.get()
                    self.timestamp = datetime.datetime.now()
                    self.fps.update()

                    # if cmd == vs.StreamCommands.RESOLUTION:
                    #     pass #print(val)

                    if cmd == vs.StreamCommands.FRAME:
                        frame.count += 1
                        frame.img = val
                        frame.timestamp = self.timestamp
                        frame.camfps = self.fps.fps()

                        if time.perf_counter() > next_time:
                            next_time += 1.0
                            if self.verbose:
                                print(
                                    f'FPS = {frame.camfps:.2f}  {frame.streamfps:.2f}'
                                )

                        if self.verbose and frame.img is not None:
                            cv2.imshow(f'CAM {frame.srcid}', frame.img)
                            cv2.waitKey(1)

                        if frame.count is not None:
                            if frame.count != lastframecount:
                                lastframecount = frame.count
                                socket.send_pyobj(frame)

                                fps.update()
                                frame.streamfps = fps.fps()
                else:
                    time.sleep(1 / self.framerate)

        except KeyboardInterrupt:
            print('Caught Keyboard interrupt')

        except Exception as e:
            print('Caught Main Exception')
            print(e)

        self.stopCamStream()
        cv2.destroyAllWindows()
Example #4
0
class mainStreamClass:
    def __init__(self, address, port, cam_num, verbose=False):

        self.src = int(cam_num)
        self.count = 0
        self.fps = Rate()
        self.timestamp = 0
        self.address = address
        self.port = port
        self.verbose = verbose

        self.camProcess = None
        self.cam_queue = None
        self.stopbit = None
        self.camlink = f'rtsp://{rtsp_user}:{password}@{rtsp_ip}//h264Preview_0{self.src}_sub'
        self.framerate = 7

    def startMain(self):

        #set  queue size
        self.cam_queue = mp.Queue(maxsize=7)

        self.stopbit = mp.Event()
        self.camProcess = vs.StreamCapture(self.camlink, self.stopbit,
                                           self.cam_queue, self.framerate)
        self.t = Thread(target=self.camProcess.run)
        self.t.setDaemon = True
        self.t.start()

        context = zmq.Context()
        socket = context.socket(zmq.PUB)
        socket.connect('tcp://' + self.address + ':' + self.port)
        socket.set_hwm(1)

        fps = Rate()
        fps.start()

        lastframecount = 0

        frame = Frame()
        frame.srcid = self.src

        next_time = time.perf_counter() + 1.0

        try:
            self.fps.start()
            while True:

                if not self.cam_queue.empty():
                    # print('Got frame')
                    cmd, val = self.cam_queue.get()
                    self.timestamp = datetime.datetime.now()
                    self.fps.update()

                    # if cmd == vs.StreamCommands.RESOLUTION:
                    #     pass #print(val)

                    if cmd == vs.StreamCommands.FRAME:
                        frame.count += 1
                        frame.img = val
                        frame.timestamp = self.timestamp
                        frame.camfps = self.fps.fps()

                        if time.perf_counter() > next_time:
                            next_time += 1.0
                            if self.verbose:
                                print(
                                    f'FPS = {frame.camfps:.2f}  {frame.streamfps:.2f}'
                                )

                        if self.verbose and frame.img is not None:
                            cv2.imshow(f'CAM {frame.srcid}', frame.img)
                            cv2.waitKey(1)

                        if frame.count is not None:
                            if frame.count != lastframecount:
                                lastframecount = frame.count
                                socket.send_pyobj(frame)

                                fps.update()
                                frame.streamfps = fps.fps()
                else:
                    time.sleep(1 / self.framerate)

        except KeyboardInterrupt:
            print('Caught Keyboard interrupt')

        except Exception as e:
            print('Caught Main Exception')
            print(e)

        self.stopCamStream()
        cv2.destroyAllWindows()

    def stopCamStream(self):
        print('in stopCamStream')

        if self.stopbit is not None:
            self.stopbit.set()
            while not self.cam_queue.empty():
                try:
                    _ = self.cam_queue.get()
                except:
                    break
                self.cam_queue.close()

            self.camProcess.join()

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        print(f'\n\n\n\nReleasing Camera {self.src}')
        try:
            self.stopCamStream()
        except:
            pass
Example #5
0
frame = Frame()
frame.srcid = cam.src

while running:
    frame.count, frame.img, frame.timestamp = cam.read()
    frame.camfps = cam.fps.fps()
    
    if (frame.count != None):
        if (frame.count != lastframecount):
            lastframecount = frame.count
            if stream:
                #_, frame.jpg = cv2.imencode('.jpeg', img)
                socket.send_pyobj(frame)

            try:    
                cv2.imshow(f'Camera {frame.srcid}', frame.img)
            except Exception as e:
                print(f'[WARNING] : {repr(e)}')            
            fps.update()
            frame.streamfps = fps.fps()
                
    key = cv2.waitKey(1)
    
    if key == 27:
        running = False
    elif key == ord('s'):
        stream = not stream