Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 90 additions & 20 deletions crop/stereo.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def bin_to_png(in_dirs, out_dirs, plot_dir, convt):
fail("\tFailed to process folder %s: %s" % (i, str(ex)))

def fail(reason):
print(reason)
print (reason)

def lower_keys(in_dict):
if type(in_dict) is dict:
Expand Down Expand Up @@ -539,6 +539,12 @@ def stitch_plot_rgb_image(in_dir, out_dir, str_date, convt):
img_hts = int(round((plot_bounds[1]-plot_bounds[0])/field_dist_per_pix))+2000
stitched_img = np.zeros((img_hts,img_wids,3),np.uint8)

rect_x_min = img_wids
rect_y_min = img_hts
rect_x_max = 0
rect_y_max = 0


start_offset = 500
for json_file in metas:
json_path = os.path.join(in_dir, json_file)
Expand All @@ -557,23 +563,34 @@ def stitch_plot_rgb_image(in_dir, out_dir, str_date, convt):
print(1)
continue

if x_start < rect_x_min :
rect_x_min = x_start
if y_start < rect_y_min:
rect_y_min = y_start
if x_start+width > rect_x_max:
rect_x_max = x_start+width
if y_start+height > rect_y_max:
rect_y_max = y_start+height

stitched_img[y_start:y_start+height, x_start:x_start+width] = img

save_img = stitched_img[rect_y_min:rect_y_max, rect_x_min:rect_x_max]

# save output
out_file_name = '{}_{}.png'.format(str_date, dir_name)
cv2.imwrite(os.path.join(out_dir, out_file_name), stitched_img)
cv2.imwrite(os.path.join(out_dir, out_file_name), save_img)

return

def test():

str_date = '2017-05-24'
str_date = '2019-06-18'

in_dir = os.path.join('/media/zli/Seagate Backup Plus Drive/OPEN/ua-mac/Level_2/stereoTop/', str_date)
out_dir = os.path.join('/media/zli/Seagate Backup Plus Drive/OPEN/ua-mac/Level_2/StitchedPlotRGB', str_date)

convt = terra_common.CoordinateConverter()
qFlag = convt.bety_query(str_date, False)
qFlag = convt.bety_query(str_date)

if not os.path.isdir(out_dir):
os.makedirs(out_dir)
Expand Down Expand Up @@ -601,24 +618,21 @@ def test_single_dir():
str_date = '2018-05-24'

convt = terra_common.CoordinateConverter()
qFlag = convt.bety_query(str_date, False)
qFlag = convt.bety_query(str_date)

stitch_plot_rgb_image(in_dir, out_dir, str_date, convt)

stitch_plot_rgb_image('/media/zli/Elements/ua-mac/Level_2/RgbCropToPlot/2017-05-05/41-09-649', '/media/zli/Elements/ua-mac/Level_2/StitchedPlotRGB/2017-05-05/41-09-649', str_date, convt)

return

def full_season_crop_rgb(raw_rgb_dir, out_dir, plot_dir, start_date, end_date):
def full_season_crop_rgb(raw_rgb_dir, out_dir, plot_dir, start_date, end_date, convt):

# initialize data structure
d0 = datetime.strptime(start_date, '%Y-%m-%d').date()
d1 = datetime.strptime(end_date, '%Y-%m-%d').date()
deltaDay = d1 - d0

convt = terra_common.CoordinateConverter()
qFlag = convt.bety_query(start_date, False) # All plot boundaries in one season is the same

# loop one season directories
for i in range(deltaDay.days+1):
str_date = str(d0+timedelta(days=i))
Expand All @@ -628,25 +642,81 @@ def full_season_crop_rgb(raw_rgb_dir, out_dir, plot_dir, start_date, end_date):

out_path = os.path.join(out_dir, str_date)

plot_path = os.path.join(plot_dir, str_date)

if not os.path.isdir(raw_path):
continue

#crop_rgb_imageToPlot(raw_path, out_path, plot_dir, convt)
full_day_multi_process(raw_path, out_path, plot_dir, convt)
if not os.path.isdir(out_path):
os.mkdir(out_path)

if not os.path.isdir(plot_path):
os.mkdir(plot_path)

crop_rgb_imageToPlot(raw_path, out_path, plot_path, convt)
#full_day_multi_process(raw_path, out_path, plot_dir, convt)

return

def main():
start_date = '2017-04-15' # S6 start date
end_date = '2017-08-31' # S6 end date
def full_season_rgb_stitch(png_dir, stitch_dir, start_date, end_date, convt):

