|
|
@@ -6,7 +6,8 @@ import json
|
|
|
import time
|
|
|
import os
|
|
|
import requests
|
|
|
-import random
|
|
|
+import datetime
|
|
|
+import uuid
|
|
|
import ast
|
|
|
from django.conf import settings
|
|
|
|
|
|
@@ -15,6 +16,8 @@ from smartfarming.models.sim_card import MongoMsg_Conf
|
|
|
from smartfarming.models.device import MongoDevice
|
|
|
from smartfarming.models.worm_forecast import MongoCBDphoto, MongoCBDphoto_B, MongoCBDphoto_C
|
|
|
from kedong.decoration import kedong_deco, PortError
|
|
|
+from .common import *
|
|
|
+from django.db.models import Q
|
|
|
config_dict = settings.CONFIG
|
|
|
|
|
|
|
|
|
@@ -240,12 +243,6 @@ def device_photo_list(request):
|
|
|
return {"disable":device.disable,"data":data,"num":num}
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-from .common import *
|
|
|
-from django.db.models import Q
|
|
|
-
|
|
|
-
|
|
|
@kedong_deco(login_required=True)
|
|
|
def equip_photo_del(request):
|
|
|
"""
|
|
|
@@ -322,343 +319,110 @@ def cbd_msg_conf(request):
|
|
|
|
|
|
|
|
|
|
|
|
-def disable_cbd_discern(request):
|
|
|
- """
|
|
|
- 禁用测报灯害虫识别功能
|
|
|
-
|
|
|
- device_id 必传(str) 设备号
|
|
|
- ret 必传(str) ret=disable时禁用识别功能, ret=enable,时启用识别统计功能, ret =count 计数功能
|
|
|
-
|
|
|
- """
|
|
|
- device_id = request.POST.get("device_id")
|
|
|
- ret = request.POST.get("ret")
|
|
|
-
|
|
|
- try:
|
|
|
- device_list = Device.objects.get(device_id=device_id)
|
|
|
- except:
|
|
|
- raise PortError("device_id", "找不到该设备")
|
|
|
- if ret == "disable":
|
|
|
- device_list.disable = 0
|
|
|
- device_list.save()
|
|
|
- elif ret == "enable":
|
|
|
- device_list.disable = 1
|
|
|
- device_list.save()
|
|
|
- elif ret == "count":
|
|
|
- device_list.disable = 2
|
|
|
- device_list.save()
|
|
|
- else:
|
|
|
- raise PortError("device_id", "参数超出范围")
|
|
|
- return True
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-def cbd_pest_type_ranking(request):
|
|
|
- """
|
|
|
- 筛选最近识别害虫列表接口
|
|
|
- 参数:
|
|
|
- start_time 非必传 开始时间 时间戳
|
|
|
- end_time 非必传 结束时间 时间戳、
|
|
|
- page 非必传 页数
|
|
|
- page_size 非必传 一页默认10条数据
|
|
|
- ret 必传 为num害虫排名, 为type列表详情数据,
|
|
|
- identify_model 必传 识别模型A,识别模型B
|
|
|
-
|
|
|
- type时返回值:
|
|
|
- "data": {
|
|
|
- "num": 2, 条数
|
|
|
- "cbd_photo_data": [
|
|
|
- {
|
|
|
- "device_id": "867814040006799", 设备号
|
|
|
- "pest_num": 18, 害虫种类数据
|
|
|
- "pest_name": [
|
|
|
- {
|
|
|
- "pest_name": "水龟虫", 害虫种类
|
|
|
- "num": 518 害虫总数
|
|
|
- },
|
|
|
- {
|
|
|
- "pest_name": "蝼蛄",
|
|
|
- "num": 128
|
|
|
- },
|
|
|
- num 时返回值:
|
|
|
- "data": [
|
|
|
- {
|
|
|
- "水龟虫": 539 害虫名称 害虫数量
|
|
|
- },
|
|
|
- {
|
|
|
- "蝼蛄": 153
|
|
|
- },
|
|
|
- {
|
|
|
- "铜绿丽金龟": 62
|
|
|
- },
|
|
|
- {
|
|
|
- "负子蝽": 42
|
|
|
- },
|
|
|
- {
|
|
|
- "暗黑鳃金龟": 21
|
|
|
- },
|
|
|
- {
|
|
|
- "夜蛾": 15
|
|
|
- }
|
|
|
- ],
|
|
|
- """
|
|
|
- page = int(request.POST.get("page",1))
|
|
|
- page_size = int(request.POST.get("page_size",10))
|
|
|
- start_time = request.POST.get("start_time")
|
|
|
- end_time = request.POST.get("end_time")
|
|
|
+@kedong_deco(login_required=True)
|
|
|
+def equip_photo_discern(request):
|
|
|
+ # 测报灯、吸虫塔、色诱测报害虫重新图片识别\详情\手动标注接口
|
|
|
+ img_id = request.POST.get("img_id")
|
|
|
ret = request.POST.get("ret")
|
|
|
- recive_pest_name = request.POST.get("pest_name")
|
|
|
- identify_model = request.POST.get("identify_model","A")
|
|
|
- if identify_model=="A":
|
|
|
- models = CBDphoto
|
|
|
- elif identify_model=="B":
|
|
|
- models = CBDphoto_B
|
|
|
- myuser = request.myuser
|
|
|
- pest_num_data = []
|
|
|
- date = []
|
|
|
- devices = Device.devices(uid=myuser.uid, myuser=myuser)
|
|
|
- print("user_type",myuser.user_type)
|
|
|
- if myuser.user_type != 4 or myuser.user_type != 1:
|
|
|
- devices = devices.filter(disable=1)
|
|
|
- for i in devices:
|
|
|
- if not myuser.user_type == 1 and i.device_expire == "1":
|
|
|
- cbd_data = models.objects.filter(~Q(indentify_photo=None),device_id=i.id,photo_status=1,addtime__lte=int(i.device_expire_time)).values()
|
|
|
- if end_time and int(end_time) > int(i.device_expire_time):
|
|
|
- end_time = i.device_expire_time
|
|
|
- cbd_data = cbd_data.filter(addtime__range=(int(start_time),int(end_time)))
|
|
|
- else:
|
|
|
- cbd_data = models.objects.filter(~Q(indentify_photo=None),device_id=i.id,photo_status=1).values()
|
|
|
- if start_time:
|
|
|
- cbd_data = cbd_data.filter(addtime__range=(int(start_time),int(end_time)))
|
|
|
- for x in cbd_data:
|
|
|
- if x["indentify_result"]:
|
|
|
- xx_result = x["indentify_result"].split("#")
|
|
|
- print(xx_result)
|
|
|
- for yy in xx_result:
|
|
|
- yy_result = yy.split(",")
|
|
|
+ if ret == "again":
|
|
|
+ # 手动标注
|
|
|
+ # 获取该图片对象
|
|
|
+ photo_obj = MongoCBDphoto.objects.filter(id=img_id)
|
|
|
+ photo_obj = photo_obj.first() if photo_obj else None
|
|
|
+ date = []
|
|
|
+ if photo_obj:
|
|
|
+ # 当图片对象存在
|
|
|
+ addr = photo_obj.addr
|
|
|
+ if addr:
|
|
|
+ # 该图片对应设备的信息
|
|
|
+ device = MongoDevice.objects.filter(id=img_id)
|
|
|
+ device = device.first() if device else None
|
|
|
+ # 获取图片本地路径
|
|
|
+ img_content_url = f'{config_dict.get("image_url").get("image")}/media'
|
|
|
+ media = f'{config_dict.get("media")}'
|
|
|
+ if addr.startswith("http"):
|
|
|
+ addr = addr.replace(img_content_url,media)
|
|
|
+ else:
|
|
|
+ addr = addr.replace("/media",media)
|
|
|
+ print(addr, "--------")
|
|
|
+ pass
|
|
|
+ # 重新调用识别接口
|
|
|
+ identify_url = config_dict.get("image_url").get("identify_url")
|
|
|
+ if device.lng and device.lat:
|
|
|
+ request_data = {
|
|
|
+ "longitude": device.lng,
|
|
|
+ "latitude": device.lat
|
|
|
+ }
|
|
|
+ else:
|
|
|
+ request_data = {
|
|
|
+ "longitude": "126.40548",
|
|
|
+ "latitude": "47.82857"
|
|
|
+ }
|
|
|
+ if os.path.exists(addr):
|
|
|
+ imageFile = {
|
|
|
+ "imageFile": ('pic.jpg', open(addr, "rb"), 'image/jpeg')
|
|
|
+ }
|
|
|
+ res = requests.post(url=identify_url, data=request_data, files=imageFile, timeout=(10, 20))
|
|
|
+ image_result = json.loads(res.text)
|
|
|
try:
|
|
|
- date.append({
|
|
|
- "device_id": i.device_id,
|
|
|
- "device_name": i.device_name,
|
|
|
- "device_add": i.province+i.city+i.district,
|
|
|
- "pest_num" : int(yy_result[1]),
|
|
|
- "pest_name" : insect_dict[yy_result[0]]})
|
|
|
- except:
|
|
|
- continue
|
|
|
- data = {}
|
|
|
- pest = {}
|
|
|
- if ret == "type":
|
|
|
- for item in date:
|
|
|
- device_id, pest_name,pest_num,device_name,device_add= item['device_id'], item['pest_name'],item['pest_num'],item['device_name'],item['device_add']
|
|
|
- key = '{},{},{}'.format(device_id,device_name,device_add)
|
|
|
- if key in data:
|
|
|
- data[key] += [pest_name]
|
|
|
- else:
|
|
|
- data[key] = [pest_name]
|
|
|
- keys = '{},{}'.format(device_id,pest_name)
|
|
|
- if keys in pest:
|
|
|
- pest[keys] += pest_num
|
|
|
-
|
|
|
- else:
|
|
|
- pest[keys] = pest_num
|
|
|
- sort_data = sorted(data.items(), key=lambda d:len(d[1]), reverse = True )
|
|
|
- for y in sort_data:
|
|
|
- device_id,device_name, device_addr = y[0].split(',')
|
|
|
- pest_data = []
|
|
|
- for x in y[1]:
|
|
|
- x = {"pest_name":x,"num":pest[device_id +","+x]}
|
|
|
- if x not in pest_data:
|
|
|
- pest_data.append(x)
|
|
|
- sorted_x = sorted(pest_data, key=operator.itemgetter('num'),reverse = True)
|
|
|
- if recive_pest_name:
|
|
|
- for pt_name in sorted_x:
|
|
|
- if recive_pest_name in pt_name["pest_name"]:
|
|
|
- pest_num_data.append({
|
|
|
- "device_id" : device_id,
|
|
|
- "pest_num": len(pest_data),
|
|
|
- "pest_name": sorted_x,
|
|
|
- "device_name" : device_name,
|
|
|
- "device_addr": device_addr
|
|
|
- })
|
|
|
- else:
|
|
|
- pass
|
|
|
- else:
|
|
|
- pest_num_data.append({
|
|
|
- "device_id" : device_id,
|
|
|
- "pest_num": len(pest_data),
|
|
|
- "pest_name": sorted_x,
|
|
|
- "device_name" : device_name,
|
|
|
- "device_addr": device_addr
|
|
|
- })
|
|
|
- num = len(pest_num_data)
|
|
|
- cbd_photo_data = pest_num_data[(page_size*(page-1)):(page*page_size)]
|
|
|
- cbd_pset = {"num":num,"cbd_photo_data":cbd_photo_data}
|
|
|
- return cbd_pset
|
|
|
- elif ret == "num":
|
|
|
- for item in date:
|
|
|
- pest_num,pest_name = item['pest_num'],item["pest_name"]
|
|
|
- key = '{}'.format(pest_name)
|
|
|
- if key in data:
|
|
|
- data[key] += pest_num
|
|
|
- else:
|
|
|
- data[key] = pest_num
|
|
|
- sort_data = sorted(data.items(), key=lambda d:d[1], reverse = True )[:6]
|
|
|
- for y in sort_data:
|
|
|
- pest_num_data.append({
|
|
|
- y[0] : y[1]
|
|
|
- })
|
|
|
- return pest_num_data
|
|
|
+ if image_result.get("returnResult").get("returnStatus") != "0":
|
|
|
+ returnString = image_result["returnResult"]["returnString"]
|
|
|
+ returnImageUrl = image_result["returnResult"]["returnImageUrl"]
|
|
|
+ returnCode = image_result["returnResult"].get("returnCode","")
|
|
|
+ remote_content = requests.get(returnImageUrl).content
|
|
|
+ indentify_photo = photo_obj.indentify_photo
|
|
|
+ # 如果第一次识别图片存在
|
|
|
+ if photo_obj:
|
|
|
+ if indentify_photo.startswith("http"):
|
|
|
+ indentify_photo = indentify_photo.replace(img_content_url,media)
|
|
|
+ else:
|
|
|
+ indentify_photo = indentify_photo.replace("/media",media)
|
|
|
+ else:
|
|
|
+ # 如果第一次识别图片不存在
|
|
|
+ inden_path = os.path.join(media, f"result/cbd/{device.device_id}")
|
|
|
+ os.makedirs(inden_path) if not os.path.exists(inden_path) else None
|
|
|
+ stamp = int(datetime.now().timestamp())
|
|
|
+ unique_id = uuid.uuid4()
|
|
|
+ combined_id = str(stamp) + "-" + str(unique_id)
|
|
|
+ indentify_photo = os.path.join(inden_path, f"{combined_id}.jpg")
|
|
|
+ # 把原本地图片覆盖
|
|
|
+ with open(indentify_photo, "wb") as f:
|
|
|
+ f.write(remote_content)
|
|
|
+ # 更新结果
|
|
|
+ cbd_img = indentify_photo.replace(media, img_content_url)
|
|
|
+ photo_obj.indentify_photo = cbd_img
|
|
|
+ photo_obj.indentify_result = returnString
|
|
|
+ photo_obj.label = returnCode
|
|
|
+ photo_obj.save()
|
|
|
+ pest_list = returnString.split("#")
|
|
|
+ for i in pest_list:
|
|
|
+ if i:
|
|
|
+ res = i.split(",")
|
|
|
+ nums = res[1] #数量
|
|
|
+ number = res[0] #编号
|
|
|
+ try:
|
|
|
+ result_list = insect_dict[number]
|
|
|
+ except:
|
|
|
+ result_list = attract_discern[number]
|
|
|
+ date.append({"nums":nums,"result_list":result_list,"number":number})
|
|
|
+ dat = {"image":cbd_img,"result":date, "label": returnCode}
|
|
|
+ print(dat)
|
|
|
+ return dat
|
|
|
+ else:
|
|
|
+ return {"code": 2, "msg": "无识别结果"}
|
|
|
+ except Exception as e:
|
|
|
+ return {"code": 2, "msg": "无识别结果"}
|
|
|
else:
|
|
|
- raise PortError("ret", "参数超出范围")
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-def cbd_pest_line_chart(request):
|
|
|
- """
|
|
|
- 筛选总设备害虫折线图
|
|
|
- 参数:
|
|
|
- start_time 非必传(string 时间戳) 开始时间 (用于时间搜索)
|
|
|
- end_time 非必传(string 时间戳) 结束时间 (用于时间搜索)
|
|
|
- pest_name 非必传(str) 害虫名称
|
|
|
- ret 必传 等于day_month 24小时数据、近一个月数据, 等于:half_year 半年、一年数据
|
|
|
- identify_model 必传 识别模型A,识别模型B
|
|
|
-
|
|
|
- 等于day_month返回值:
|
|
|
- "data": [
|
|
|
- {
|
|
|
- "addtime": "2021-03-09 11:28:08", 时间
|
|
|
- "pest_name": "夜蛾", 害虫名
|
|
|
- "pest_num": 5 数量
|
|
|
- },
|
|
|
- {
|
|
|
- "addtime": "2021-03-09 18:51:07",
|
|
|
- "pest_name": "夜蛾",
|
|
|
- "pest_num": 4
|
|
|
- },
|
|
|
- 等于half_year返回值:
|
|
|
- "data": [
|
|
|
- {
|
|
|
- "addtime": "2021-03", 月份
|
|
|
- "pest_name": "夜蛾", 害虫名
|
|
|
- "pest_num": 54 数量
|
|
|
- },
|
|
|
- {
|
|
|
- "addtime": "2021-04",
|
|
|
- "pest_name": "夜蛾",
|
|
|
- "pest_num": 44
|
|
|
- }
|
|
|
- ],
|
|
|
- "pest_name":{
|
|
|
- "水龟虫":20
|
|
|
- }
|
|
|
- """
|
|
|
-
|
|
|
- start_time = request.POST.get("start_time")
|
|
|
- end_time = request.POST.get("end_time")
|
|
|
- ret = request.POST.get("ret")
|
|
|
- pest_name = request.POST.get("pest_name")
|
|
|
- identify_model = request.POST.get("identify_model","A")
|
|
|
- if identify_model=="A":
|
|
|
- models = CBDphoto
|
|
|
- elif identify_model=="B":
|
|
|
- models = CBDphoto_B
|
|
|
- myuser = request.myuser
|
|
|
- print(myuser.uid)
|
|
|
- date = []
|
|
|
- devices = Device.devices(uid=myuser.uid, myuser=myuser)
|
|
|
- if myuser.user_type != 4 or myuser.user_type != 1:
|
|
|
- devices = devices.filter(disable=1)
|
|
|
- for i in devices:
|
|
|
- if not myuser.user_type == 1 and i.device_expire == "1":
|
|
|
- cbd_data = models.objects.filter(~Q(indentify_photo=None),device_id=i.id,photo_status=1).filter(addtime__lte=int(i.device_expire_time))
|
|
|
- if end_time:
|
|
|
- if int(end_time) > int(i.device_expire_time):
|
|
|
- end_time = i.device_expire_time
|
|
|
- cbd_data = cbd_data.filter(addtime__range=(int(start_time),int(end_time)))
|
|
|
- else:
|
|
|
- cbd_data = models.objects.filter(~Q(indentify_photo=None),device_id=i.id,photo_status=1)
|
|
|
- if start_time:
|
|
|
- cbd_data = cbd_data.filter(addtime__range=(int(start_time),int(end_time)))
|
|
|
- for x in cbd_data:
|
|
|
- photo_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(x.addtime))
|
|
|
- if x.indentify_result:
|
|
|
- xx_result = x.indentify_result.split("#")
|
|
|
- for yy in xx_result:
|
|
|
- yy_result = yy.split(",")
|
|
|
- try:
|
|
|
- if pest_name == insect_dict[yy_result[0]]:
|
|
|
- date.append({
|
|
|
- "photo_time" : photo_time,
|
|
|
- "pest_num" : int(yy_result[1]),
|
|
|
- "pest_name" : insect_dict[yy_result[0]]})
|
|
|
- if not pest_name:
|
|
|
- date.append({
|
|
|
- "photo_time" : photo_time,
|
|
|
- "pest_num" : int(yy_result[1]),
|
|
|
- "pest_name" : insect_dict[yy_result[0]]})
|
|
|
- except:
|
|
|
- continue
|
|
|
- data = {}
|
|
|
- if ret == "day_month":
|
|
|
- print("24小时数据---近一个月数据")
|
|
|
- for item in date:
|
|
|
- photo_time, pest_name, pest_num = item['photo_time'], item['pest_name'], item['pest_num']
|
|
|
- key = '{},{}'.format(photo_time, pest_name)
|
|
|
- if key in data:
|
|
|
- data[key] += pest_num
|
|
|
- else:
|
|
|
- data[key] = pest_num
|
|
|
- result = []
|
|
|
- for key, value in data.items():
|
|
|
- photo_time,pest_name = key.split(',')
|
|
|
- result.append({
|
|
|
- "addtime" : photo_time,
|
|
|
- "pest_name" : pest_name,
|
|
|
- "pest_num" : data[key]
|
|
|
+ return {"code": 2, "msg": "参数范围越界"}
|
|
|
|
|
|
- })
|
|
|
- list2 = sorted(result, key=operator.itemgetter('addtime'))
|
|
|
- elif ret == "half_year":
|
|
|
- print("近半年_近一年")
|
|
|
- for item in date:
|
|
|
- photo_time, pest_name, pest_num = item['photo_time'], item['pest_name'], item['pest_num']
|
|
|
- addtime = photo_time.split("-")[0] + "-" + photo_time.split("-")[1]
|
|
|
- key = '{},{}'.format(addtime, pest_name)
|
|
|
- if key in data:
|
|
|
- data[key] += pest_num
|
|
|
- else:
|
|
|
- data[key] = pest_num
|
|
|
- result = []
|
|
|
- for key, value in data.items():
|
|
|
- addtime,pest_name = key.split(',')
|
|
|
- result.append({
|
|
|
- "addtime" : addtime,
|
|
|
- "pest_name" : pest_name,
|
|
|
- "pest_num" : data[key]
|
|
|
-
|
|
|
- })
|
|
|
- list2 = sorted(result, key=operator.itemgetter('addtime'))
|
|
|
- else:
|
|
|
- raise PortError("ret", "参数超出范围")
|
|
|
- datp = {}
|
|
|
- pest_num_data = []
|
|
|
- for item in date:
|
|
|
- pest_num,pest_name = item['pest_num'],item["pest_name"]
|
|
|
- key = '{}'.format(pest_name)
|
|
|
- if key in datp:
|
|
|
- datp[key] += pest_num
|
|
|
- else:
|
|
|
- datp[key] = pest_num
|
|
|
- sort_data = sorted(datp.items(), key=lambda d:d[1], reverse = True )[:6]
|
|
|
- for y in sort_data:
|
|
|
- pest_num_data.append({
|
|
|
- y[0] : y[1]
|
|
|
- })
|
|
|
- da = {
|
|
|
- "data":list2,
|
|
|
- "pest_name":pest_num_data
|
|
|
- }
|
|
|
- return da
|
|
|
|
|
|
+@kedong_deco(login_required=True)
|
|
|
+def cbdphoto_receive(request):
|
|
|
+ # 测报灯/色诱/吸虫塔/害虫图片手动添加标注接口:
|
|
|
+ img_id = request.POST.get('img_id')
|
|
|
+ mark = request.POST.get('mark')
|
|
|
+ photo_obj = MongoCBDphoto.objects.get(id=img_id)
|
|
|
+ photo_obj.is_mark = 1
|
|
|
+ photo_obj.mark = mark
|
|
|
+ photo_obj.save()
|
|
|
+ return json.loads(mark)
|