| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540 |
- from celery import shared_task
- import time
- import os
- from imutils import paths
- import stat
- import datetime
- import re
- import copy
- from django.utils.decorators import method_decorator
- from django.views.decorators.csrf import csrf_exempt
- # import xlrd
- from apps.SimInfo.views import get_siminfo
- import requests
- from io import BytesIO
- import sys
- from PIL import Image
- import argparse
- import cv2
- import shutil
- import glob
- from apps.AppInfoManage.models import Equip, User_Log, MyUser,Equip_Forward
- from apps.AppInfoManage.models import SCDdata, SCDstatus, RecentSCDdata, YBQdata, YBQstatus, QXZstatus, QXZdata, Trapstatus, Trapdata, Trapphoto, Equip_SimInfo
- from apps.AppInfoManage.models import CBDstatus, CBDdata, CBDphoto, CBDpest, CBDphoto_desc, YBQphoto, BZYstatus, BZYdata, BZYphoto, Spore_Exist, QXZswitchstatus, QXZswitchdata,\
- RecentCBDdata, QXZTimingswitch, QXZAutoswitch, Msg_Conf
- import json
- import time
- import paho.mqtt.client as mqtt
- from apps.EquipManage.views import CJSONEncoder
- # 复制
- def mycopyfile(srcfile,dstfile):
- if not os.path.isfile(srcfile):
- print("%s not exist!"%(srcfile))
- else:
- fpath,fname=os.path.split(dstfile) #分离文件名和路径
- if not os.path.exists(fpath):
- os.makedirs(fpath) #创建路径
- shutil.copyfile(srcfile,dstfile) #复制文件
- print("copy %s -> %s"%(srcfile,dstfile))
- def mkdir(path):
-
- # 去除首位空格
- path=path.strip()
- # 去除尾部 \ 符号
- path=path.rstrip("\\")
-
- # 判断路径是否存在
- # 存在 True
- # 不存在 False
- isExists=os.path.exists(path)
-
- # 判断结果
- if not isExists:
- # 如果不存在则创建目录
- # 创建目录操作函数
- os.makedirs(path)
- print(path+' 创建成功')
- return True
- else:
- # 如果目录存在则不创建,并提示目录已存在
- print(path+' 目录已存在')
- return False
- def variance_of_laplacian(image):
- # 拉普拉斯的方差
- return cv2.Laplacian(image, cv2.CV_64F).var()
- @shared_task
- def photo_transpond(imei,a_path,a_name):
- # FTP转发图片
- nowtime = datetime.datetime.now().strftime('%Y%m%d')
- origin = sys.stdout
- mkdir('logs/%s'%imei)
- f = open('logs/%s/'%imei + nowtime +'.txt','a')
- sys.stdout = f
- img_addr = "pyftp/" + os.path.join(a_path, a_name)
- try:
- # if imei == "868575028869008":
- #丁艳青 云洋 更换设备号
- if imei == "869300039229913" or imei == "860344048751540":
- # if imei == "868956044831497" or imei == "865650043175898":
- print("img_addr",img_addr)
- time.sleep(20)
- a = "/data/yfwlw/" + img_addr
- # a = "/home/web/yfwlw/" + img_addr
- # c = event.name
- # b = c.replace(':','-')
- # 切割文件名
- # aa = img_addr.split("/")
- # 拼接文件夹
- # b = aa[3] + "/" + aa[4] + "/" + b
- n_time = datetime.datetime.now()
- b = imei + "/" + n_time.strftime('%Y%m%d') + "/" + n_time.strftime('%H-%M-%S') + ".jpg"
- # print("bbbbbbb",b)
- print("target_ftp_name:",b)
- ftp_url = os.system(r'curl ftp://47.95.247.86:8022/%s --ftp-create-dirs -u "hnyfkj:hnyfkj19416" -T "%s" -# -v'%(b,a))
- print("=====>>",ftp_url)
- else:
- pass
- if imei == "869300039255967" or imei == "869300039253798" or imei == "868575028876029":
- print("img_addr",img_addr)
- time.sleep(20)
- a = "/data/yfwlw/" + img_addr
- # a = "/home/web/yfwlw/" + img_addr
- # c = event.name
- # b = c.replace(':','-')
- # 切割文件名
- # aa = img_addr.split("/")
- # 拼接文件夹
- # b = aa[3] + "/" + aa[4] + "/" + b
- n_time = datetime.datetime.now()
- b = imei + "/" + n_time.strftime('%Y%m%d') + "/" + n_time.strftime('%H-%M-%S') + ".jpg"
- print("target_ftp_name:",b)
- ftp_url = os.system(r'curl ftp://47.95.247.86:8022/%s --ftp-create-dirs -u "xjhnyfkj:xjhnyfkj511" -T "%s" -# -v'%(b,a))
- print("=====>>",ftp_url)
- else:
- pass
- except Exception as eee:
- print(eee)
-
- try:
- time.sleep(10)
- # insect_indentify 昆虫识别
- # # 保存图片之后调用害虫识别接口
- # 正则切割文件名 去掉.jpg后缀
- a_name = a_name.replace(".jpg","")
- # pattern = r'[.]'
- # res = a_name
- # result = re.split(pattern, res)
- # insect_pic_name = result[0] + 'result' + '.jpg'
- insect_pic_name = a_name + 'result' + '.jpg'
- # url = 'http://60.166.14.184:9143/upload'
- url = 'http://60.166.14.184:9143/upload'
- # 测试服务器路径
- # imageFile = {'imageFile':('pic.jpg',open("/home/web/yfwlw/" + img_addr,'rb'),'image/jpeg')}
- lng = CBDstatus.objects.get(equip_id=imei).lng
- lat = CBDstatus.objects.get(equip_id=imei).lat
- # 正式服务器路径
- imageFile = {'imageFile':('pic.jpg',open("/data/yfwlw/" + img_addr,'rb'),'image/jpeg')}
- data = {'longitude':lng,'latitude':lat}
- print('data:',data)
- res = requests.post(url,data=data,files=imageFile,timeout=10)
- print("res:",res.text)
- resp = json.loads(res.text)
- print("insect_pic_name:", insect_pic_name)
- print("-------------------------接口调用成功")
-
- except Exception as e:
- print(e)
- print("---------------------识别失败")
- try:
- if resp["returnResult"]["returnStatus"] == "0":
- print("识别结果为空,不进行保存")
- else:
- # 将识别结果中的链接中的图片存入本地 测试服务器
- # pic_dir_1 = "/home/web/yfwlw/result_pyftp/" + a_path + "/"
- pic_dir_1 = "/data/yfwlw/result_pyftp/" + a_path + "/"
- print("pic_dir_1:",pic_dir_1)
- if os.path.exists(pic_dir_1) == False:
- os.makedirs(pic_dir_1)
- img_src = resp["returnResult"]["returnImageUrl"]
- response = requests.get(img_src)
- image = Image.open(BytesIO(response.content))
- insect_image_addr = pic_dir_1 + insect_pic_name
- print("insect_image_addr:",insect_image_addr)
- image.save(insect_image_addr)
- print("------------------图片保存成功")
- try:
- photo_obj = CBDphoto.objects.get(addr=img_addr)
- if resp["returnResult"]["returnStatus"] == "0":
- print("识别结果为空,不进行保存")
- else:
- # 将识别结果存入数据库
- pic_dir = "result_pyftp/" + os.path.join(a_path, insect_pic_name)
-
- photo_obj.indentify_photo = pic_dir
- photo_obj.indentify_result = resp["returnResult"]["returnString"]
- photo_obj.save()
- print("图片路径:", pic_dir)
- print("---------------数据库保存识别结果成功")
- except Exception as ex:
- print(ex)
- print("---------------数据库保存识别结果失败")
- except Exception as exc:
- print(exc)
- print("----------------识别结果图片保存失败")
-
- try:
- if imei == "868956044807455" or imei == "868956044806671" or imei == "868956044831604" or imei == "868956044831711" or imei == "868956044807471" or imei == "868956044830994" or imei == "868956044824930" or imei == "868956044807455" or imei == "865650043209341" or imei == "868956044806705" or imei == "868956044830929" or imei == "868956044824898" or imei == "868956044830887" or imei == "868956044830911":
- try:
- if CBDphoto.objects.get(addr=img_addr).indentify_photo == None:
- indentify_photo_1 = ""
- indentify_result_1 = ""
- img_addr_1 = "http://120.27.222.26/" + img_addr
- else:
- r_photo = CBDphoto.objects.get(addr=img_addr).indentify_photo
- indentify_result_1 = CBDphoto.objects.get(addr=img_addr).indentify_result
- img_addr_1 = "http://120.27.222.26/" + img_addr
- indentify_photo_1 = "http://120.27.222.26/" + r_photo
- data = {
- "cmd": "Imageresult",
- "ext": {
- "Image": img_addr_1,
- "Result_image": indentify_photo_1,
- "Result": indentify_result_1,
- "imei": imei
- }
- }
- except Exception as eee:
- print("====京东错误信息为",eee)
- data = {
- "cmd": "Imageresult",
- "ext": {
- "Image": "",
- "Result_image": "",
- "Result": "",
- "imei": imei
- }
- }
- TASK_TOPIC = '/yfkj/cbd/sub/%s'%imei # 客户端发布消息主题
- """
- 客户端发布消息
- :param message: 消息主体
- :return:
- """
- payload = data
- # publish(主题:Topic; 消息内容)
- client_id = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
- client = mqtt.Client(client_id, transport='tcp')
- client.connect("120.27.222.26", 1883, 60) # 此处端口默认为1883,通信端口期keepalive默认60
- client.loop_start()
- client.publish(TASK_TOPIC, json.dumps(payload, ensure_ascii=False))
- print("京东信息发送成功",data)
- # equip = Equip.objects.get(equip_id=imei)
- # if equip.ftp_addr:
- # # http://120.27.222.26/cbd/pyftp/ftp_file/ykm_cbd/868575028845867/20190305/20:56:15.jpg
- # ftp_addr = equip.ftp_addr
- # pattern = r'[#]'
- # split_result = re.split(pattern, ftp_addr)
- # print("split_result:",split_result)
- # try:
- # need_result = split_result[1]
- # except:
- # print("对接设备的地址填写有误!请检查!")
- # need_result = "0"
- print("imei------------------------>>",imei)
- try:
- equip_list = Equip_Forward.objects.get(equip_id=imei)
- ftp_addr = equip_list.equip_img_url
- pattern = r'[#]'
- split_result = re.split(pattern, ftp_addr)
- print("split_result:",split_result)
- need_result = split_result[1]
- except:
- print("对接设备的地址填写有误!请检查!")
- need_result = "400"
- #害虫识别
- if need_result == "1":
- if CBDphoto.objects.get(addr=img_addr).indentify_result == None:
- indentify_photo_1 = CBDphoto.objects.get(addr=img_addr).indentify_result
- else:
- r_photo = CBDphoto.objects.get(addr=img_addr).indentify_photo
- indentify_photo_1 = "http://120.27.222.26/" + r_photo
- try:
- # r_photo = CBDphoto.objects.get(addr=img_addr).indentify_photo
- indentify_result_1 = CBDphoto.objects.get(addr=img_addr).indentify_result
- img_addr_1 = "http://120.27.222.26/" + img_addr
- # indentify_photo_1 = "http://120.27.222.26/" + r_photo
- print("img_addr_1:",img_addr_1)
- print("indentify_photo_1:",indentify_photo_1)
- print("indentify_result_1:",indentify_result_1)
- if indentify_result_1 == None:
- data = {
- "Image":img_addr_1,
- "imei":imei,
- "Result_image":"0",
- "Result":"0",
- }
- else:
- data = {
- "Image":img_addr_1,
- "Result_image":indentify_photo_1,
- "Result":indentify_result_1,
- "imei":imei
- }
-
- data = json.dumps(data, cls=CJSONEncoder)
- print("data:",data)
- if equip_list.equip_img_between == "1":
- res_1 = requests.post(split_result[0],data=data,timeout=2)
- elif equip_list.equip_img_between == "2":
- headers = {"Content-Type": "application/json; charset=UTF-8", 'Connection': 'close'}
- res_1 = requests.post(split_result[0], data=data, timeout=3,headers=headers)
- print("res_1:",res_1)
- print("------------------识别结果发送成功!!!")
- except Exception as aex:
- print(aex)
- try:
- img_addr_1 = "http://120.27.222.26/" + img_addr
- print("img_addr_1:",img_addr_1)
- data = {
- "Image":img_addr_1,
- "imei":imei,
- "Result_image":"0",
- "Result":"0",
- }
- data = json.dumps(data, cls=CJSONEncoder)
- print("data:",data)
- print("url:",split_result[0])
- if equip_list.equip_img_between == "1":
- res_1 = requests.post(split_result[0],data=data,timeout=2)
- elif equip_list.equip_img_between == "2":
- headers = {"Content-Type": "application/json; charset=UTF-8", 'Connection': 'close'}
- res_1 = requests.post(split_result[0], data=data, timeout=3,headers=headers)
- print(res_1.text)
- except Exception as e:
- print("错误信息为",e)
- print("------------------识别结果发送失败!!!")
- #害虫计数
- elif need_result == "2":
- try:
- # r_photo = CBDphoto.objects.get(addr=img_addr).indentify_photo
- indentify_result_1 = CBDphoto.objects.get(addr=img_addr).indentify_result
- img_addr_1 = "http://120.27.222.26/" + img_addr
- # indentify_photo_1 = "http://120.27.222.26/" + r_photo
- print("img_addr_1:",img_addr_1)
- print("indentify_result_1:",indentify_result_1)
- data = {
- "Image":img_addr_1,
- "Result":indentify_result_1,
- "imei":imei
- }
- data = json.dumps(data, cls=CJSONEncoder)
- print("data:",data)
- if equip_list.equip_img_between == "1":
- res_1 = requests.post(split_result[0],data=data,timeout=2)
- elif equip_list.equip_img_between == "2":
- headers = {"Content-Type": "application/json; charset=UTF-8", 'Connection': 'close'}
- res_1 = requests.post(split_result[0], data=data, timeout=3,headers=headers)
- print("res_1:",res_1)
- print("------------------识别结果发送成功!!!")
- except Exception as aex:
- print(aex)
- try:
- img_addr_1 = "http://120.27.222.26/" + img_addr
- print("img_addr_1:",img_addr_1)
- data = {
- "Image":img_addr_1,
- "imei":imei,
- "Result":"0",
- }
- data = json.dumps(data, cls=CJSONEncoder)
- print("data:",data)
- print("url:",split_result[0])
- if equip_list.equip_img_between == "1":
- res_1 = requests.post(split_result[0],data=data,timeout=2)
- elif equip_list.equip_img_between == "2":
- headers = {"Content-Type": "application/json; charset=UTF-8", 'Connection': 'close'}
- res_1 = requests.post(split_result[0], data=data, timeout=3,headers=headers)
- print(res_1.text)
- except Exception as e:
- print("错误信息为",e)
- print("------------------识别结果发送失败!!!")
- #不带害虫识别
- elif need_result == "0":
- try:
- img_addr_1 = "http://120.27.222.26/" + img_addr
- print("img_addr_1:",img_addr_1)
- data = {
- "Image":img_addr_1,
- "imei":imei
- }
- data = json.dumps(data, cls=CJSONEncoder)
- print("data:",data)
- print("url:",split_result[0])
- if equip_list.equip_img_between == "1":
- res_1 = requests.post(split_result[0],data=data,timeout=2)
- elif equip_list.equip_img_between == "2":
- headers = {"Content-Type": "application/json; charset=UTF-8", 'Connection': 'close'}
- res_1 = requests.post(split_result[0], data=data, timeout=3,headers=headers)
- print("res_1:",res_1.text)
- # 判断文件大小的方法
- # path = 'F:\YFWLW\yfwlw\\16_19_21.jpg'
- # size = os.path.getsize(path)/1024
- print("------------------对接设备图片发送成功!!!")
- except Exception as aexc:
- print(aexc)
- print("------------------对接设备图片发送失败!!!")
- except Exception as exce:
- print(exce)
- print("!!!")
- equip = Equip.objects.get(equip_id=imei)
- if equip.scene == "1":
- mycopyfile("../pyftp/" + os.path.join(a_path, a_name),"../pyftp/scene1/" + os.path.join(a_path, a_name))
- # img_addr = "pyftp/scene1/" + os.path.join(event.path, event.name)
- elif equip.scene == "2":
- mycopyfile("../pyftp/" + os.path.join(a_path, a_name),"../pyftp/scene2/" + os.path.join(a_path, a_name))
- # img_addr = "pyftp/scene1/" + os.path.join(event.path, event.name)
- elif equip.scene == "3":
- mycopyfile("../pyftp/" + os.path.join(a_path, a_name),"../pyftp/scene3/" + os.path.join(a_path, a_name))
- # img_addr = "pyftp/scene1/" + os.path.join(event.path, event.name)
- elif equip.scene == "4":
- mycopyfile("../pyftp/" + os.path.join(a_path, a_name),"../pyftp/scene4/" + os.path.join(a_path, a_name))
- # img_addr = "pyftp/scene1/" + os.path.join(event.path, event.name)
- sys.stdout = origin
- f.close()
- return
- @shared_task
- def test(imei):
- # time.sleep(20)
- nowtime = datetime.datetime.now().strftime('%Y%m%d')
- origin = sys.stdout
- f = open('logs/'+"bzyphoto" + nowtime +'.txt','a')
- sys.stdout = f
- print('=================================================')
- print("<-----imei:\n" + imei + ';\n')
- print("Message:\n" + "http:bzyphoto" + "----->\n")
- print("%s"%datetime.datetime.now())
- print('=================================================')
-
- pic_name = datetime.datetime.now().strftime('%Y%m')
-
- try:
- args = "pyftp/ftp_file/bzy_test/" + imei
- cv_num_list = []
- file_list1 = list(paths.list_images(args))
- print("\n===========================")
- print("file_list1:",file_list1)
- print("type file_list1:",type(file_list1))
- cv_num_list1 = []
- for imagePath1 in file_list1[::]:
- # fm = get_value(imagePath)
- # 孢子仪图片过小删除
- if os.path.getsize(imagePath1) <= 10:
- file_list1.remove(imagePath1)
- img1 = cv2.imread(imagePath1)
- img1 = cv2.resize(img1, (800, 600))
- gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
- # 创建SIFT对象
- sift = cv2.xfeatures2d.SIFT_create()
- keypoints, descriptor = sift.detectAndCompute(gray, None)
- img1 = cv2.drawKeypoints(image=img1, outImage=img1, keypoints=keypoints,flags=cv2.DRAW_MATCHES_FLAGS_DEFAULT, color=(0, 0, 255))
- fm1 = len(keypoints)
- cv_num_list1.append(fm1)
- # 打印清晰度值:
- # print("fm1",fm1)
- # # 打印文件路径
- # print("imagePath1",imagePath1)
- file_cv_tuple1 = tuple(zip(cv_num_list1,file_list1))
- print("file_cv_tuple1:\n",file_cv_tuple1)
- print("=========================================/n")
- file_cv_tuple_list1 = list(file_cv_tuple1)
- print("file_cv_tuple_list1:\n",file_cv_tuple_list1)
- print("*****************************************/n")
- # 按照清晰度排序:, reverse=True降序
- result1 = sorted(file_cv_tuple_list1, key=lambda s: s[0],reverse=True)
- print("result1:\n",result1)
- # 切片取清晰度最高的前五张图片:
- result1_5 = result1[0:3]
- # result5 = result[0:1]
- print("result1_5:\n",result1_5)
- pic_name = datetime.datetime.now().strftime('%Y%m')
- # 定义要创建的目录:
- mkpath="./pyftp/ftp_file/bzy_photo/" + imei + "/" + pic_name
- # 调用函数,创建目录:
- mkdir(mkpath)
- for i in result1_5:
- # for i in result5:
- #i值:(137.06453470539032, 'images\\192.168.2.64_01_20190802095315882_ALARM_INPUT.jpg')
- print("file path:",i[1])
- #复制文件:
- shutil.copy(i[1],mkpath)
- # os.system("rm ./pyftp/ftp_file/bzy_test/%s/*.jpg -rf"%imei)
-
- os.system("mv ./pyftp/ftp_file/bzy_test/%s ./pyftp/ftp_file/bzy_test/%s_%s"%(imei,imei,datetime.datetime.now().strftime("%Y%m%d%H%M%S")))
- print("-----------------image move success----------------------")
- equip = Equip.objects.get(equip_id=imei)
- file_dir="pyftp/ftp_file/bzy_photo/" + imei + "/" + pic_name
- for root, dirs, files in os.walk(file_dir):
- print("当前目录为=========>>",root) #当前目录路径
- print("当前子目录为=========>>",dirs) #当前路径下所有子目录
- print("当前目录文件为=========>>",files) #当前路径下所有非目录子文件
- for i in files:
- photo_addr = file_dir + "/" + i
- print("文件夹图片路径===========》》",photo_addr)
- if BZYphoto.objects.filter(addr=photo_addr).exists():
- print("照片存在")
- else:
- bzys = BZYphoto.objects.create(equip_id=equip, addr=photo_addr)
- print("照片不存在")
- if equip.ftp_addr:
- bzy_imgs = "http://www.yfzhwlw.com/bzy/" + bzys.addr
- data = {
- "Image":bzy_imgs,
- "imei":bzys.equip_id.equip_id,
- }
- data = json.dumps(data)
- headers = {"Content-Type": "application/json; charset=UTF-8", 'Connection': 'close'}
- bzy_photo = requests.post(equip.ftp_addr,data=data,headers=headers,timeout=3)
- print("转发成功")
- except Exception as a:
- print('=================================================')
- print("<-----imei:\n" + imei + ';\n')
- print("Message:\n" + "http:bzyphoto" + "----->\n")
- print("%s"%datetime.datetime.now())
- print("==========%s========"%a)
- print("-----------------image move default----------------------")
- sys.stdout = origin
- f.close()
- return
|