# 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()