算法全部采用url方式
This commit is contained in:
parent
4111a5ef03
commit
9a7df21526
|
@ -5,6 +5,8 @@ from io import BytesIO
|
||||||
import os
|
import os
|
||||||
import requests
|
import requests
|
||||||
import grpc
|
import grpc
|
||||||
|
|
||||||
|
from apps.ecm.service import save_dahua_pic
|
||||||
from . import BeiHangGrpc_pb2_grpc
|
from . import BeiHangGrpc_pb2_grpc
|
||||||
from . import BeiHangGrpc_pb2
|
from . import BeiHangGrpc_pb2
|
||||||
import uuid
|
import uuid
|
||||||
|
@ -12,29 +14,29 @@ from django.conf import settings
|
||||||
import cv2
|
import cv2
|
||||||
requests.packages.urllib3.disable_warnings()
|
requests.packages.urllib3.disable_warnings()
|
||||||
|
|
||||||
|
# 本地图像输入
|
||||||
|
# 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=pic_url, verify=False).content).read()), 'utf-8')
|
||||||
|
# with open('face_test.txt', 'w') as f:
|
||||||
|
# f.write(base64img)
|
||||||
|
# request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id, imgUrl=pic_url)
|
||||||
|
|
||||||
|
|
||||||
def helmet(ip: str, pic_url: str):
|
def helmet(ip: str, pic_url: str):
|
||||||
"""安全帽事件
|
"""安全帽事件(抠图)
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
ip (_type_): _description_
|
ip (_type_): _description_
|
||||||
pic_url (_type_): _description_
|
pic_url (_type_): _description_
|
||||||
"""
|
"""
|
||||||
# print(pic_url)
|
|
||||||
channel = grpc.insecure_channel(ip+':2000') # 监听频道
|
channel = grpc.insecure_channel(ip+':2000') # 监听频道
|
||||||
stub = BeiHangGrpc_pb2_grpc.Helmet_RegStub(channel) # 客户端使用Stub类发送请求,参数为频道,为了绑定链接
|
stub = BeiHangGrpc_pb2_grpc.Helmet_RegStub(channel) # 客户端使用Stub类发送请求,参数为频道,为了绑定链接
|
||||||
image_id = 3
|
image_id = 3
|
||||||
|
img_path = save_dahua_pic(pic_url)
|
||||||
# 本地图像输入
|
request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id, imgUrl=settings.BASE_URL_OUT + img_path)
|
||||||
# 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=pic_url, verify=False).content).read()), 'utf-8')
|
|
||||||
# with open('face_test.txt', 'w') as f:
|
|
||||||
# f.write(base64img)
|
|
||||||
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)
|
response = stub.sendHelmet_Info(request)
|
||||||
if response.helmetinfoList:
|
if response.helmetinfoList:
|
||||||
for i in response.helmetinfoList:
|
for i in response.helmetinfoList:
|
||||||
|
@ -43,23 +45,40 @@ def helmet(ip: str, pic_url: str):
|
||||||
return False, response
|
return False, response
|
||||||
|
|
||||||
|
|
||||||
|
def helme2(ip: str, pic_url: str):
|
||||||
|
"""安全帽事件(全图)
|
||||||
|
|
||||||
|
Args:
|
||||||
|
ip (_type_): _description_
|
||||||
|
pic_url (_type_): _description_
|
||||||
|
"""
|
||||||
|
channel = grpc.insecure_channel(ip+':2002') # 监听频道
|
||||||
|
stub = BeiHangGrpc_pb2_grpc.Helmet_RegStub(channel) # 客户端使用Stub类发送请求,参数为频道,为了绑定链接
|
||||||
|
image_id = 3
|
||||||
|
img_path = save_dahua_pic(pic_url)
|
||||||
|
request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id, imgUrl=settings.BASE_URL_OUT + img_path)
|
||||||
|
response = stub.sendHelmet_Info(request)
|
||||||
|
os.remove(settings.BASE_DIR + img_path)
|
||||||
|
if response.helmetinfoList:
|
||||||
|
for i in response.helmetinfoList:
|
||||||
|
if i.head_helmet == 1: # 未戴安全帽事件成立
|
||||||
|
return True, response
|
||||||
|
return False, response
|
||||||
|
|
||||||
|
|
||||||
def fire1(ip: str, pic_url: str):
|
def fire1(ip: str, pic_url: str):
|
||||||
# 未配置灭火器事件
|
# 未配置灭火器事件
|
||||||
channel = grpc.insecure_channel(ip+':2001')
|
channel = grpc.insecure_channel(ip+':2001')
|
||||||
stub = BeiHangGrpc_pb2_grpc.Fire_RegStub(channel)
|
stub = BeiHangGrpc_pb2_grpc.Fire_RegStub(channel)
|
||||||
image_id = 3
|
image_id = 3
|
||||||
img_name = '{}.png'.format(uuid.uuid4())
|
img_path = save_dahua_pic(pic_url)
|
||||||
img_path = settings.BASE_DIR + '/temp/' + img_name
|
# img = cv2.imread(img_path)
|
||||||
with open(img_path, "wb") as f:
|
# resized_img = cv2.resize(img, None, fx=0.25, fy=0.25)
|
||||||
f.write(requests.get(url=pic_url, verify=False).content)
|
# resized_img = cv2.imencode('.png', resized_img)[1]
|
||||||
img = cv2.imread(img_path)
|
# image_base64 = str(base64.b64encode(resized_img))[2:-1]
|
||||||
resized_img = cv2.resize(img, None, fx=0.25, fy=0.25)
|
request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id, imgUrl=settings.BASE_URL_OUT + img_path)
|
||||||
resized_img = cv2.imencode('.png', resized_img)[1]
|
|
||||||
image_base64 = str(base64.b64encode(resized_img))[2:-1]
|
|
||||||
|
|
||||||
request = BeiHangGrpc_pb2.JinYu_Request(zzid=image_id, imgsbase64=image_base64)
|
|
||||||
response = stub.sendFire_Info(request)
|
response = stub.sendFire_Info(request)
|
||||||
os.remove(img_path) # 删除临时图片
|
os.remove(settings.BASE_DIR + img_path) # 删除临时图片
|
||||||
if response.FireinfoList:
|
if response.FireinfoList:
|
||||||
for i in response.FireinfoList:
|
for i in response.FireinfoList:
|
||||||
if i.fire == 0: # 配置了灭火器
|
if i.fire == 0: # 配置了灭火器
|
||||||
|
|
|
@ -7,12 +7,13 @@ myLogger = logging.getLogger('log')
|
||||||
|
|
||||||
algo_dict = {
|
algo_dict = {
|
||||||
"helmet": "apps.ai.client.helmet",
|
"helmet": "apps.ai.client.helmet",
|
||||||
|
"helmet2": "apps.ai.client.helmet2",
|
||||||
"fire1": "apps.ai.client.fire1"
|
"fire1": "apps.ai.client.fire1"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def ai_analyse(codes: list, global_img: str, face_img: str = None):
|
def ai_analyse(codes: list, global_img: str, face_img: str = None):
|
||||||
"""多线程进行算法分析图片
|
"""算法分析图片
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
codes: 算法列表
|
codes: 算法列表
|
||||||
|
@ -27,13 +28,10 @@ def ai_analyse(codes: list, global_img: str, face_img: str = None):
|
||||||
f = getattr(m, func)
|
f = getattr(m, func)
|
||||||
try:
|
try:
|
||||||
is_happend, res = False, None
|
is_happend, res = False, None
|
||||||
if i == 'helmet': # 如果是安全帽识别
|
if i == 'helmet' and face_img: # 如果是安全帽抠图识别
|
||||||
if face_img:
|
|
||||||
is_happend, res = f(ip=settings.AI_IP, pic_url=face_img)
|
is_happend, res = f(ip=settings.AI_IP, pic_url=face_img)
|
||||||
else:
|
else:
|
||||||
is_happend, res = f(ip=settings.AI_IP, pic_url=global_img)
|
is_happend, res = f(ip=settings.AI_IP, pic_url=global_img)
|
||||||
else:
|
|
||||||
is_happend, res = f(ip=settings.AI_IP, pic_url=global_img)
|
|
||||||
if is_happend:
|
if is_happend:
|
||||||
results.update({i: res})
|
results.update({i: res})
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|
|
@ -86,7 +86,7 @@ def opl_task(vc_codes: list, opl_id: str):
|
||||||
algo_codes = list(EventCate.objects.filter(opl_cates=opl_cate).values_list('code', flat=True))
|
algo_codes = list(EventCate.objects.filter(opl_cates=opl_cate).values_list('code', flat=True))
|
||||||
vchannels = TDevice.objects.filter(code__in=vc_codes)
|
vchannels = TDevice.objects.filter(code__in=vc_codes)
|
||||||
opl = Opl.objects.get(id=opl_id)
|
opl = Opl.objects.get(id=opl_id)
|
||||||
while time.time()-start_time < 28800: # 一次任务不手动关闭最多持续8小时
|
while time.time()-start_time < 14400: # 一次任务不手动关闭最多持续8小时
|
||||||
for i in vchannels:
|
for i in vchannels:
|
||||||
Thread(target=snap_and_analyse, args=(i, algo_codes, opl)).start()
|
Thread(target=snap_and_analyse, args=(i, algo_codes, opl)).start()
|
||||||
time.sleep(10)
|
time.sleep(10)
|
||||||
|
|
Loading…
Reference in New Issue