import base64 from io import BytesIO import requests import grpc from . import BeiHangGrpc_pb2_grpc from . import BeiHangGrpc_pb2 # from apps.utils.tools import convert_to_base64 def helmet(ip: str, pic_url: str): """安全帽事件 Args: ip (_type_): _description_ pic_url (_type_): _description_ """ channel = grpc.insecure_channel(ip) # 监听频道 stub = BeiHangGrpc_pb2_grpc.Helmet_RegStub(channel) # 客户端使用Stub类发送请求,参数为频道,为了绑定链接 image_id = 3 # 本地图像输入 # image_path = "./onnx/image/single3.jpg" # with open(image_path, 'rb') as f: # image_base64 = str(base64.b64encode(f.read()), encoding='utf-8') # request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id,imgsbase64=image_base64) base64img = str(base64.b64encode(BytesIO(requests.get(url=path).content).read()), 'utf-8') request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id, imgsbase64=base64img) # request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id, imgUrl=pic_url) response = stub.sendHelmet_Info(request) if response.helmetinfoList: for i in response.helmetinfoList: if i.head_helmet == 0: # 未戴安全帽事件成立 return True, response return False, response if __name__ == "__main__": path = 'http://10.99.5.79:20309/media/2022/08/25/dsf_e9de9deb-213e-11ed-884a-e4246c7d1635_11584620_11593689.jpg' base64img = str(base64.b64encode(BytesIO(requests.get(url=path).content).read()), 'utf-8') with open('face_test.txt', 'w') as f: f.write(base64img) helmet( ip='122.224.229.24:2000', pic_url='http://1.203.161.103:2800/media/2022/07/06/dsf_bf6446cc-f9cd-11ec-bd2d-e4246c7d1635_34597240_34614457.jpg')