Skip to content

Commit 652fefa

Browse files
committed
error checking imgs
1 parent cbd3b6f commit 652fefa

16 files changed

Lines changed: 303 additions & 245 deletions

File tree

dliblib/dliblib.pyproj

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<SchemaVersion>2.0</SchemaVersion>
66
<ProjectGuid>{34014272-854c-4379-95c8-9dc9ca7b233a}</ProjectGuid>
77
<ProjectHome />
8-
<StartupFile>scripts\train_shape_predictor.py</StartupFile>
8+
<StartupFile>scripts\validate_points.py</StartupFile>
99
<SearchPath>..\</SearchPath>
1010
<WorkingDirectory>
1111
</WorkingDirectory>
@@ -15,8 +15,7 @@
1515
<InterpreterId />
1616
<InterpreterVersion />
1717
<IsWindowsApplication>False</IsWindowsApplication>
18-
<CommandLineArguments>
19-
</CommandLineArguments>
18+
<CommandLineArguments>"C:/Users/Graham Monkman/OneDrive/Documents/PHD/images/bass/fiducial/train/candidate/roi_grow/resized/vgg_landmarks.json"</CommandLineArguments>
2019
<EnableNativeCodeDebugging>False</EnableNativeCodeDebugging>
2120
<Environment>vgg_pts_to_xml.py "C:/Users/Graham Monkman/OneDrive/Documents/PHD/images/bass/fiducial/train/candidate/roi_whole/resized/vgg_keypoints.json" "C:/Users/Graham Monkman/OneDrive/Documents/PHD/images/bass/fiducial/train/candidate/roi_whole/resized/vgg_keypoints.xml"</Environment>
2221
</PropertyGroup>
@@ -31,7 +30,10 @@
3130
<Compile Include="scripts\train_shape_predictor.py">
3231
<SubType>Code</SubType>
3332
</Compile>
34-
<Compile Include="scripts\vgg_pts_to_xml.py">
33+
<Compile Include="scripts\validate_points.py">
34+
<SubType>Code</SubType>
35+
</Compile>
36+
<Compile Include="scripts\vgg2xml.py">
3537
<SubType>Code</SubType>
3638
</Compile>
3739
<Compile Include="test\test_vgg2xml.py">

dliblib/scripts/vgg_pts_to_xml.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

dliblib/vgg2xml.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import opencvlib.roi as _roi
1515

1616
SILENT = False
17+
VALID_LABELS = [str(lbl).zfill(2) for lbl in range(1, 20)]
1718

