Skip to content

Instantly share code, notes, and snippets.

@iandol
Created February 19, 2022 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iandol/ebf722f75251bb9f5bc57ba967fd12ba to your computer and use it in GitHub Desktop.
Save iandol/ebf722f75251bb9f5bc57ba967fd12ba to your computer and use it in GitHub Desktop.
Trying to get this demo code working: https://abhitronix.github.io/vidgear/latest/help/netgear_ex/#using-netgear-with-webgear — I get a client error about ZMQ. Running the basic ZMQ REP/REQ code works over the same network address+port
❯ python client.py
12:08:59 :: Helper :: INFO :: Running VidGear Version: 0.2.5
12:08:59 :: Helper_Async :: DEBUG :: Found valid WebGear data-files successfully.
12:08:59 :: WebGear :: DEBUG :: `/Users/ian/.vidgear/webgear` is the default location for saving WebGear data-files.
12:08:59 :: WebGear :: WARNING :: Given source is of NoneType. Therefore, JPEG Frame-Compression is disabled!
12:08:59 :: WebGear :: DEBUG :: Initiating Video Streaming.
12:08:59 :: WebGear :: DEBUG :: Running Starlette application.
INFO: Started server process [17175]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://localhost:8000 (Press CTRL+C to quit)
INFO: ::1:54225 - "GET / HTTP/1.1" 200 OK
INFO: ::1:54225 - "GET /css/custom.css HTTP/1.1" 304 Not Modified
INFO: ::1:54225 - "GET /js/custom.js HTTP/1.1" 304 Not Modified
INFO: ::1:54225 - "GET /video HTTP/1.1" 200 OK
12:09:06 :: NetGear :: ERROR :: Can't assign requested address
Traceback (most recent call last):
File "/Users/ian/.venv/vidgear/lib/python3.10/site-packages/vidgear/gears/netgear.py", line 620, in __init__
self.__msg_socket.bind(
File "/Users/ian/.venv/vidgear/lib/python3.10/site-packages/zmq/sugar/socket.py", line 214, in bind
super().bind(addr)
File "zmq/backend/cython/socket.pyx", line 540, in zmq.backend.cython.socket.Socket.bind
File "zmq/backend/cython/checkrc.pxd", line 28, in zmq.backend.cython.checkrc._check_rc
zmq.error.ZMQError: Can't assign requested address
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/Users/ian/.venv/vidgear/lib/python3.10/site-packages/uvicorn/protocols/http/h11_impl.py", line 366, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/Users/ian/.venv/vidgear/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
return await self.app(scope, receive, send)
File "/Users/ian/.venv/vidgear/lib/python3.10/site-packages/starlette/applications.py", line 119, in __call__
await self.middleware_stack(scope, receive, send)
File "/Users/ian/.venv/vidgear/lib/python3.10/site-packages/starlette/middleware/errors.py", line 181, in __call__
raise exc
File "/Users/ian/.venv/vidgear/lib/python3.10/site-packages/starlette/middleware/errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "/Users/ian/.venv/vidgear/lib/python3.10/site-packages/starlette/exceptions.py", line 87, in __call__
raise exc
File "/Users/ian/.venv/vidgear/lib/python3.10/site-packages/starlette/exceptions.py", line 76, in __call__
await self.app(scope, receive, sender)
File "/Users/ian/.venv/vidgear/lib/python3.10/site-packages/starlette/routing.py", line 659, in __call__
await route.handle(scope, receive, send)
File "/Users/ian/.venv/vidgear/lib/python3.10/site-packages/starlette/routing.py", line 259, in handle
await self.app(scope, receive, send)
File "/Users/ian/.venv/vidgear/lib/python3.10/site-packages/starlette/routing.py", line 64, in app
await response(scope, receive, send)
File "/Users/ian/.venv/vidgear/lib/python3.10/site-packages/starlette/responses.py", line 241, in __call__
async with anyio.create_task_group() as task_group:
File "/Users/ian/.venv/vidgear/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 574, in __aexit__
raise exceptions[0]
File "/Users/ian/.venv/vidgear/lib/python3.10/site-packages/starlette/responses.py", line 244, in wrap
await func()
File "/Users/ian/.venv/vidgear/lib/python3.10/site-packages/starlette/responses.py", line 233, in stream_response
async for chunk in self.body_iterator:
File "/Users/ian/Code/gears/client.py", line 24, in my_frame_producer
client = NetGear(
File "/Users/ian/.venv/vidgear/lib/python3.10/site-packages/vidgear/gears/netgear.py", line 669, in __init__
raise RuntimeError(
RuntimeError: [NetGear:ERROR] :: Receive Mode failed to bind address: tcp://192.168.1.44:5454 and pattern: 1! Kindly recheck all parameters.
# import necessary libs
import uvicorn, asyncio, cv2
from vidgear.gears import NetGear
from vidgear.gears.asyncio import WebGear
from vidgear.gears.asyncio.helper import reducer
# initialize WebGear app without any source
web = WebGear(logging=True)
# activate jpeg encoding and specify other related parameters
options = {
"jpeg_compression": True,
"jpeg_compression_quality": 90,
"jpeg_compression_fastdct": True,
"jpeg_compression_fastupsample": True,
}
# create your own custom frame producer
async def my_frame_producer():
'''
frame producer
'''
client = NetGear(
receive_mode=True,
address="192.168.1.44",
port="5454",
protocol="tcp",
pattern=1,
logging=True,
**options,
)
# loop over frames
while True:
# receive frames from network
frame = client.recv()
# if NoneType
if frame is None:
return
# reducer frames size if you want more performance otherwise comment this line
frame = await reducer(
frame, percentage=30, interpolation=cv2.INTER_AREA
) # reduce frame by 30%
# handle JPEG encoding
encodedImage = cv2.imencode(".jpg", frame)[1].tobytes()
# yield frame in byte format
yield (b"--frame\r\nContent-Type:image/jpeg\r\n\r\n" + encodedImage + b"\r\n")
await asyncio.sleep(0)
# close stream
client.close()
# add your custom frame producer to config with adequate IP address
web.config["generator"] = my_frame_producer
# run this app on Uvicorn server at address http://localhost:8000/
uvicorn.run(web(), host="localhost", port=8000)
# close app safely
web.shutdown()
❯ python server.py
12:08:45 :: Helper :: INFO :: Running VidGear Version: 0.2.5
12:08:46 :: NetGear :: DEBUG :: Reliable transmission is enabled for this pattern with max-retries: 3 and timeout: 1024.0 secs.
12:08:46 :: NetGear :: DEBUG :: Successfully connected to address: tcp://192.168.1.44:5454 with pattern: 1.
12:08:46 :: NetGear :: DEBUG :: JPEG Frame-Compression is activated for this connection with Colorspace:`BGR`, Quality:`90`%, Fastdct:`enabled`, and Fastupsample:`enabled`.
12:08:46 :: NetGear :: DEBUG :: Unique System ID is 6T4VWWHU.
12:08:46 :: NetGear :: DEBUG :: Send Mode is successfully activated and ready to send data.
# import required libraries
from vidgear.gears import VideoGear
from vidgear.gears import NetGear
import cv2
# activate jpeg encoding and specify other related parameters
options = {
"request_timeout": 1024,
"jpeg_compression": True,
"jpeg_compression_quality": 90,
"jpeg_compression_fastdct": True,
"jpeg_compression_fastupsample": True,
}
stream = VideoGear(source=0).start()
server = NetGear(
address="192.168.1.44",
port="5454",
protocol="tcp",
pattern=1,
logging=True,
**options
)
while True:
try:
# read frames from stream
frame = stream.read()
# check for frame if None-type
if frame is None:
break
# send frame to server
server.send(frame)
except KeyboardInterrupt:
break
# safely close video stream
stream.stop()
# safely close server
server.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment