import ctypes
import platform
from pathlib import Path
import cv2
import uvicorn
from fastapi import FastAPI
app = FastAPI()
from libtiff import TIFF
import numpy as np
import os
import sys
import tifffile
import time
from shutil import copyfile
np.set_printoptions(threshold=np.sys.maxsize)
import base64
def in_out_zone(midhang, midlie, s1, s2):
if ((midhang - 5).any() <= 0):
up_in = 1
else:
up_in = midhang - 5
if ((midlie - 5).any() <= 0):
left_in = 1
else:
left_in = midlie - 5
if ((midhang + 5).any() >= s1):
down_in = s1
else:
down_in = midhang + 5
if ((midlie + 5).any() >= s2):
right_in = s2
else:
right_in = midlie + 5
if ((midhang - 15).any() <= 0):
up_out = 1
else:
up_out = midhang - 15
if ((midlie - 15).any() <= 0):
left_out = 1
else:
left_out = midlie - 15
if ((midhang + 15).any() >= s1):
down_out = s1
else:
down_out = midhang + 15
if ((midlie + 15).any() >= s2):
right_out = s2
else:
right_out = midlie + 15
return up_in, down_in, left_in, right_in, up_out, down_out, left_out, right_out
def flags(hang, lie, pixels, s1, s2):
if ((hang - pixels).any() <= 0):
up = 1
else:
up = hang - pixels
if ((hang + pixels).any() > s1):
down = s1
else:
down = hang + pixels
if ((lie - pixels).any() <= 0):
left = 1
else:
left = lie - pixels
if ((lie + pixels).any() > s2):
right = s2
else:
right = lie + pixels
if type(up) == np.ndarray:
up = up.tolist()
else:
up = [up]
if type(down) == np.ndarray:
down = down.tolist()
else:
down = [down]
if type(left) == np.ndarray:
left = left.tolist()
else:
left = [left]
if type(right) == np.ndarray:
right = right.tolist()
else:
right = [right]
return up, down, left, right
def jpg_to_tiff( image_address, tiff_raw_out_address,):
image_address = str(base64.b64decode(image_address.encode("utf-8")), "utf-8")
tiff_raw_out_address = str(base64.b64decode(tiff_raw_out_address.encode("utf-8")), "utf-8")
image_address_split = image_address.split('/')
image_address_split_raw = image_address_split[-1][0:-3]
dji_exe = 'F:/GuanYong/zzb/guanyong_pro/rgbtotiff_exe/utility/bin/windows/release_x64/dji_irp.exe' +' '+'-s'+' '+str(image_address)+' '+'-o'+' '+tiff_raw_out_address+'/'+image_address_split_raw+'raw'+' '+'-a measure --measurefmt float32'
r_v = os.system(dji_exe)
img = np.fromfile(tiff_raw_out_address+'/'+image_address_split_raw+'raw', dtype='float32')
img = img.reshape(512,640)
img2 = img.T
tifffile.imsave(tiff_raw_out_address+ '/' + image_address_split_raw + 'tiff', img2)
for imagename in os.listdir(tiff_raw_out_address):
if imagename.endswith(".raw"):
os.remove(tiff_raw_out_address+ '/'+imagename)
tiff_img_name = image_address_split_raw + 'tiff'
return tiff_img_name
def peizhun( thermal_tiff_img_path, thermal_rgb_img_path, visible_img_path, output_path):
print('启动配准')
peizhun_exe = 'E:/ProgramData/peizhun/peizhun/TAMM_package_noparelle/main_TAMM.exe' +' '+'--thermal_tiff_path'+' '+str(thermal_tiff_img_path)+' '+'--thermal_rgb_path'+' '+thermal_rgb_img_path+' '+'--visible_path'+' '+visible_img_path+' '+'--output_path'+' '+output_path
os.system(peizhun_exe)
return output_path
def one_shibie(thermal_rgb_path, save_thermal_rgb_path , visible_rgb_path, save_visible_rgb_path , temperature_difference, temperature_threshold2, temperature_threshold):
thermal_rgb_path = str(base64.b64decode(thermal_rgb_path.encode("utf-8")), "utf-8")
save_thermal_rgb_path = str(base64.b64decode(save_thermal_rgb_path.encode("utf-8")), "utf-8")
visible_rgb_path = str(base64.b64decode(visible_rgb_path.encode("utf-8")), "utf-8")
save_visible_rgb_path = str(base64.b64decode(save_visible_rgb_path.encode("utf-8")), "utf-8")
start = time.clock()
time_list = {'dji_time':None, 'method_time':None, 'suanfa_time':None}
thermal_rgb_path2 = 'F:/GuanYong/zzb/zhongzhuan_files/zz.JPG'
copyfile(thermal_rgb_path, thermal_rgb_path2)
one_shibie_return = {'save_thermal_rgb_path': None,
'save_visible_rgb_path': None,
'shibie_result': None,
}
image_address_split = thermal_rgb_path.split('/')
image_address_split_raw = image_address_split[-1][0:-3]
thermal_image_rgb = cv2.imread(thermal_rgb_path2)
image_address_split2 = visible_rgb_path.split('/')
visable_image_rgb = cv2.imdecode(np.fromfile(visible_rgb_path, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
visable_image_rgb = cv2.resize(visable_image_rgb,(640, 512))
cv2.imwrite(save_visible_rgb_path + '/' + image_address_split2[-1][0:-4] + 'output.JPG', visable_image_rgb)
cv2.imwrite(save_thermal_rgb_path + '/' + image_address_split[-1][0:-4] + 'output.JPG',
thermal_image_rgb)
one_shibie_return['save_visible_rgb_path'] = save_visible_rgb_path + '/' + image_address_split2[-1][
0:-4] + 'output.JPG'
end1 = time.clock()
dji_exe = 'F:/GuanYong/zzb/guanyong_pro/rgbtotiff_exe/utility/bin/windows/release_x64/dji_irp.exe' + ' ' + '-s' + ' ' + str(
thermal_rgb_path2) + ' ' + '-o' + ' ' + save_thermal_rgb_path + '/' + image_address_split_raw + 'raw' + ' ' + '-a measure --measurefmt float32'
r_v = os.system(dji_exe)
try:
img_raw = np.fromfile(save_thermal_rgb_path + '/' + image_address_split_raw + 'raw', dtype='float32')
except:
return "image_address Error, con't find image"
img_raw2 = img_raw.reshape(512, 640)
img_raw2_t = img_raw2.T
temperature = img_raw2_t
os.remove(save_thermal_rgb_path + '/' + image_address_split_raw + 'raw')
end2 = time.clock()
time_list['dji_time'] = end2-end1
temperature = np.around(temperature, 4)
s1, s2 = temperature.shape
for i in range(s1):
for j in range(s2):
if temperature[i][j] < 0:
temperature[i][j] = None
diff1 = np.zeros((s1 - 1, s2 - 1))
diff2 = np.zeros((s1 - 1, s2 - 1))
LST = temperature
for j in range(s2 - 1):
for i in range(s1 - 1):
if (LST[i, j] != None) & (LST[i, j + 1] != None):
diff1[i, j] = abs(LST[i, j] - LST[i, j + 1])
if diff1[i, j] > 10:
print(diff2[i, j], '大小不同')
LST[i, j] = None
LST[i, j + 1] = None
diff2[i, j] = abs(LST[i, j] - LST[i, j + 1])
if diff2[i, j] > 10:
LST[i, j] = None
LST[i + 1, j] = None
temperature_min1 = np.nanmin(np.nanmin(LST))
temp = temperature.reshape([s1 * s2, 1])
index_h, index_l = np.where(np.isnan(temp) == True)
temp_dl = np.delete(temp, index_h)
temp = np.sort(temp_dl)
numtmp = len(temp)
temperature_min2 = temp[int(0.0001 * numtmp)]
temperature_min = (temperature_min1 + temperature_min2) / 2
zones = temperature < temperature_min + temperature_threshold
if (sum(sum(zones)) / s1 / s2 > 0.7):
one_shibie_return['shibie_result'] = 0
cv2.imwrite(save_thermal_rgb_path + '/' + image_address_split[-1][0:-5] + 'output.JPG',
thermal_image_rgb)
end = time.clock()
else:
one_shibie_return['shibie_result'] = 1
interval_h = int(s1 / 4)
interval_l = int(s2 / 4)
hang_begin = [1, interval_h, interval_h * 2, interval_h * 3]
hang_end = [interval_h, interval_h * 2, interval_h * 3, s1]
lie_begin = [1, interval_l, interval_l * 2, interval_l * 3]
lie_end = [interval_l, interval_l * 2, interval_l * 3, s2]
for i in range(4):
for j in range(4):
hang_index1 = hang_begin[i]
hang_index2 = hang_end[i]
lie_index1 = lie_begin[j]
lie_index2 = lie_end[j]
zone_tmp = zones[hang_index1:hang_index2, lie_index1: lie_index2]
if (sum(sum(zone_tmp)) == 0):
continue
temperature_zone = temperature[hang_index1:hang_index2, lie_index1: lie_index2]
minT = np.nanmin(np.nanmin(temperature_zone))
midhang, midlie = np.where(temperature_zone == minT)
size1, size2 = temperature_zone.shape
up_in, down_in, left_in, right_in, up_out, down_out, left_out, right_out = in_out_zone(midhang,
midlie,
size1,
size2)
if type(up_in) == np.ndarray:
up_in = up_in.tolist()
else:
up_in = [up_in]
if type(down_in) == np.ndarray:
down_in = down_in.tolist()
else:
down_in = [down_in]
if type(left_in) == np.ndarray:
left_in = left_in.tolist()
else:
left_in = [left_in]
if type(right_in) == np.ndarray:
right_in = right_in.tolist()
else:
right_in = [right_in]
if type(up_out) == np.ndarray:
up_out = up_out.tolist()
else:
up_out = [up_out]
if type(down_out) == np.ndarray:
down_out = down_out.tolist()
else:
down_out = [down_out]
if type(left_out) == np.ndarray:
left_out = left_out.tolist()
else:
left_out = [left_out]
if type(right_out) == np.ndarray:
right_out = right_out.tolist()
else:
right_out = [right_out]
temperature_inside = np.nanmean(
np.nanmean(temperature_zone[up_in[0]:down_in[0], left_in[0]:right_in[0]]))
temperature_outside = np.nanmean(
np.nanmean(temperature[abs(up_out[0]):down_out[0], abs(left_out[0]):right_out[0]]))
if (temperature_outside - temperature_inside < temperature_difference):
zones[hang_index1: hang_index2, lie_index1: lie_index2] = 0
continue
else:
zones[hang_index1: hang_index2, lie_index1: lie_index2] = 0
up, down, left, right = flags(hang_index1 + midhang, lie_index1 + midlie, 20, s1, s2)
zones[up[0]: down[0], left[0]: right[0]] = 1
tmp2 = zones * temperature
ww, hh = np.where((tmp2 < (temperature_min + temperature_threshold2)) & (tmp2 != 0))
tmp2[ww, hh] = 255
ww1, hh1 = np.where(tmp2 != 255)
tmp2[ww1, hh1] = 0
end4 = time.clock()
time_list['suanfa_time'] = end4-end2
if (sum(sum(tmp2)) == 0):
one_shibie_return['shibie_result'] = 0
cv2.imwrite(save_thermal_rgb_path + '/' + image_address_split[-1][0:-5] + 'output.JPG',
thermal_image_rgb)
else:
img2 = np.zeros([512, 640, 3], dtype=np.uint8)
img2[:, :, 0] = 0
img2[:, :, 1] = 255
img2[:, :, 2] = 255
img_ROI2 = cv2.addWeighted(thermal_image_rgb, 1, img2, 0.3, 0)
img_ROI2[hh, ww] = [0, 0, 255]
cv2.imwrite(save_thermal_rgb_path + '/' + image_address_split[-1][0:-5] + 'output.JPG', img_ROI2)
one_shibie_return['save_thermal_rgb_path'] = save_thermal_rgb_path + '/' + image_address_split[-1][
0:-5] + 'output.JPG'
end3 = time.clock()
time_list['method_time'] = end3-start
print(time_list)
return one_shibie_return
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/jpg_to_tiff")
def read_item( image_address:str, tiff_raw_out_address:str):
tiff_img_name = jpg_to_tiff( image_address, tiff_raw_out_address)
return {'tiff_img_name':tiff_img_name}
@app.get("/peizhun")
def read_item( thermal_tiff_img_path:str, thermal_rgb_img_path:str, visible_img_path:str, output_path:str):
peizhun_ok = peizhun( thermal_tiff_img_path, thermal_rgb_img_path, visible_img_path, output_path)
return output_path
temperature_difference = 1.5
temperature_threshold2 =4
temperature_threshold = 3
@app.get("/one_shibie")
def read_item(thermal_rgb_path:str, save_thermal_rgb_path:str, visible_rgb_path:str, save_visible_rgb_path:str):
one_shibie_return = one_shibie(thermal_rgb_path, save_thermal_rgb_path, visible_rgb_path, save_visible_rgb_path, temperature_difference, temperature_threshold2, temperature_threshold)
return one_shibie_return
def start_server():
if platform.system() == "Windows":
kernel32 = ctypes.windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-10), 128)
uvicorn.run(f"{Path(__file__).stem}:app", host="0.0.0.0", port=8000)
if __name__ == "__main__":
start_server()

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
- 444
- 445
- 446
- 447
- 448
- 449
- 450
- 451
- 452
- 453
- 454
- 455
- 456
- 457
- 458
- 459
- 460
- 461
- 462
- 463
- 464
- 465
- 466
- 467
- 468
- 469
- 470
- 471
- 472
- 473
- 474
- 475
- 476
- 477
- 478
- 479
- 480
- 481
- 482
- 483
- 484
- 485
- 486
- 487
- 488
- 489
- 490
- 491
- 492
- 493
- 494
- 495
- 496
- 497
- 498
- 499
- 500
- 501
- 502
- 503
- 504
- 505
- 506
- 507
- 508
- 509
- 510
- 511
- 512
- 513
- 514
- 515
- 516
- 517
- 518
- 519
- 520
- 521
- 522
- 523
- 524
- 525
- 526
- 527
- 528
- 529
- 530
- 531
- 532
- 533
- 534
- 535
- 536
- 537
- 538
- 539
- 540
- 541
- 542
- 543
- 544
- 545
- 546
- 547
- 548
- 549
- 550
- 551
- 552
- 553
- 554
- 555
- 556
- 557
- 558
- 559
- 560
- 561
- 562
- 563
- 564
- 565