Explorar el Código

上傳檔案到 'NDVI_20201204'

allen hace 4 años
padre
commit
28ce52120a
Se han modificado 1 ficheros con 228 adiciones y 0 borrados
  1. 228 0
      NDVI_20201204/ndvi2.py

+ 228 - 0
NDVI_20201204/ndvi2.py

@@ -0,0 +1,228 @@
+import numpy as np
+import time,sys
+#import requests
+import json
+import matplotlib
+import matplotlib as mpl
+mpl.use('Agg')
+
+import matplotlib.pyplot as plt
+import datetime
+import os
+import urllib
+import urllib.request
+import urllib.parse
+import base64
+import cv2
+from PIL import Image
+import time
+import pymysql
+from concurrent.futures import ThreadPoolExecutor
+
+def img(c,x):
+    try:
+        db = pymysql.connect(host='52.69.200.169', port=3306, user='edamame', password='skyeye', database='Edamame',
+                             charset='utf8')
+        a = db.cursor()
+        if int(c) <= 9:
+            a.execute(
+                "SELECT * FROM jvt_o_time where nr ='GTW00900200" + c + "'  and mode = '" + x + "'ORDER BY  datetime DESC LIMIT 0 , 1")
+        else:
+            a.execute(
+                "SELECT * FROM jvt_o_time where nr ='GTW0090020" + c + "'  and mode = '" + x + "'ORDER BY  datetime DESC LIMIT 0 , 1")
+        result = a.fetchall()
+        if result == ():
+            s = ""
+        else:
+            s = "_" + result[0][3]
+        a.close()
+        db.close()
+        ndvis = datetime.datetime.now().strftime(
+            'C:/Users/NB/Desktop/0/' + c + '/ndvi' + x + '/%Y-%m-%d_%H.%M.tif')  # ndvi is time.tif
+        # ndvia = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'/ndvia'+x+'/%Y-%m-%d_%H.%M.png')  #ndvi is time.png
+        # ndviajpg = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'/ndvia/c.png')  #ndvi is c.png
+        irt = datetime.datetime.now().strftime(
+            'C:/Users/NB/Desktop/0/' + c + '/ir' + x + '/%Y-%m-%d_%H.%M.tif')  # irtif is time.tif
+        nt = datetime.datetime.now().strftime(
+            'C:/Users/NB/Desktop/0/' + c + '/n' + x + '/%Y-%m-%d_%H.%M.tif')  # ntif is time.tif
+        # irj = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'/irj'+x+'/%Y-%m-%d_%H.%M.jpg')  # irjpg is time.jpg
+        # nj = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'/nj'+x+'/%Y-%m-%d_%H.%M.jpg')  # njpg is time.jpg
+        date = datetime.datetime.now().strftime('\n%Y-%m-%d_%H.%Mn')
+        date += s
+        print(date)
+        label = datetime.datetime.now().strftime('No.' + c + ' Direction ' + x + ' @%Y.%m.%d.%H.%M.%S')
+        # rgb_img0 = Image.open('/var/www/html/cust/'+c+'/c/b'+x+'.tif')
+        # ir_img0 = Image.open('/var/www/html/cust/'+c+'/c/a'+x+'.tif')
+        rgb_img0 = Image.open('C:/Users/NB/Desktop/0/' + c + '/c/b' + x + '.tif')
+        ir_img0 = Image.open('C:/Users/NB/Desktop/0/' + c + '/c/a' + x + '.tif')
+
+        rgb_img0.save(nt)
+        ir_img0.save(irt)
+
+        # rgb_img0.save(nj,quality=95)
+        # ir_img0.save(irj,quality=95)
+
+        r, _, _ = rgb_img0.split()
+        r = np.asarray(r, dtype=float)
+
+        ir, _, _ = ir_img0.split()
+        ir = np.asarray(ir, dtype=float)
+
+        ndvi = np.zeros(r.size)  # The NDVI image will be the same size as the input image
+
+        np.seterr(divide='ignore', invalid='ignore')
+        ndvi = np.true_divide(np.subtract(ir, r), np.add(ir, r))
+
+        # Display the results
+        fig, ax = plt.subplots()
+        plt.plot(ndvi)
+        plt.axis('off')
+        plt.figure(figsize=(19, 10))
+        plt.imshow(ndvi, cmap=plt.cm.get_cmap('Spectral'), vmin=-1, vmax=1)
+        plt.gca().xaxis.set_major_locator(plt.NullLocator())
+        plt.gca().yaxis.set_major_locator(plt.NullLocator())
+        plt.margins(0, 0)
+        # plt.show()
+        # plt.savefig(ndvis)
+        # plt.savefig(ndvia)
+        # plt.savefig(ndviajpg,bbox_inches='tight',pad_inches=0,format='png')
+        plt.close('all')
+        f = open('C:/Users/NB/Desktop/0/' + c + '_' + x + '.txt', 'a')
+        f.write(date)
+        f.close()
+    except:
+        print('')
+
+
+
+def imgm(c,x):
+  try:
+    db = pymysql.connect(host='52.69.200.169', port=3306, user='edamame', password='skyeye', database='Edamame',
+                           charset='utf8')
+    a = db.cursor()
+    if int(c)<=9:
+        a.execute("SELECT * FROM jvt_o_time where nr ='GTW00900100"+c+"'  and mode = '"+x+"'ORDER BY  datetime DESC LIMIT 0 , 1")
+    else:
+        a.execute("SELECT * FROM jvt_o_time where nr ='GTW0090010"+c+"'  and mode = '"+x+"' ORDER BY  datetime DESC LIMIT 0 , 1")
+    result = a.fetchall()
+    if result ==():
+     s =""
+    else:
+     s = "_" + result[0][3]
+    db.close()
+    ndvis = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'m/ndvi'+x+'/%Y-%m-%d_%H.%M.tif') #ndvi is time.tif
+    ndvia = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'m/ndvia'+x+'/%Y-%m-%d_%H.%M.png')  #ndvi is time.png
+    ndviajpg = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'m/ndvia/c.png')  #ndvi is c.png
+    irt = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'m/ir'+x+'/%Y-%m-%d_%H.%M.tif')  # irtif is time.tif
+    nt = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'m/n'+x+'/%Y-%m-%d_%H.%M.tif')  # ntif is time.tif
+    #irj = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'m/irj'+x+'/%Y-%m-%d_%H.%M.jpg')  # irjpg is time.jpg
+    #nj = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'m/nj'+x+'/%Y-%m-%d_%H.%M.jpg')  # njpg is time.jpg
+    date = datetime.datetime.now().strftime('\n%Y-%m-%d_%H.%Mn')
+    date += s
+    label= datetime.datetime.now().strftime('No.'+c+' Direction '+x+' @%Y.%m.%d.%H.%M.%S')
+    
+    rgb_img0 = Image.open('/var/www/html/cust/'+c+'m/c/b'+x+'.tif')
+    ir_img0 = Image.open('/var/www/html/cust/'+c+'m/c/a'+x+'.tif')
+
+    rgb_img0.save(nt)
+    ir_img0.save(irt)
+
+    #rgb_img0.save(nj,quality=95)
+    #ir_img0.save(irj,quality=95)
+
+    r,_,_ = rgb_img0.split()
+    r = np.asarray(r, dtype=float)
+      
+      
+    ir,_,_ = ir_img0.split()
+    ir = np.asarray(ir, dtype=float)
+      
+      
+    ndvi = np.zeros(r.size)  # The NDVI image will be the same size as the input image
+      
+      
+    np.seterr(divide='ignore', invalid='ignore')
+    ndvi = np.true_divide(np.subtract(ir, r), np.add(ir, r))
+            
+    
+    # Display the results
+    fig, ax = plt.subplots()
+    plt.plot(ndvi)
+    plt.axis('off')
+    plt.figure(figsize=(19, 10))
+    plt.imshow(ndvi, cmap=plt.cm.get_cmap('Spectral'), vmin=-1, vmax=1)
+    plt.gca().xaxis.set_major_locator(plt.NullLocator())
+    plt.gca().yaxis.set_major_locator(plt.NullLocator())
+    plt.margins(0, 0)
+    #plt.show()
+    plt.savefig(ndvis)
+    plt.savefig(ndvia)
+    plt.savefig(ndviajpg,bbox_inches='tight',pad_inches=0,format='png')
+    plt.close('all')
+    f = open('/var/www/html/cust/c'+c+'m_'+x+'.txt','a')
+    f.write(date)
+    f.close()
+
+    dress = '/var/www/html/cust/'+c+'m/c/'
+    for root, dirs, files in os.walk(dress):
+        print('files: {}'.format(len(files)))
+        if len(files)>=1:
+            files.sort()
+            for f in files:
+                with open(os.path.join(root, f), "rb") as imageFile:
+                    str = base64.b64encode(imageFile.read())
+                url = 'http://60.250.156.230/cust/tm1.php'
+                values = {'data1':str, 'name':f}
+                data1 = urllib.parse.urlencode(values)
+                data1 = data1.encode('utf-8')
+                req = urllib.request.Request(url, data1)
+                req.add_header('User-Agent','Magic Browser')
+                resp = urllib.request.urlopen(req)
+                respdata1 = resp.read()
+                print(os.path.join(root, f))
+                #print(respdata1)
+                os.remove(os.path.join(root, f))
+                print("ok")
+  except:
+    print('error')
+
+def main():
+       start1 = time.time()
+       for i in range(18):
+            d = str(i+1)
+            for j in range(8):
+                e = str(j+1)
+                img(d,e)
+       end1 = time.time()
+       print("time1: " + str(end1 - start1))
+      # pool = ThreadPoolExecutor(180)
+       #for i in range(18):
+        #   d = str(i+1)
+         #  for j in range(8):
+          #  e = str(j+1)
+           # pool.submit(img, d, e)
+       #end1 = time.time()
+       #print("time1: " + str(end1 - start1))
+       # with ThreadPoolExecutor(180) as executor1:
+        #    for i in range(18):
+         #       d = str(i+1)
+          #      for j in range(8):
+           #         e = str(j+1)
+            #        executor1.submit(img,d,e)
+        #executor1.shutdown()
+        #end2 = time.time()
+        #print("time2: " + str(end2 - end1))
+        #with ThreadPoolExecutor(180) as executor2:
+         #   for i in range(4):
+            #    a = str(i)
+          #      for j in range(9):
+           #         b = str(j)
+                    #executor2.submit(imgm,a,b)
+        #executor2.shutdown()
+        #end3 = time.time()
+        #print("time3: " + str(end3 - end2))
+
+
+while True:
+    print("start")
+    main()