Ejemplo n.º 1
0
def startStream():

	# 使用服务器的套接字地址初始化 ImageSender 对象
	sender = imagezmq.ImageSender(connect_to='tcp://localhost:5555')

	# 指定网络视频流地址和名称
	paths = []
	
	# 读取全部内容,并以列表方式返回
	t = open("streams.txt","r")
	lines = t.readlines()
	for line in lines:
		line = line.strip()
		paths.append(line)

	for path in paths:
		if path == "" or "://" not in path:
			paths.remove(path)

	rpiNames = ["Stream 1", "Stream 2", "Stream 3", "Stream 4", "Stream 5", "Stream 6", "Stream 7", "Stream 8", "Stream 9"]

	while True:
		# 从网络视频流读取帧并将其发送到 Video Capturer
		for (path, rpiName) in zip(paths, rpiNames):
			vs = VideoStream(path).start()
			frame = vs.read()
			try:
				sender.send_image(rpiName, frame)
			except AttributeError:
				pass

			continue
Ejemplo n.º 2
0
def main():
    # Parse arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('--port', help='port to use', default='5555', type=str)
    parser.add_argument('--host',
                        help='server IP or name',
                        default='localhost',
                        required=False)
    args = parser.parse_args()

    sender = imagezmq.ImageSender(connect_to='tcp://' + args.host + ':' +
                                  args.port)

    cap = cv2.VideoCapture(0)

    while True:  # send images as stream until Ctrl-C

        ret, frame = cap.read()

        if not ret:
            print("Unable to open camera")
            return

        image = cv2.flip(frame, 1)

        mst, img = sender.send_image2("image", image)

        c = img.shape[1] * 1
        r = img.shape[0] * 1

        img = cv2.resize(img, dsize=(c, r), interpolation=cv2.INTER_CUBIC)

        cv2.imshow('test', img)
        cv2.waitKey(1)
Ejemplo n.º 3
0
def main():
        # construct the argument parser and parse the arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-s", "--server-ip", required=True,
                    help="ip address of the server to which the client will connect")
    args = vars(ap.parse_args())

    # initialize the ImageSender object with the socket address of the
    # server
    sender = imagezmq.ImageSender(connect_to="tcp://{}:5555".format(
        args["server_ip"]))

    # get the host name, initialize the video stream, and allow the
    # camera sensor to warmup
    rpiName = socket.gethostname()
    # vs = VideoStream(usePiCamera=True, resolution=(
    #     240, 135), framerate=25).start()
    vs = VideoStream(usePiCamera=True, resolution=(
        240, 135), framerate=25).start()
    #vs = VideoStream(src=0).start()
    time.sleep(2.0)

    while True:
        # read the frame from the camera and send it to the server
        frame = vs.read()
        sender.send_image(rpiName, frame)
Ejemplo n.º 4
0
 def __init__(self, addr='tcp://127.0.0.1'):
     # self.sock = socket.socket()
     # self.sock.connect((addr, 9090))
     # self.sock_img = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     # self.sock_img.connect((addr, 9095))
     # context = zmq.Context()
     self.sender = imagezmq.ImageSender('tcp://192.168.0.70:5555')
     self.socket = self.sender.zmq_context.socket(zmq.REQ)
     # self.socket.connect(addr+"%s" % 5588)
     self.socket.connect("tcp://192.168.0.70:%s" % 5588)
Ejemplo n.º 5
0
def main():
    image1 = np.zeros((400, 400, 3), dtype='uint8')
    green = (0, 255, 0)
    cv2.rectangle(image1, (50, 50), (300, 300), green, 40)
    # A red square on a black background
    image2 = np.zeros((400, 400, 3), dtype='uint8')
    red = (0, 0, 255)
    cv2.rectangle(image2, (100, 100), (350, 350), red, 40)

    sender = imagezmq.ImageSender(connect_to="tcp://{}:{}".format(HOST, PORT))
    while True:  # press Ctrl-C to stop images sending program
        sender.send_image("TEST CLIENT", image1)
        print(image1.shape)
        sender.send_image("TEST CLIENT", image2)
