Skip to content

Commit d79eddf

Browse files
committed
Switching over to new filenames
1 parent ee646cc commit d79eddf

15 files changed

+433
-131
lines changed

.DS_Store

6 KB
Binary file not shown.

Image_based/.DS_Store

6 KB
Binary file not shown.

Image_based/BaseStatsImage.py

+44-13
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
from os.path import exists
1414
import json
1515
from line_seg_2d import LineSeg2D
16-
from HandleFileNames import HandleFileNames
16+
17+
from os.path import abspath as os_abs_path
18+
from sys import path as syspath
19+
syspath.insert(0, os_abs_path('../Utilities'))
20+
syspath.insert(0, os_abs_path('./Utilities'))
21+
from FileNames import FileNames
1722

1823

1924
class BaseStatsImage:
@@ -47,7 +52,12 @@ def __init__(self, fname_mask_image, fname_calculated=None, fname_debug=None, b_
4752
@param b_recalc: Force recalculate the result, y/n"""
4853

4954
self.stats_dict = None
50-
mask_image_rgb = cv2.imread(fname_mask_image)
55+
if exists(fname_mask_image):
56+
mask_image_rgb = cv2.imread(fname_mask_image)
57+
else:
58+
self.mask_image = np.zeros((8, 8), dtype=np.uint8)
59+
return
60+
5161
if len(mask_image_rgb.shape) == 3:
5262
self.mask_image = cv2.cvtColor(mask_image_rgb, cv2.COLOR_BGR2GRAY)
5363
else:
@@ -179,22 +189,43 @@ def debug_image(self, in_image):
179189
ymax = self.stats_dict["y_max"]
180190
LineSeg2D.draw_rect(in_image, [[xmin, xmax], [ymin, ymax]], (256, 128, 128), 2)
181191

192+
@staticmethod
193+
def create_from_filenames(filenames, index=(0,0,0,0), b_do_recalc=False, b_do_debug=True):
194+
""" Create a base image from a file name in FileNames
195+
@param filenames - FileNames instance
196+
@param index tuple (eg (0,0,0,0))
197+
@param b_do_recalc - recalculate from scratch
198+
@param b_do_debug - spit out a debug image y/n
199+
@return base stats image"""
200+
201+
# File name
202+
mask_fname = filenames.get_mask_name(index=index, b_add_tag=True)
203+
# Debug image file name
204+
if b_do_debug:
205+
mask_fname_debug = filenames.get_mask_name(index=index, b_debug_path=True, b_add_tag=False)
206+
else:
207+
mask_fname_debug = None
208+
209+
# The stub of the filename to save all of the data to
210+
mask_fname_calculate = filenames.get_mask_name(index=index, b_calculate_path=True, b_add_tag=False)
211+
212+
if not exists(mask_fname):
213+
print(f"Warning, file {mask_fname} does not exist")
214+
b_stats = BaseStatsImage(mask_fname, mask_fname_calculate, mask_fname_debug, b_recalc=b_do_recalc)
215+
return b_stats
216+
182217

183218
if __name__ == '__main__':
219+
path_bpd_envy = "/Users/cindygrimm/VSCode/treefitting/Image_based/data/EnvyTree/"
220+
all_fnames_envy = FileNames.read_filenames(path=path_bpd_envy,
221+
fname="envy_fnames.json")
222+
BaseStatsImage.create_from_filenames(all_fnames_envy, (0, 0, 0, 0), b_do_recalc=False, b_do_debug=False)
223+
184224
#path_bpd = "./data/trunk_segmentation_names.json"
185225
path_bpd = "./data/forcindy_fnames.json"
186-
all_files = HandleFileNames.read_filenames(path_bpd)
226+
all_files = FileNames.read_filenames(path_bpd)
187227

188228
b_do_debug = True
189229
b_do_recalc = False
190230
for ind in all_files.loop_masks():
191-
mask_fname = all_files.get_mask_name(path=all_files.path, index=ind, b_add_tag=True)
192-
mask_fname_debug = all_files.get_mask_name(path=all_files.path_debug, index=ind, b_add_tag=False)
193-
if not b_do_debug:
194-
mask_fname_debug = None
195-
196-
mask_fname_calculate = all_files.get_mask_name(path=all_files.path_calculated, index=ind, b_add_tag=False)
197-
198-
if not exists(mask_fname):
199-
raise ValueError(f"Error, file {mask_fname} does not exist")
200-
b_stats = BaseStatsImage(mask_fname, mask_fname_calculate, mask_fname_debug, b_recalc=b_do_recalc)
231+
BaseStatsImage.create_from_filenames(all_files, ind, b_do_debug=b_do_debug, b_do_recalc=b_do_recalc)

Image_based/data/EnvyTree

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../PycharmProjects/treefitting/Image_based/data/EnvyTree

Image_based/extract_curves.py

+45-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import json
1313
from os.path import exists
1414
from line_seg_2d import LineSeg2D
15-
from HandleFileNames import HandleFileNames
1615
from fit_bezier_cyl_2d_edge import FitBezierCyl2DEdge
16+
from FileNames import FileNames
1717

1818

1919
class ExtractCurves:
@@ -38,6 +38,10 @@ def __init__(self, fname_rgb_image, fname_edge_image, fname_mask_image, params=N
3838
params=params,
3939
fname_debug=fname_debug,
4040
b_recalc=b_recalc)
41+
42+
if not exists(fname_mask_image):
43+
self.params = None
44+
return
4145

4246
# List o pairs (t, plus/minus)
4347
self.left_curve = []
@@ -279,11 +283,50 @@ def curves_from_stats(stats_edge, params):
279283
# Left curve as t, perc pairs and same for right
280284
return crvs[0], crvs[1]
281285

286+
@staticmethod
287+
def create_from_filenames(filenames, index=(0,0,0,0), b_do_recalc=False, b_do_debug=True, b_use_optical_flow_edge=False):
288+
""" Create a base image from a file name in FileNames
289+
@param filenames - FileNames instance
290+
@param index tuple (eg (0,0,0,0))
291+
@param b_do_recalc - recalculate from scratch
292+
@param b_do_debug - spit out a debug image y/n
293+
@param b_use_optical_flow_edge - use optical flow edge image instead of rgb edge image
294+
@return extract curves"""
295+
296+
rgb_fname = filenames.get_image_name(index=index, b_add_tag=True)
297+
298+
if not exists(rgb_fname):
299+
raise ValueError(f"No file {rgb_fname}")
300+
301+
# File name
302+
mask_fname = filenames.get_mask_name(index=index, b_add_tag=True)
303+
edge_fname = filenames.get_edge_name(index=index, b_optical_flow=b_use_optical_flow_edge, b_add_tag=True)
304+
# Debug image file name
305+
if b_do_debug:
306+
mask_fname_debug = filenames.get_mask_name(index=index, b_debug_path=True, b_add_tag=False)
307+
else:
308+
mask_fname_debug = None
309+
310+
# The stub of the filename to save all of the data to
311+
mask_fname_calculate = filenames.get_mask_name(index=index, b_calculate_path=True, b_add_tag=False)
312+
313+
if not exists(mask_fname):
314+
print(f"Warning, file {mask_fname} does not exist")
315+
profile_crvs = ExtractCurves(rgb_fname, edge_fname, mask_fname,
316+
fname_calculated=mask_fname_calculate,
317+
fname_debug=mask_fname_debug, b_recalc=b_do_recalc)
318+
return profile_crvs
319+
282320

283321
if __name__ == '__main__':
322+
path_bpd_envy = "/Users/cindygrimm/VSCode/treefitting/Image_based/data/EnvyTree/"
323+
all_fnames_envy = FileNames.read_filenames(path=path_bpd_envy,
324+
fname="envy_fnames.json")
325+
ExtractCurves.create_from_filenames(all_fnames_envy, (0, 0, 0, 0), b_do_recalc=False, b_do_debug=False)
326+
284327
# path_bpd = "./data/trunk_segmentation_names.json"
285328
path_bpd = "./data/forcindy_fnames.json"
286-
all_files = HandleFileNames.read_filenames(path_bpd)
329+
all_files = FileNames.read_filenames(path_bpd)
287330

288331
b_do_debug = True
289332
b_do_recalc = True

Image_based/fit_bezier_cyl_2d_edge.py

+49-17
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
from bezier_cyl_2d import BezierCyl2D
2727
from fit_bezier_cyl_2d import FitBezierCyl2D
2828
from line_seg_2d import LineSeg2D
29-
from HandleFileNames import HandleFileNames
3029
from fit_bezier_cyl_2d_mask import FitBezierCyl2DMask
30+
from FileNames import FileNames
3131

3232

3333
class FitBezierCyl2DEdge:
@@ -84,6 +84,10 @@ def __init__(self, fname_rgb_image, fname_edge_image, fname_mask_image, fname_ca
8484
# This is the curve that will be fit to the edge
8585
self.bezier_crv_fit_to_edge = None
8686

87+
if not exists(fname_mask_image):
88+
self.params = None
89+
return
90+
8791
# Copy params used in fit mask and add the new ones
8892
self.params = {}
8993
for k in self.mask_crv.params.keys():
@@ -424,28 +428,56 @@ def debug_image_edge_fit(self, image_debug):
424428
else:
425429
self.bezier_crv_fit_to_edge.draw_boundary(image_debug, 10)
426430

431+
@staticmethod
432+
def create_from_filenames(filenames, index=(0,0,0,0), b_do_recalc=False, b_do_debug=True, b_use_optical_flow_edge=False):
433+
""" Create a base image from a file name in FileNames
434+
@param filenames - FileNames instance
435+
@param index tuple (eg (0,0,0,0))
436+
@param b_do_recalc - recalculate from scratch
437+
@param b_do_debug - spit out a debug image y/n
438+
@param b_use_optical_flow_edge - use optical flow edge image instead of rgb edge image
439+
@return FitBezierCyl2DEdge"""
440+
441+
rgb_fname = filenames.get_image_name(index=index, b_add_tag=True)
442+
443+
if not exists(rgb_fname):
444+
raise ValueError(f"No file {rgb_fname}")
445+
446+
# File name
447+
mask_fname = filenames.get_mask_name(index=index, b_add_tag=True)
448+
# Debug image file name
449+
if b_do_debug:
450+
edge_fname_debug = filenames.get_mask_name(index=index, b_debug_path=True, b_add_tag=False)
451+
else:
452+
edge_fname_debug = None
453+
454+
edge_fname = filenames.get_edge_name(index=index, b_add_tag=True, b_optical_flow=b_use_optical_flow_edge)
455+
456+
# The stub of the filename to save all of the data to
457+
edge_fname_calculate = filenames.get_mask_name(index=index, b_calculate_path=True, b_add_tag=False)
458+
459+
if not exists(mask_fname):
460+
print(f"Warning, file {mask_fname} does not exist")
461+
edge_crv = FitBezierCyl2DEdge(rgb_fname, edge_fname, mask_fname, edge_fname_calculate, edge_fname_debug, b_recalc=b_do_recalc)
462+
return edge_crv
463+
427464

428465
if __name__ == '__main__':
466+
path_bpd_envy = "/Users/cindygrimm/VSCode/treefitting/Image_based/data/EnvyTree/"
467+
all_fnames_envy = FileNames.read_filenames(path=path_bpd_envy,
468+
fname="envy_fnames.json")
469+
FitBezierCyl2DEdge.create_from_filenames(all_fnames_envy, (0, 0, 0, 0), b_do_recalc=False, b_do_debug=False)
470+
429471
# path_bpd = "./data/trunk_segmentation_names.json"
430472
path_bpd = "./data/forcindy_fnames.json"
431-
all_files = HandleFileNames.read_filenames(path_bpd)
473+
all_files = FileNames.read_filenames(path_bpd)
432474

433475
b_do_debug = True
434476
b_do_recalc = False
477+
b_use_optical_flow = False
435478
for ind in all_files.loop_masks():
436-
rgb_fname = all_files.get_image_name(path=all_files.path, index=ind, b_add_tag=True)
437-
edge_fname = all_files.get_edge_image_name(path=all_files.path_calculated, index=ind, b_add_tag=True)
438-
mask_fname = all_files.get_mask_name(path=all_files.path, index=ind, b_add_tag=True)
439-
edge_fname_debug = all_files.get_mask_name(path=all_files.path_debug, index=ind, b_add_tag=False)
440-
if not b_do_debug:
441-
edge_fname_debug = None
442-
443-
edge_fname_calculate = all_files.get_mask_name(path=all_files.path_calculated, index=ind, b_add_tag=False)
444-
445-
if not exists(mask_fname):
446-
raise ValueError(f"Error, file {mask_fname} does not exist")
447-
if not exists(rgb_fname):
448-
raise ValueError(f"Error, file {rgb_fname} does not exist")
449-
450-
edge_crv = FitBezierCyl2DEdge(rgb_fname, edge_fname, mask_fname, edge_fname_calculate, edge_fname_debug, b_recalc=b_do_recalc)
479+
FitBezierCyl2DEdge.create_from_filenames(all_files,
480+
b_do_debug=b_do_debug,
481+
b_do_recalc=b_do_recalc,
482+
b_use_optical_flow_edge=b_use_optical_flow)
451483
print("foo")

Image_based/fit_bezier_cyl_2d_mask.py

+37-14
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from bezier_cyl_2d import BezierCyl2D
1818
from fit_bezier_cyl_2d import FitBezierCyl2D
1919
from BaseStatsImage import BaseStatsImage
20-
from HandleFileNames import HandleFileNames
20+
from FileNames import FileNames
2121

2222

2323
class FitBezierCyl2DMask:
@@ -34,6 +34,10 @@ def __init__(self, fname_mask_image, fname_calculated=None, fname_debug=None, pa
3434
# First do the stats - this also reads the image in
3535
self.stats_dict = BaseStatsImage(fname_mask_image, fname_calculated, fname_debug, b_recalc)
3636

37+
if not exists(fname_mask_image):
38+
self.params = None
39+
return
40+
3741
# Now initialize bezier curve with info from stats - no need to cache this because it's so light-weight
3842
p0 = self.stats_dict.stats_dict["lower_left"]
3943
p2 = self.stats_dict.stats_dict["upper_right"]
@@ -202,24 +206,43 @@ def score_mask_fit(self, im_mask):
202206

203207
return np.count_nonzero(pixs_in_both_masks) / np.count_nonzero(pixs_in_union)
204208

209+
@staticmethod
210+
def create_from_filenames(filenames, index=(0,0,0,0), b_do_recalc=False, b_do_debug=True):
211+
""" Create a base image from a file name in FileNames
212+
@param filenames - FileNames instance
213+
@param index tuple (eg (0,0,0,0))
214+
@param b_do_recalc - recalculate from scratch
215+
@param b_do_debug - spit out a debug image y/n
216+
@return BezierCyl2DMask"""
217+
218+
# File name
219+
mask_fname = filenames.get_mask_name(index=index, b_add_tag=True)
220+
# Debug image file name
221+
if b_do_debug:
222+
mask_fname_debug = filenames.get_mask_name(index=index, b_debug_path=True, b_add_tag=False)
223+
else:
224+
mask_fname_debug = None
225+
226+
# The stub of the filename to save all of the data to
227+
mask_fname_calculate = filenames.get_mask_name(index=index, b_calculate_path=True, b_add_tag=False)
228+
229+
if not exists(mask_fname):
230+
print(f"Warning, file {mask_fname} does not exist")
231+
b_crv_mask = FitBezierCyl2DMask(mask_fname, mask_fname_calculate, mask_fname_debug, b_recalc=b_do_recalc)
232+
return b_crv_mask
233+
205234

206235
if __name__ == '__main__':
207-
im = cv2.imread("/Users/grimmc/Downloads/depth_raw_3.jpg")
208-
print(f" {im.min()}, {im.max()}")
236+
path_bpd_envy = "/Users/cindygrimm/VSCode/treefitting/Image_based/data/EnvyTree/"
237+
all_fnames_envy = FileNames.read_filenames(path=path_bpd_envy,
238+
fname="envy_fnames.json")
239+
FitBezierCyl2DMask.create_from_filenames(all_fnames_envy, (0, 0, 0, 0), b_do_recalc=False, b_do_debug=False)
240+
209241
# path_bpd = "./data/trunk_segmentation_names.json"
210242
path_bpd = "./data/forcindy_fnames.json"
211-
all_files = HandleFileNames.read_filenames(path_bpd)
243+
all_files = FileNames.read_filenames(path_bpd)
212244

213245
b_do_debug = True
214246
b_do_recalc = False
215247
for ind in all_files.loop_masks():
216-
mask_fname = all_files.get_mask_name(path=all_files.path, index=ind, b_add_tag=True)
217-
mask_fname_debug = all_files.get_mask_name(path=all_files.path_debug, index=ind, b_add_tag=False)
218-
if not b_do_debug:
219-
mask_fname_debug = None
220-
221-
mask_fname_calculate = all_files.get_mask_name(path=all_files.path_calculated, index=ind, b_add_tag=False)
222-
223-
if not exists(mask_fname):
224-
raise ValueError(f"Error, file {mask_fname} does not exist")
225-
b_stats = FitBezierCyl2DMask(mask_fname, mask_fname_calculate, mask_fname_debug, b_recalc=b_do_recalc)
248+
FitBezierCyl2DMask.create_from_filenames(all_files, ind, b_do_debug=b_do_debug, b_do_recalc=b_do_recalc)

0 commit comments

Comments
 (0)