import os
import json
import time
from django.conf import settings
from osgeo import gdal
from django.http import JsonResponse, response, HttpResponse, StreamingHttpResponse
def tifWA(request):
result = []
if request.method == 'POST':
print("函数已触发")
tif = request.FILES.get('file')
print(tif)
base_dir = settings.BASE_DIR
timestrap = str(time.time())
upload_dir = os.path.join(base_dir, 'media', 'Tifs')
print(upload_dir)
path = os.path.join(upload_dir, tif.name)
print(path)
with open(path, 'wb+') as f:
for chunk in tif.chunks():
f.write(chunk)
tifPath = gdal.Open(path)
CutTif(tifPath,timestrap)
print('切割完毕')
path = os.path.join(base_dir, 'media', 'CutAfter', timestrap)
tiftest(path,timestrap)
return JsonResponse(result)
from PIL import Image
def CutTif(in_ds,timestrap):
in_band1 = in_ds.GetRasterBand(1)
in_band2 = in_ds.GetRasterBand(2)
in_band3 = in_ds.GetRasterBand(3)
ori_transform = in_ds.GetGeoTransform()
top_left_x = ori_transform[0]
top_left_y = ori_transform[3]
w_e_pixel_resolution = ori_transform[1]
n_s_pixel_resolution = ori_transform[5]
offset_x = 0
offset_y = 0
block_xsize = 600
block_ysize = 600
count = 0
im_width = in_ds.RasterXSize
im_height = in_ds.RasterYSize
num_width = int(im_width / block_ysize)
num_height = int(im_height / block_xsize)
if True:
if im_width % block_ysize == 0:
num_width = num_width
wb = False
else:
num_width += 1
wb = True
if im_height % block_xsize == 0:
num_height = num_height
hb = False
else:
num_height += 1
hb = True
for i in range(num_width):
offset_x1 = offset_x + block_xsize * i
if hb and i == num_width - 1:
offset_x1 = im_width - block_xsize
for j in range(num_height):
count = count + 1
offset_y1 = offset_y + block_ysize * j
if wb and j == num_height - 1:
offset_y1 = im_height - block_ysize
out_band1 = in_band1.ReadAsArray(offset_x1, offset_y1, block_xsize, block_ysize)
out_band2 = in_band2.ReadAsArray(offset_x1, offset_y1, block_xsize, block_ysize)
out_band3 = in_band3.ReadAsArray(offset_x1, offset_y1, block_xsize, block_ysize)
gtif_driver = gdal.GetDriverByName("GTiff")
base_dir = settings.BASE_DIR
path = os.path.join(base_dir, 'media', 'CutAfter',timestrap)
if not os.path.exists(path):
os.makedirs(path)
filename = 'image' + str(count) + '.png'
allpath = path + '\\' + filename
out_ds = gtif_driver.Create(allpath, block_xsize, block_ysize, 3, in_band1.DataType)
print("create new tif file succeed")
top_left_x1 = top_left_x + offset_x1 * w_e_pixel_resolution
top_left_y1 = top_left_y + offset_y1 * n_s_pixel_resolution
dst_transform = (
top_left_x1, ori_transform[1], ori_transform[2], top_left_y1, ori_transform[4], ori_transform[5])
out_ds.SetGeoTransform(dst_transform)
out_ds.SetProjection(in_ds.GetProjection())
out_ds.GetRasterBand(1).WriteArray(out_band1)
out_ds.GetRasterBand(2).WriteArray(out_band2)
out_ds.GetRasterBand(3).WriteArray(out_band3)
out_ds.FlushCache()
print(f"FlushCache succeed{count}")
del out_ds
import argparse
from detect import main, parse_opt
def tiftest(path,exp):
print('检测函数被触发')
parser = argparse.ArgumentParser()
parser.add_argument('--weights', nargs='+', type=str,
default=r'D:\Desktop\windmillSystem\dvadmin-backend\runs\train\1663417314.2989476\weights\best.pt', help='model.pt path(s)')
parser.add_argument('--source', type=str, default=path, help='file/dir/URL/glob, 0 for webcam')
parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=640, help='inference size (pixels)')
parser.add_argument('--conf-thres', type=float, default=0.25, help='confidence threshold')
parser.add_argument('--iou-thres', type=float, default=0.45, help='NMS IoU threshold')
parser.add_argument('--max-det', type=int, default=1000, help='maximum detections per image')
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
parser.add_argument('--view-img', action='store_true', help='show results')
parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
parser.add_argument('--save-crop', action='store_true', help='save cropped prediction boxes')
parser.add_argument('--nosave', action='store_true', help='do not save images/videos')
parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --class 0, or --class 0 2 3')
parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
parser.add_argument('--augment', action='store_true', help='augmented inference')
parser.add_argument('--visualize', action='store_true', help='visualize features')
parser.add_argument('--update', action='store_true', help='update all models')
parser.add_argument('--project', default='media/DetectAfter', help='save results to project/name')
parser.add_argument('--name', default=exp, help='save results to project/name')
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
parser.add_argument('--line-thickness', default=3, type=int, help='bounding box thickness (pixels)')
parser.add_argument('--hide-labels', default=False, action='store_true', help='hide labels')
parser.add_argument('--hide-conf', default=False, action='store_true', help='hide confidences')
parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
opt = parser.parse_args(args=[])
print(opt)
time_cost = main(opt)
return time_cost