Ejemplo n.º 6
0
    def __init__(self, g_pool, my_persistent_var=10.0, ):
        super(Yolov4_Detection_Plugin, self).__init__(g_pool)
        # order (0-1) determines if your plugin should run before other plugins or after
        self.order = .1

        # all markers that are detected in the most recent frame
        self.my_var = my_persistent_var
        # attribute for the UI
        self.window = None
        self.menu = None
        self.img = None
        self.color = {
            "blue": [172, 183, 114],  # color : #5AA333 colors are in BGR format
            "red": [12, 15, 145]  # color : #9C2118
            }
        # booleans used in switches
        self.activation_state = False  # determine if object detection is active or not
        self.detect_mode = "Yolo 608 (best precision)"  # determine the model to use
        self.new_window = False  # determine if we open a new window for object detection
        self.model_loaded = False  # determine if we have loaded a model
        self.sight_only = False
        self.export = False
        self.is_streaming = False

        # streaming attributes
        self.SERVER_IP = "127.0.0.1"
        self.SERVER_PORT = 12001 # if you get errors while streaming, try different port number
        self.HOST_NAME = "localhost"
        self.sender = imagezmq.ImageSender(connect_to="tcp://{}:{}".format(self.SERVER_IP, self.SERVER_PORT))

        # we need to create a darknet image then use it for every detection
        self.darknet_image = None
        self.network_image_size = 608
        self.fps = 29.97

        self.gaze_position_2d = []
        self.focus_object_index=-1
        self.data = None
        self.width = 1280
        self.height = 720
        self.codec = cv2.VideoWriter_fourcc(*'DIVX') # codec use to create .avi video file
        # stream definition is HD by default but you can change it
        self.out = None
Ejemplo n.º 7
0
    def startBotVisionClient(self, server_ip):
        import socket  # import needs to be here b/c same name as "from socket ..." on line 0
        print("Entered the startBotVisionClient thread")
        global vs

        # initialize the ImageSender object with the socket address of server
        sender = imagezmq.ImageSender(
            connect_to="tcp://{}:5555".format(server_ip))

        # get the host name, initialize the video stream, and allow the
        # camera sensor to warmup
        rpiName = socket.gethostname()
        vs = VideoStream(usePiCamera=True, resolution=(240, 144), framerate=25)
        vs.start()
        # vs = VideoStream(src=0).start()
        time.sleep(2.0)

        while True:
            # read the frame from the camera and send it to the server
            frame = vs.read()
            sender.send_image(rpiName, frame)
Ejemplo n.º 8
0
                default=240)

ap.add_argument("-u",
                "--url",
                type=str,
                help="height of image output",
                default="localhost")
ap.add_argument("-p",
                "--port",
                type=str,
                help="height of image output",
                default="5555")

args = vars(ap.parse_args())

sender = imagezmq.ImageSender(connect_to=f'tcp://{args["url"]}:{args["port"]}')
# sender = imagezmq.ImageSender()
i = 0

if args["source"].isnumeric():
    source = int(args["source"])
    start = True
else:
    source = args["source"]

    if os.path.isfile(source):
        start = True
    else:
        start = False

# vs = VideoStream(src=source, resolution=(args["width"], args["height"])).start()
Ejemplo n.º 9
0
    def recent_events(self, events):
        prev_time = time.time()  # used to show fps
        events["objects"] = [] # creates the topic "objects" that will be used to export recognition data

        if 'frame' in events:
            frame = events['frame']
        else:
            return
        # get current frame
        self.img = frame.img

        # get the gaze data from Pupil Core
        gaze = events.get("gaze", [])
        try:
        	self.gaze_position_2d = list(gaze[0].get('norm_pos')) # "norm_pos" returns the gaze position in the normalized plane of the image.
        	self.gaze_position_2d[0] = self.gaze_position_2d[0] * self.width
        	self.gaze_position_2d[1] = (1-self.gaze_position_2d[1]) * self.height
        except IndexError: # if you don't calibrate the Pupil Core, gaze will be empty and it raises an IndexError.
        	self.gaze_position_2d = []
        
        # print(self.gaze_position_2d)
        # the vector base of this plane is the same as Yolo's vector base except that the y axis is inverted
        # therefore we need to convert back the normalized position and then rescale it with our image dimensions
        
        if self.activation_state:
            if self.model_loaded:
                # resize frame to right size for prediction
                frame_resized = cv2.resize(self.img,
                                           (network_width(netMain),
                                            network_height(netMain)),
                                           interpolation=cv2.INTER_LINEAR)
                copy_image_from_bytes(self.darknet_image, frame_resized.tobytes())  # transform the current frame ina darknet compatible format
                detections = detect_image(netMain, metaMain, self.darknet_image, thresh=0.25)  # Yolo is used here to detect objects
                image = self.cvDrawBoxes(detections=detections, img=self.img)   # draw boxes around objects
                self.img = image  # display in Pupil Capture
                '''
                Export objects classes and box coordinates with their associated timestamps and coordinates in the topic "objects"
                We only export recognition data when the subject is focusing on an object. if no object is being watch, nothing happends.
                '''
                if self.focus_object_index > 0:
                    obj_entry = {
                        'topic': "objects",
                        'timestamp': self.g_pool.get_timestamp(),
                        'mode': self.detect_mode
                    }

                    obj_entry = self.format_json(obj_entry, detections=detections) # create JSON format string
                    events["objects"].append(obj_entry) # add the object data to "objects" topic
                    self.data = events["objects"] # getting back the data to use it, it allow to check if everything is fine

        # local .avi video exportation
        if self.export:
            if self.out is None:
                self.out = cv2.VideoWriter('C:/Users/' + USER + '/pupil_capture_settings/plugins/data/output.avi', self.codec, 24.0, (1280, 720))
            self.out.write(self.img)  # write local .avi video
            self.export_data_json(datum=self.data) # write local JSON file
        else:
            if self.out is not None:
                self.out.release()

        '''
        Stream the current frame at tcp://127.0.0.1:12001 but you can change ip and port in self.__init__().
        It sends JPG images but you can change the format if needed. 
        '''
        if self.is_streaming:
            if self.sender is None:
                self.sender = imagezmq.ImageSender(connect_to="tcp://{}:{}".format(self.SERVER_IP, self.SERVER_PORT), REQ_REP=False)
                print('Connect the ImageSender object to {}:{}'.format(self.SERVER_IP, self.SERVER_PORT))
                print('Host name is {}'.format(self.HOST_NAME))
            _, jpg_buffer = cv2.imencode(".jpg", self.img)
            self.sender.send_jpg(self.HOST_NAME, jpg_buffer)
        else:
            if self.sender is not None:
                self.sender.close() # close the stream when the switch is off
            self.sender = None

        # detection fps indicator
        total_time = time.time()-prev_time
        self.menu.elements.pop()
        self.fps = int((1 / total_time)*100)/100
        self.menu.append(ui.Info_Text(str(self.fps) + " fps"
        ))
        
        if self.new_window: # if you prefer to open the detection in a new window
            if self.img is not None:
                cv2.imshow('Demo', self.img)
                cv2.waitKey(3)
