1
+ import traceback
2
+ import json
3
+ from pathlib import Path
4
+ import numpy as np
5
+
6
+ from core import pathex
7
+ from core .imagelib import IEPolys
8
+ from core .interact import interact as io
9
+ from DFLIMG import *
10
+
11
+
12
+ def merge (input_dir ):
13
+ input_path = Path (input_dir )
14
+ if not input_path .exists ():
15
+ raise ValueError ('input_dir not found. Please ensure it exists.' )
16
+
17
+ images_paths = pathex .get_image_paths (input_path , return_Path_class = True )
18
+
19
+ images_processed = 0
20
+ for filepath in io .progress_bar_generator (images_paths , "Processing" ):
21
+ json_filepath = filepath .parent / (filepath .stem + '.json' )
22
+ if json_filepath .exists ():
23
+ dflimg = DFLIMG .load (filepath )
24
+ if dflimg is not None :
25
+ try :
26
+ json_dict = json .loads (json_filepath .read_text ())
27
+
28
+ seg_ie_polys = IEPolys ()
29
+ total_points = 0
30
+
31
+ #include polys first
32
+ for shape in json_dict ['shapes' ]:
33
+ if shape ['shape_type' ] == 'polygon' and \
34
+ shape ['label' ] != '0' :
35
+ seg_ie_poly = seg_ie_polys .add (1 )
36
+
37
+ for x ,y in shape ['points' ]:
38
+ seg_ie_poly .add ( int (x ), int (y ) )
39
+ total_points += 1
40
+
41
+ #exclude polys
42
+ for shape in json_dict ['shapes' ]:
43
+ if shape ['shape_type' ] == 'polygon' and \
44
+ shape ['label' ] == '0' :
45
+ seg_ie_poly = seg_ie_polys .add (0 )
46
+
47
+ for x ,y in shape ['points' ]:
48
+ seg_ie_poly .add ( int (x ), int (y ) )
49
+ total_points += 1
50
+
51
+ if total_points == 0 :
52
+ io .log_info (f"No points found in { json_filepath } , skipping." )
53
+ continue
54
+
55
+ dflimg .embed_and_set (filepath , seg_ie_polys = seg_ie_polys )
56
+
57
+ json_filepath .unlink ()
58
+
59
+ images_processed += 1
60
+ except :
61
+ io .log_err (f"err { filepath } , { traceback .format_exc ()} " )
62
+ return
63
+
64
+ io .log_info (f"Images processed: { images_processed } " )
65
+
66
+ def split (input_dir ):
67
+ input_path = Path (input_dir )
68
+ if not input_path .exists ():
69
+ raise ValueError ('input_dir not found. Please ensure it exists.' )
70
+
71
+ images_paths = pathex .get_image_paths (input_path , return_Path_class = True )
72
+
73
+ images_processed = 0
74
+ for filepath in io .progress_bar_generator (images_paths , "Processing" ):
75
+ json_filepath = filepath .parent / (filepath .stem + '.json' )
76
+
77
+
78
+ dflimg = DFLIMG .load (filepath )
79
+ if dflimg is not None :
80
+ try :
81
+ seg_ie_polys = dflimg .get_seg_ie_polys ()
82
+ if seg_ie_polys is not None :
83
+ json_dict = {}
84
+ json_dict ['version' ] = "4.2.9"
85
+ json_dict ['flags' ] = {}
86
+ json_dict ['shapes' ] = []
87
+ json_dict ['imagePath' ] = filepath .name
88
+ json_dict ['imageData' ] = None
89
+
90
+ for poly_type , points_list in seg_ie_polys :
91
+ shape_dict = {}
92
+ shape_dict ['label' ] = str (poly_type )
93
+ shape_dict ['points' ] = points_list
94
+ shape_dict ['group_id' ] = None
95
+ shape_dict ['shape_type' ] = 'polygon'
96
+ shape_dict ['flags' ] = {}
97
+ json_dict ['shapes' ].append ( shape_dict )
98
+
99
+ json_filepath .write_text ( json .dumps (json_dict ,indent = 4 ) )
100
+
101
+ dflimg .remove_seg_ie_polys ()
102
+ dflimg .embed_and_set (filepath )
103
+ images_processed += 1
104
+ except :
105
+ io .log_err (f"err { filepath } , { traceback .format_exc ()} " )
106
+ return
107
+
108
+ io .log_info (f"Images processed: { images_processed } " )
0 commit comments