1819
def _create_xml(fname):
1920
'''(str) -> void
@@ -54,6 +55,11 @@ def _validate(vggImg, ignore_no_roi):
5455
assert isinstance(vggReg, _vgg.Region)
5556
lbl = int(vggReg.region_json_key) + 1 if vggReg.region_attr.get('pts', '') == '' else vggReg.region_attr['pts']
5657
lbl = str(lbl)
58+
if VALID_LABELS:
59+
lblz = lbl.zfill(2)
60+
if not lblz in VALID_LABELS:
61+
problems.append('lbl %s not a valid label (vgg2xml.VALID_LABEL)' % lblz)
62+
valid = False
5763
if lbl in lbls:
5864
problems.append('lbl %s duplicated.' % lbl)
5965
valid = False
@@ -107,8 +113,6 @@ def convert(vgg_in, xml_out, ignore_no_roi=True):
107113
if not SILENT:
108114
PP.increment()
109115
assert isinstance(vggImg, _vgg.Image)
110-
if vggImg.filename == 'roir5b9bdbaba0b59679f838c4d1619f0c9d1ddca410.jpg':
111-
x = 10
112116

113117
valid, problems = _validate(vggImg, ignore_no_roi)
114118
if not valid:

funclib/iolib.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ def print_progress(
892892
decimals - Optional : number of decimals in percent complete (Int)
893893
bar_length - Optional : character length of progbar (Int)
894894
"""
895-
filled_length = int(round(bar_length * iteration / float(total)))
895+
filled_length = int(round(bar_length * iteration / float(total))) if total > 0 else 0
896896
if iteration / float(total) > 1:
897897
total = iteration
898898
percents = round(100.00 * (iteration / float(total)), decimals)
@@ -969,6 +969,17 @@ def increment(self, step=1):
969969
self.iteration, self.max, prefix='%i of %i' %
970970
(self.iteration, self.max), bar_length=self.bar_length)
971971
self.iteration += step
972+
973+
def reset(self, max=None):
974+
'''reset the counter. set max
975+
if need to change total expected
976+
iterations.
977+
'''
978+
if max:
979+
self.max = max
980+
self.iteration = 1
981+
982+
972983
# endregion
973984

974985

geometry/point.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,13 +1455,23 @@ def rotate2d(self, theta, origin=None, axis='z', radians=False):
14551455
raise KeyError('unknown axis {}, expecting x, y or z'.format(axis))
14561456

14571457
def irotate2d(self, theta, origin=None, axis='z', radians=False):
1458-
14591458
self.xyz = self.rotate2d(theta, origin, axis, radians).xyz
1460-
14611459
return self
14621460

1463-
def rotate(self, theta_x, theta_y, theta_z, origin=None, radians=False):
14641461

1462+
def angle_origin(self, as_degrees=True):
1463+
'''(bool) -> float
1464+
Calculate the angle between two points
1465+
'''
1466+
ang1 = math.atan2(self.y, self.x)
1467+
ang2 = math.atan2(0, 0)
1468+
if as_degrees:
1469+
return 180/math.pi * ((ang1 - ang2) % (2 * math.pi))
1470+
else:
1471+
return (ang1 - ang2) % (2 * math.pi)
1472+
1473+
1474+
def rotate(self, theta_x, theta_y, theta_z, origin=None, radians=False):
14651475
origin = Point._convert(origin)
14661476

14671477
thetas = [theta_x, theta_y, theta_z]
@@ -1488,18 +1498,18 @@ def __init__(self,*args,**kwds):
14881498
:base: optional base character for labeling points
14891499
14901500
The first point is labeled with the base character and
1491-
subsequent points are labeled with the character
1501+
subsequent points are labeled with the character
14921502
calculated by:
14931503
14941504
label = chr(ord(base)+index)
1495-
1505+
14961506
'''
14971507
self(*args, **kwds)
14981508

14991509
def __call__(self, *args, **kwds):
15001510
'''
15011511
'''
1502-
1512+
15031513
try:
15041514
self._base = kwds['base']
15051515
except KeyError:
@@ -1514,7 +1524,7 @@ def __call__(self, *args, **kwds):
15141524
if len(args) == 1:
15151525
self.vertices.extend(args[0])
15161526
else:
1517-
self.vertices.extend(args)
1527+
self.vertices.extend(args)
15181528

15191529
try:
15201530
for i,p in enumerate(kwds['defaults']):
@@ -1531,7 +1541,7 @@ def __call__(self, *args, **kwds):
15311541
setattr(self,label,kwds[label])
15321542
except KeyError:
15331543
pass
1534-
1544+
15351545
@property
15361546
def vertices(self):
15371547
try:
@@ -1608,7 +1618,7 @@ def __getattr__(self, attr):
16081618
return self.__dict__[attr]
16091619
except KeyError:
16101620
pass
1611-
1621+
16121622
raise AttributeError("{} object has no attribute '{}'".format(self.__class__.__name__,attr))
16131623

16141624
def __setattr__(self, attr, value):
@@ -1632,7 +1642,7 @@ def __setattr__(self, attr, value):
16321642

16331643
super().__setattr__(attr, value)
16341644

1635-
1645+
16361646
def __delattr__(self, attr):
16371647
'''
16381648
@@ -1684,7 +1694,7 @@ def pairs(self,xy=None):
16841694

16851695
if isinstance(xy,collections.Iterable):
16861696
return (self[xy[0]],self[xy[1]])
1687-
1697+
16881698
return zip(self[0:],self[1:]+self[0:1])
16891699

16901700
def __eq__(self,other):
@@ -1695,9 +1705,9 @@ def __eq__(self,other):
16951705
'''
16961706

