ndvi2.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import numpy as np
  2. import time,sys
  3. #import requests
  4. import json
  5. import matplotlib
  6. import matplotlib as mpl
  7. mpl.use('Agg')
  8. import matplotlib.pyplot as plt
  9. import datetime
  10. import os
  11. import urllib
  12. import urllib.request
  13. import urllib.parse
  14. import base64
  15. import cv2
  16. from PIL import Image
  17. import time
  18. import pymysql
  19. from concurrent.futures import ThreadPoolExecutor
  20. def img(c,x):
  21. try:
  22. db = pymysql.connect(host='52.69.200.169', port=3306, user='edamame', password='skyeye', database='Edamame',
  23. charset='utf8')
  24. a = db.cursor()
  25. if int(c) <= 9:
  26. a.execute(
  27. "SELECT * FROM jvt_o_time where nr ='GTW00900200" + c + "' and mode = '" + x + "'ORDER BY datetime DESC LIMIT 0 , 1")
  28. else:
  29. a.execute(
  30. "SELECT * FROM jvt_o_time where nr ='GTW0090020" + c + "' and mode = '" + x + "'ORDER BY datetime DESC LIMIT 0 , 1")
  31. result = a.fetchall()
  32. if result == ():
  33. s = ""
  34. else:
  35. s = "_" + result[0][3]
  36. a.close()
  37. db.close()
  38. ndvis = datetime.datetime.now().strftime(
  39. 'C:/Users/NB/Desktop/0/' + c + '/ndvi' + x + '/%Y-%m-%d_%H.%M.tif') # ndvi is time.tif
  40. # ndvia = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'/ndvia'+x+'/%Y-%m-%d_%H.%M.png') #ndvi is time.png
  41. # ndviajpg = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'/ndvia/c.png') #ndvi is c.png
  42. irt = datetime.datetime.now().strftime(
  43. 'C:/Users/NB/Desktop/0/' + c + '/ir' + x + '/%Y-%m-%d_%H.%M.tif') # irtif is time.tif
  44. nt = datetime.datetime.now().strftime(
  45. 'C:/Users/NB/Desktop/0/' + c + '/n' + x + '/%Y-%m-%d_%H.%M.tif') # ntif is time.tif
  46. # irj = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'/irj'+x+'/%Y-%m-%d_%H.%M.jpg') # irjpg is time.jpg
  47. # nj = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'/nj'+x+'/%Y-%m-%d_%H.%M.jpg') # njpg is time.jpg
  48. date = datetime.datetime.now().strftime('\n%Y-%m-%d_%H.%Mn')
  49. date += s
  50. print(date)
  51. label = datetime.datetime.now().strftime('No.' + c + ' Direction ' + x + ' @%Y.%m.%d.%H.%M.%S')
  52. # rgb_img0 = Image.open('/var/www/html/cust/'+c+'/c/b'+x+'.tif')
  53. # ir_img0 = Image.open('/var/www/html/cust/'+c+'/c/a'+x+'.tif')
  54. rgb_img0 = Image.open('C:/Users/NB/Desktop/0/' + c + '/c/b' + x + '.tif')
  55. ir_img0 = Image.open('C:/Users/NB/Desktop/0/' + c + '/c/a' + x + '.tif')
  56. rgb_img0.save(nt)
  57. ir_img0.save(irt)
  58. # rgb_img0.save(nj,quality=95)
  59. # ir_img0.save(irj,quality=95)
  60. r, _, _ = rgb_img0.split()
  61. r = np.asarray(r, dtype=float)
  62. ir, _, _ = ir_img0.split()
  63. ir = np.asarray(ir, dtype=float)
  64. ndvi = np.zeros(r.size) # The NDVI image will be the same size as the input image
  65. np.seterr(divide='ignore', invalid='ignore')
  66. ndvi = np.true_divide(np.subtract(ir, r), np.add(ir, r))
  67. # Display the results
  68. fig, ax = plt.subplots()
  69. plt.plot(ndvi)
  70. plt.axis('off')
  71. plt.figure(figsize=(19, 10))
  72. plt.imshow(ndvi, cmap=plt.cm.get_cmap('Spectral'), vmin=-1, vmax=1)
  73. plt.gca().xaxis.set_major_locator(plt.NullLocator())
  74. plt.gca().yaxis.set_major_locator(plt.NullLocator())
  75. plt.margins(0, 0)
  76. # plt.show()
  77. # plt.savefig(ndvis)
  78. # plt.savefig(ndvia)
  79. # plt.savefig(ndviajpg,bbox_inches='tight',pad_inches=0,format='png')
  80. plt.close('all')
  81. f = open('C:/Users/NB/Desktop/0/' + c + '_' + x + '.txt', 'a')
  82. f.write(date)
  83. f.close()
  84. except:
  85. print('')
  86. def imgm(c,x):
  87. try:
  88. db = pymysql.connect(host='52.69.200.169', port=3306, user='edamame', password='skyeye', database='Edamame',
  89. charset='utf8')
  90. a = db.cursor()
  91. if int(c)<=9:
  92. a.execute("SELECT * FROM jvt_o_time where nr ='GTW00900100"+c+"' and mode = '"+x+"'ORDER BY datetime DESC LIMIT 0 , 1")
  93. else:
  94. a.execute("SELECT * FROM jvt_o_time where nr ='GTW0090010"+c+"' and mode = '"+x+"' ORDER BY datetime DESC LIMIT 0 , 1")
  95. result = a.fetchall()
  96. if result ==():
  97. s =""
  98. else:
  99. s = "_" + result[0][3]
  100. db.close()
  101. ndvis = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'m/ndvi'+x+'/%Y-%m-%d_%H.%M.tif') #ndvi is time.tif
  102. ndvia = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'m/ndvia'+x+'/%Y-%m-%d_%H.%M.png') #ndvi is time.png
  103. ndviajpg = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'m/ndvia/c.png') #ndvi is c.png
  104. irt = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'m/ir'+x+'/%Y-%m-%d_%H.%M.tif') # irtif is time.tif
  105. nt = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'m/n'+x+'/%Y-%m-%d_%H.%M.tif') # ntif is time.tif
  106. #irj = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'m/irj'+x+'/%Y-%m-%d_%H.%M.jpg') # irjpg is time.jpg
  107. #nj = datetime.datetime.now().strftime('/var/www/html/cust/'+c+'m/nj'+x+'/%Y-%m-%d_%H.%M.jpg') # njpg is time.jpg
  108. date = datetime.datetime.now().strftime('\n%Y-%m-%d_%H.%Mn')
  109. date += s
  110. label= datetime.datetime.now().strftime('No.'+c+' Direction '+x+' @%Y.%m.%d.%H.%M.%S')
  111. rgb_img0 = Image.open('/var/www/html/cust/'+c+'m/c/b'+x+'.tif')
  112. ir_img0 = Image.open('/var/www/html/cust/'+c+'m/c/a'+x+'.tif')
  113. rgb_img0.save(nt)
  114. ir_img0.save(irt)
  115. #rgb_img0.save(nj,quality=95)
  116. #ir_img0.save(irj,quality=95)
  117. r,_,_ = rgb_img0.split()
  118. r = np.asarray(r, dtype=float)
  119. ir,_,_ = ir_img0.split()
  120. ir = np.asarray(ir, dtype=float)
  121. ndvi = np.zeros(r.size) # The NDVI image will be the same size as the input image
  122. np.seterr(divide='ignore', invalid='ignore')
  123. ndvi = np.true_divide(np.subtract(ir, r), np.add(ir, r))
  124. # Display the results
  125. fig, ax = plt.subplots()
  126. plt.plot(ndvi)
  127. plt.axis('off')
  128. plt.figure(figsize=(19, 10))
  129. plt.imshow(ndvi, cmap=plt.cm.get_cmap('Spectral'), vmin=-1, vmax=1)
  130. plt.gca().xaxis.set_major_locator(plt.NullLocator())
  131. plt.gca().yaxis.set_major_locator(plt.NullLocator())
  132. plt.margins(0, 0)
  133. #plt.show()
  134. plt.savefig(ndvis)
  135. plt.savefig(ndvia)
  136. plt.savefig(ndviajpg,bbox_inches='tight',pad_inches=0,format='png')
  137. plt.close('all')
  138. f = open('/var/www/html/cust/c'+c+'m_'+x+'.txt','a')
  139. f.write(date)
  140. f.close()
  141. dress = '/var/www/html/cust/'+c+'m/c/'
  142. for root, dirs, files in os.walk(dress):
  143. print('files: {}'.format(len(files)))
  144. if len(files)>=1:
  145. files.sort()
  146. for f in files:
  147. with open(os.path.join(root, f), "rb") as imageFile:
  148. str = base64.b64encode(imageFile.read())
  149. url = 'http://60.250.156.230/cust/tm1.php'
  150. values = {'data1':str, 'name':f}
  151. data1 = urllib.parse.urlencode(values)
  152. data1 = data1.encode('utf-8')
  153. req = urllib.request.Request(url, data1)
  154. req.add_header('User-Agent','Magic Browser')
  155. resp = urllib.request.urlopen(req)
  156. respdata1 = resp.read()
  157. print(os.path.join(root, f))
  158. #print(respdata1)
  159. os.remove(os.path.join(root, f))
  160. print("ok")
  161. except:
  162. print('error')
  163. def main():
  164. start1 = time.time()
  165. for i in range(18):
  166. d = str(i+1)
  167. for j in range(8):
  168. e = str(j+1)
  169. img(d,e)
  170. end1 = time.time()
  171. print("time1: " + str(end1 - start1))
  172. # pool = ThreadPoolExecutor(180)
  173. #for i in range(18):
  174. # d = str(i+1)
  175. # for j in range(8):
  176. # e = str(j+1)
  177. # pool.submit(img, d, e)
  178. #end1 = time.time()
  179. #print("time1: " + str(end1 - start1))
  180. # with ThreadPoolExecutor(180) as executor1:
  181. # for i in range(18):
  182. # d = str(i+1)
  183. # for j in range(8):
  184. # e = str(j+1)
  185. # executor1.submit(img,d,e)
  186. #executor1.shutdown()
  187. #end2 = time.time()
  188. #print("time2: " + str(end2 - end1))
  189. #with ThreadPoolExecutor(180) as executor2:
  190. # for i in range(4):
  191. # a = str(i)
  192. # for j in range(9):
  193. # b = str(j)
  194. #executor2.submit(imgm,a,b)
  195. #executor2.shutdown()
  196. #end3 = time.time()
  197. #print("time3: " + str(end3 - end2))
  198. while True:
  199. print("start")
  200. main()