# initialize data structure
d0 = datetime.strptime(start_date, '%Y-%m-%d').date()
d1 = datetime.strptime(end_date, '%Y-%m-%d').date()
deltaDay = d1 - d0

print(deltaDay.days)

# loop one season directories
for i in range(deltaDay.days+1):
str_date = str(d0+timedelta(days=i))
print(str_date)

png_path = os.path.join(png_dir, str_date)

stitch_path = os.path.join(stitch_dir, str_date)

if not os.path.isdir(png_path):
continue

if not os.path.isdir(stitch_path):
os.makedirs(stitch_path)

list_dirs = os.listdir(png_path)

args = options()
for d in list_dirs:
in_path = os.path.join(png_path, d)
out_path = os.path.join(stitch_path, d)

if not os.path.isdir(in_path):
continue

if not os.path.isdir(out_path):
os.mkdir(out_path)

stitch_plot_rgb_image(in_path, out_path, str_date, convt)

full_season_crop_rgb('/media/zli/Seagate Backup Plus Drive/OPEN/ua-mac/raw_data/stereoTop',
'/media/zli/Seagate Backup Plus Drive/OPEN/ua-mac/Level_1/stereoTop',
'/media/zli/Seagate Backup Plus Drive/OPEN/ua-mac/Level_2/stereoTop', start_date, end_date)
#full_season_crop_rgb(args.in_dir, args.out_dir, args.plot_dir, start_date, end_date)
return

def main():
start_date = '2018-06-02' # S9 start date
end_date = '2018-06-05' # S9 end date

convt = terra_common.CoordinateConverter()
qFlag = convt.bety_query('2019-06-18') # All plot boundaries in one season should be the same, currently 2019-06-18 works best

if not qFlag:
return
#args = options()

raw_dir = '/media/zli/Elements/ua-mac/raw_data/stereoTop/'
out_dir = '/media/zli/Seagate Backup Plus Drive/OPEN/ua-mac/Level_1/stereoTop'
plot_dir = '/media/zli/Seagate Backup Plus Drive/OPEN/ua-mac/Level_2/rgb_crop'
stitch_dir = '/media/zli/Seagate Backup Plus Drive/OPEN/ua-mac/Level_2/StitchedPlotRGB'

#full_season_crop_rgb(raw_dir, out_dir, plot_dir, start_date, end_date, convt)

full_season_rgb_stitch(plot_dir, stitch_dir, start_date, end_date, convt)


if __name__ == '__main__':
Expand All @@ -661,4 +731,4 @@ def main():