16971707
return len(set(self.vertices).difference(set(other.vertices))) == 0
1698-
1699-
1700-
1708+
1709+
1710+
17011711
class MutablePointSequence(PointSequence,collections.MutableSequence):
17021712
'''
17031713
A labeled mutable sequence of points. Work in Progress.
@@ -1768,25 +1778,25 @@ def __add__(self, other):
17681778
def __isub__(self, other):
17691779
for p in self:
17701780
p -= other
1771-
return self
1772-
1781+
return self
1782+
17731783
def __imul__(self, other):
17741784
for p in self:
17751785
p *= other
1776-
return self
1786+
return self
17771787

17781788
def __itruediv__(self, other):
17791789
for p in self:
17801790
p /= other
17811791
return self
1782-
1792+
17831793
def __ifloordiv__(self, other):
17841794
for p in self:
17851795
p //= other
17861796
return self
17871797

1788-
1789-
1798+
1799+
17901800

17911801

17921802

imagedb/imagedb.sln

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "pgnet", "..\pgnet\pgnet.pyp
2424
EndProject
2525
Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "dliblib", "..\dliblib\dliblib.pyproj", "{34014272-854C-4379-95C8-9DC9CA7B233A}"
2626
EndProject
27+
Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "geometry", "..\geometry\geometry.pyproj", "{D0E36231-F132-478A-8399-3E00DDF08AE0}"
28+
EndProject
2729
Global
2830
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2931
Debug|Any CPU = Debug|Any CPU
@@ -44,6 +46,8 @@ Global
4446
{52FB7047-B7AD-43D1-A402-BB033DB31901}.Release|Any CPU.ActiveCfg = Release|Any CPU
4547
{34014272-854C-4379-95C8-9DC9CA7B233A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
4648
{34014272-854C-4379-95C8-9DC9CA7B233A}.Release|Any CPU.ActiveCfg = Release|Any CPU
49+
{D0E36231-F132-478A-8399-3E00DDF08AE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
50+
{D0E36231-F132-478A-8399-3E00DDF08AE0}.Release|Any CPU.ActiveCfg = Release|Any CPU
4751
EndGlobalSection
4852
GlobalSection(SolutionProperties) = preSolution
4953
HideSolutionNode = FALSE

opencvlib/geometry.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ def angle_between_pts(pt1, pt2, as_degrees=True):
4747
'''
4848
ang1 = _np.arctan2(*pt1[::-1])
4949
ang2 = _np.arctan2(*pt2[::-1])
50-
return _np.rad2deg((ang1 - ang2) % (2 * _np.pi))
50+
if as_degrees:
51+
return _np.rad2deg((ang1 - ang2) % (2 * _np.pi))
52+
53+
return (ang1 - ang2) % (2 * _np.pi)
5154

5255

5356
def angle_min_rotation_to_x(angle, as_degrees=True):

opencvlib/imgpipes/generators.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,8 @@ def vggParams(self, vggParams):
511511
self.dirty_filters = True #for use in inherited classes, not needed for this explicitly
512512

513513