Ejemplo n.º 10
0
 def init(self):
     self.frames = []
     self.sender = imagezmq.ImageSender(connect_to='tcp://192.168.0.1:5555')
     self.stopped = False
Ejemplo n.º 11
0
from picamutil import PiJpegStream
import imagezmq.imagezmq as imagezmq
import cv2
import argparse

ap = argparse.ArgumentParser()
ap.add_argument("-H",
                "--hub",
                required=True,
                help="image hub ip address or host name")
args = vars(ap.parse_args())

hub_host = args["hub"]
hub_host_url = 'tcp://{}:5555'.format(hub_host)
print('hub host: {}'.format(hub_host_url))
sender = imagezmq.ImageSender(connect_to=hub_host_url)

rpi_name = socket.gethostname()  # send RPi hostname with each image
picam = PiJpegStream(resolution=(640, 480), framerate=16).start()
time.sleep(2.0)  # allow camera sensor to warm up

avg_time_to_send = None

while True:  # send images as stream until Ctrl-C
    s = time.time()
    image = picam.read()
    print('read {} bytes image in {} s'.format(len(image), time.time() - s))
    s = time.time()
    sender.send_jpg(rpi_name, image)
    time_to_send = time.time() - s
    avg_time_to_send = time_to_send if avg_time_to_send is None else (
Ejemplo n.º 12
0
 def connect(self):
     address = 'tcp://{}:5555'.format(self.server_ip)
     sender = imagezmq.ImageSender(connect_to=address)
     host_name = socket.gethostname()
     print('[INFO] Connecting to {}'.format(address))
     return host_name, sender
Ejemplo n.º 13
0
    help="Port server", default="5555")
ap.add_argument("-a", "--analitic", type=str, 
    help="Image processing", default="true")
ap.add_argument("-H", "--height", type=int, 
    help="Height image", default=480)
ap.add_argument("-W", "--width", type=int, 
    help="Width image", default=640)
ap.add_argument("-t", "--type", type=str, 
    help="Type of client", default="single")
args = vars(ap.parse_args())

time.sleep(3.0)
if args["url"] == "None":
    host = os.environ.get('HOST_SERVER')
    url = f'tcp://{host}:{args["port"]}'
    sender  = imagezmq.ImageSender(connect_to=url,REQ_REP = True)
else:
    sender  = imagezmq.ImageSender(connect_to=f'tcp://{args["url"]}:{args["port"]}', REQ_REP = True)

source  = args["source"]
id      = args["id"]
name    = args["name"]
type_cam    = args["type"]
calibrate = "OK"

process_opsi = ["rgb", "hsv", "grey"]
opsi_active = "rgb"