136 changes: 125 additions & 11 deletions crop/thermalCropping.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@author: zli
'''

import sys, os.path, json, random, terra_common, math
import sys, os.path, json, random, terra_common, math, argparse
import matplotlib.pyplot as plt
from matplotlib import cm
from glob import glob
Expand All @@ -13,7 +13,7 @@
from numpy.matlib import repmat
from scipy.ndimage.filters import convolve
from PIL import Image
from datetime import date, timedelta
from datetime import date, timedelta, datetime
import shutil
import cv2

Expand Down Expand Up @@ -424,7 +424,7 @@ def find_input_files(in_dir):


def fail(reason):
print >> sys.stderr, reason
print (sys.stderr, reason)

def extract_roiBox_from_metadata(metadata):

Expand Down Expand Up @@ -466,6 +466,11 @@ def stitch_plot_thermal_image(in_dir, out_dir, str_date, convt):
img_hts = int(round((plot_bounds[1]-plot_bounds[0])/field_dist_per_pix))+800
stitched_img = np.zeros((img_hts,img_wids,3),np.uint8)

rect_x_min = img_wids
rect_y_min = img_hts
rect_x_max = 0
rect_y_max = 0

start_offset = 500
for json_file in metas:
json_path = os.path.join(in_dir, json_file)
Expand All @@ -484,22 +489,135 @@ def stitch_plot_thermal_image(in_dir, out_dir, str_date, convt):
print(1)
continue

if x_start < rect_x_min :
rect_x_min = x_start
if y_start < rect_y_min:
rect_y_min = y_start
if x_start+width > rect_x_max:
rect_x_max = x_start+width
if y_start+height > rect_y_max:
rect_y_max = y_start+height

stitched_img[y_start:y_start+height, x_start:x_start+width] = img

save_img = stitched_img[rect_y_min:rect_y_max, rect_x_min:rect_x_max]
# save output
out_file_name = '{}_{}.png'.format(str_date, dir_name)
cv2.imwrite(os.path.join(out_dir, out_file_name), stitched_img)
cv2.imwrite(os.path.join(out_dir, out_file_name), save_img)

return

def full_season_thermalCrop_frame(in_dir, out_dir, plot_dir, png_dir, start_date, end_date, convt):

def main(convt, str_date):
# initialize data structure
d0 = datetime.strptime(start_date, '%Y-%m-%d').date()
d1 = datetime.strptime(end_date, '%Y-%m-%d').date()
deltaDay = d1 - d0

print(deltaDay.days)

# loop one season directories
for i in range(deltaDay.days+1):
str_date = str(d0+timedelta(days=i))
print(str_date)

raw_path = os.path.join(in_dir, str_date)

out_path = os.path.join(out_dir, str_date)

plot_path = os.path.join(plot_dir, str_date)

png_path = os.path.join(png_dir, str_date)

if not os.path.isdir(raw_path):
continue

if not os.path.isdir(out_path):
os.makedirs(out_path)

if not os.path.isdir(plot_path):
os.makedirs(plot_path)

if not os.path.isdir(png_path):
os.makedirs(png_path)

#crop_rgb_imageToPlot(raw_path, out_path, plot_dir, convt)
crop_thermal_imageToPlot(raw_path, out_path, plot_path, png_path, convt)
#full_day_gen_cc(raw_path, out_path, convt)

return



def full_season_thermal_stitch(png_dir, stitch_dir, start_date, end_date, convt):

# initialize data structure
d0 = datetime.strptime(start_date, '%Y-%m-%d').date()
d1 = datetime.strptime(end_date, '%Y-%m-%d').date()
deltaDay = d1 - d0

print(deltaDay.days)

# loop one season directories
for i in range(deltaDay.days+1):
str_date = str(d0+timedelta(days=i))
print(str_date)

png_path = os.path.join(png_dir, str_date)

stitch_path = os.path.join(stitch_dir, str_date)

if not os.path.isdir(png_path):
continue

if not os.path.isdir(stitch_path):
os.makedirs(stitch_path)

list_dirs = os.listdir(png_path)

for d in list_dirs:
in_path = os.path.join(png_path, d)
out_path = os.path.join(stitch_path, d)

if not os.path.isdir(in_path):
continue

if not os.path.isdir(out_path):
os.mkdir(out_path)

stitch_plot_thermal_image(in_path, out_path, str_date, convt)

return

def main():

'''
in_dir = os.path.join('/media/zli/Seagate Backup Plus Drive/OPEN/ua-mac/raw_data/flirlrCamera', str_date)
out_dir = os.path.join('/media/zli/Seagate Backup Plus Drive/OPEN/ua-mac/Level_1/thermalData', str_date)
plot_dir = os.path.join('/media/zli/Seagate Backup Plus Drive/OPEN/ua-mac/Level_2/thermalCropToPlot/', str_date)
png_dir = os.path.join('/media/zli/Seagate Backup Plus Drive/OPEN/ua-mac/Level_2/thermalCropToPlot_png/', str_date)
crop_thermal_imageToPlot(in_dir, out_dir, plot_dir, png_dir, convt)
'''
print("start...")

start_date = '2018-06-02'
end_date = '2018-06-08'

convt = terra_common.CoordinateConverter()
qFlag = convt.bety_query('2019-06-18') # All plot boundaries in one season should be the same, currently 2019-06-18 works best

if not qFlag:
return

in_dir = os.path.join('/media/zli/Elements/ua-mac/raw_data/flirlrCamera')
out_dir = os.path.join('/media/zli/Seagate Backup Plus Drive/OPEN/ua-mac/Level_1/thermalData')
plot_dir = os.path.join('/media/zli/Seagate Backup Plus Drive/OPEN/ua-mac/Level_2/thermalCropToPlot/')
png_dir = os.path.join('/media/zli/Seagate Backup Plus Drive/OPEN/ua-mac/Level_2/thermalCropToPlot_png/')
stitch_dir = os.path.join('/media/zli/Seagate Backup Plus Drive/OPEN/ua-mac/Level_2/StitchedPlotThermal')

#full_season_thermalCrop_frame(in_dir, out_dir, plot_dir, png_dir, start_date, end_date, convt)

full_season_thermal_stitch(png_dir, stitch_dir, start_date, end_date, convt)

return

Expand Down Expand Up @@ -534,12 +652,8 @@ def test(convt, str_date):

if __name__ == '__main__':

str_date = '2018-05-24'
convt = terra_common.CoordinateConverter()
convt.bety_query(str_date, False)

#main(convt, str_date)
test(convt, str_date)
main()
#test(convt, str_date)



Expand Down