514-
def generate(self, *args, pathonly=False, outflag=_cv2.IMREAD_UNCHANGED, **kwargs):
515-
'''(bool)-> ndarray,str, dict
514+
def generate(self, *args, pathonly=False, outflag=_cv2.IMREAD_UNCHANGED, file_attr_match=None, **kwargs):
515+
'''(bool, int, dict)-> ndarray,str, dict
516516
517517
Uses the filters set in the VGGFilter and DigikamSearchParams
518518
to yield image regions to the caller.
@@ -522,6 +522,9 @@ def generate(self, *args, pathonly=False, outflag=_cv2.IMREAD_UNCHANGED, **kwarg
522522
outflag:
523523
cv2 flag for imread
524524
cv2.IMREAD_COLOR|cv2.IMREAD_GRAYSCALE|cv2.IMREAD_UNCHANGED
525+
file_attr_match:
526+
A dictionary, Only generate regions for file which match these attributes.
527+
e.g. file_attr_match = {'train'=1}
525528
526529
Returns [Yields]
527530
image region, imgpath, dictionary containing additional infornation
@@ -550,7 +553,7 @@ def generate(self, *args, pathonly=False, outflag=_cv2.IMREAD_UNCHANGED, **kwarg
550553
if not self.silent:
551554
print('Opened regions file %s' % p)
552555

553-
for Img in _vgg.imagesGenerator():
556+
for Img in _vgg.imagesGenerator(file_attr_match=file_attr_match):
554557

555558
if dk_image_list:
556559
if not Img.filepath in dk_image_list: # effectively applying a filter for the digikamlib conditions
@@ -593,7 +596,7 @@ def generate(self, *args, pathonly=False, outflag=_cv2.IMREAD_UNCHANGED, **kwarg
593596
if not self.silent:
594597
print('Opened regions file %s' % p)
595598

596-
for Img in _vgg.imagesGenerator():
599+
for Img in _vgg.imagesGenerator(file_attr_match=file_attr_match):
597600

598601
if dk_image_list:
599602
if not Img.filepath in dk_image_list: # effectively applying a filter for the digikamlib conditions
@@ -662,11 +665,23 @@ def __init__(self, vgg_file_paths, *args, **kwargs):
662665
super().__init__(*args, **kwargs)
663666

664667

665-
def generate(self, path_only=False, outflag=_cv2.IMREAD_UNCHANGED):
668+
def generate(self, path_only=False, outflag=_cv2.IMREAD_UNCHANGED, file_attr_match=None):
669+
'''(bool, int, dict|None) -> ndarray|None, str, dict
670+
Generate images and paths, dict will be empty.
671+
672+
file_attr_match:
673+
a dictionary which is checked for a partial
674+
match against the image file attributes,
675+
eg {'is_train'=1}
676+
677+
Example:
678+
>>>for Img, pth, _ in VGGImages.generate(file_attr_match={'train'=1}):
679+
#do some work
680+
'''
666681
for vgg_file in self.vgg_file_paths:
667682
try:
668683
_vgg.load_json(vgg_file)
669-
for I in _vgg.imagesGenerator():
684+
for I in _vgg.imagesGenerator(file_attr_match=file_attr_match):
670685
if path_only:
671686
yield None, I.filepath, None
672687
continue
@@ -715,8 +730,8 @@ def __init__(self, vgg_file_paths, *args, region_attrs=None, **kwargs):
715730
super().__init__(*args, **kwargs)
716731

717732

718-
def generate(self, shape_type='rect', region_attrs=None, path_only=False, outflag=_cv2.IMREAD_UNCHANGED, skip_imghdr_check=False, grow_roi_proportion=1):
719-
'''(str|list|None, dict, bool, cv2.imread option, bool) -> ndarray|None, str, dict
733+
def generate(self, shape_type='rect', region_attrs=None, path_only=False, outflag=_cv2.IMREAD_UNCHANGED, skip_imghdr_check=False, grow_roi_proportion=1, file_attr_match=None):
734+
'''(str|list|None, dict, bool, cv2.imread option, bool, dict) -> ndarray|None, str, dict
720735
Yields the images with the bounding boxes and category name of all objects
721736
in the pascal voc images
722737
@@ -742,7 +757,9 @@ def generate(self, shape_type='rect', region_attrs=None, path_only=False, outfla
742757
grow_roi_percent:
743758
increase or decreaese roi by this percentage of the
744759
original roi.
745-
760+
file_attr_match:
761+
If not None, only regions from images with the matching file attributes
762+
will be generated
746763
747764
Yields:
748765
image, path, region_attributes dict
@@ -759,7 +776,7 @@ def generate(self, shape_type='rect', region_attrs=None, path_only=False, outfla
759776
for vgg_file in self.vgg_file_paths:
760777
try:
761778
_vgg.load_json(vgg_file)
762-
for I in _vgg.imagesGenerator(skip_imghdr_check=skip_imghdr_check):
779+
for I in _vgg.imagesGenerator(skip_imghdr_check=skip_imghdr_check, file_attr_match=file_attr_match):
763780
for reg in I.roi_generator(shape_type, self.region_attrs):
764781
assert isinstance(reg, _vgg.Region)
765782
if path_only:

0 commit comments

Comments
 (0)