if source != "None":
    if source == "pi":
        vs = VideoStream(usePiCamera=True).start()
Ejemplo n.º 14
0
                help="resolution height")
ap.add_argument("-c",
                "--jpg-compression",
                required=False,
                default=90,
                help="jpg compression, lower = lower network latency")
args = vars(ap.parse_args())

# get the host name of the machine
rpi_name = socket.gethostname()

# initialize the ImageSender object with the socket address of the server
zmq_url = 'tcp://{}:{}'.format(args["server_ip"], args["server_port"])
print('[INFO] Connecting {} to imagezmq server on {}...'.format(
    rpi_name, zmq_url))
sender = imagezmq.ImageSender(connect_to=zmq_url)

# initialize the video stream, and allow the
# camera sensor to warmup
print('[INFO] Warming up camera sensor...')
res_dim = (int(args['res_width']), int(args['res_height']))
vs = VideoStream(usePiCamera=True, resolution=res_dim).start()
time.sleep(2.0)

# start streaming images
print('[INFO] Streaming video at resolution: {}...'.format(res_dim))
try:
    while True:  # send images as stream until Ctrl-C

        # read the frame from the camera
        frame = vs.read()
Ejemplo n.º 15
0
 def _start_image_sender(self, outgoing_addr, outgoing_socket='5555'):
     self.sender = imagezmq.ImageSender(
         connect_to="tcp://{}:".format(outgoing_addr) +
         str(outgoing_socket))
     print("Connected to outgoing stream at: " +
           "tcp://{}:".format(outgoing_addr) + str(outgoing_socket))
Ejemplo n.º 16
0
# run this program on each RPi to send a labelled image stream
import socket
import time
from imutils.video import VideoStream
import imagezmq.imagezmq as imagezmq
import cv2

sender = imagezmq.ImageSender(connect_to='tcp://192.168.10.183:5555')

rpi_name = socket.gethostname()  # send RPi hostname with each image
picam = VideoStream(usePiCamera=True, resolution=(640, 480),
                    framerate=16).start()
time.sleep(2.0)  # allow camera sensor to warm up

avg_time_to_send = None

while True:  # send images as stream until Ctrl-C
    s = time.time()
    image = picam.read()
    print('read {} byte  {} array image in {} s'.format(
        image.size * image.itemsize, image.shape,
        time.time() - s))
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    print('gray image {} byte'.format(gray_image.size * gray_image.itemsize))
    s = time.time()
    sender.send_image(rpi_name, image)
    time_to_send = time.time() - s
    avg_time_to_send = time_to_send if avg_time_to_send is None else (
        avg_time_to_send + time_to_send) / 2
    print('sent image in {} s'.format(avg_time_to_send))
    #time.sleep(5.0)
Ejemplo n.º 17
0
# import necessary packages
from imutils.video import VideoStream
from imagezmq import imagezmq
import argparse
import socket
import time

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-s", "--server-ip", required=True, help="ip address")
args = vars(ap.parse_args())

# initialise the ImageSender object with the socket address of the 
# server
sender = imagezmq.ImageSender(connect_to="tcp://{}:5555".format(args["server_ip"]))

# get the host name, initialise the video, and allow the camera sensor to warmup
rpiName = socket.gethostname() # hostname is stored as rpiName
vs = VideoStream(usePiCamera=True).start() #  grabs frames  from camera, TODO: may need to reduce resolution
time.sleep(2.0)

while True:
	# read the frame from the camera and send it to the server
	frame = vs.read()
	sender.send_image(rpiName, frame)
export VIDEO_SRC=0
python zmq_producer.py 

export VIDEO_SRC=http://192.168.2.34:8080/video?.mjpeg


"""
import os
import time
import cv2

from imagezmq import imagezmq
from imutils.video import WebcamVideoStream

# initialize the ImageSender object with the socket address of the server
sender = imagezmq.ImageSender(connect_to="tcp://{}:{}".format(
    os.environ["SERVER_IP"], os.environ['SERVER_PORT']))
print('Connect the ImageSender object to {}:{}'.format(
    os.environ["SERVER_IP"], os.environ['SERVER_PORT']))
print('Host name is {}'.format(os.environ["HOST_NAME"]))
print('Video source is {}'.format(os.environ["VIDEO_SRC"]))

# if video source is digit or string
video = os.environ['VIDEO_SRC']
video = int(video) if str.isdigit(video) else video

# initialize the video stream
video_stream = WebcamVideoStream(src=video)

# 4:3=1280x960, 640x480 / 16:9=1280x720, 640x360
video_stream.stream.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
video_stream.stream.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)