private IEnumerable<ILabelledFeature> extractRegionsFromYaml( YamlStream yamlStream ) { List<ILabelledFeature> features = new List<ILabelledFeature>(); // Pull out the regions from the YAML files foreach ( YamlDocument doc in yamlStream.Documents ) { Assert.IsInstanceOf( typeof( Mapping ), doc.Root ); Mapping m = ( Mapping ) doc.Root; foreach ( MappingEntry me in m.Enties ) { Assert.IsInstanceOf( typeof( Mapping ), me.Value ); Mapping mPoints = ( Mapping ) me.Value; Dictionary<String, Int32> coords = MappingConverter.convertToDictionary( mPoints, MappingConverter.KeyCase.FORCE_LOWER ); Point[] points = new Point[ 4 ]; bool found = true; int x = 0; int y = 0; int width = 0; int height = 0; found = found && coords.TryGetValue( "x", out x ); found = found && coords.TryGetValue( "y", out y ); found = found && coords.TryGetValue( "width", out width ); found = found && coords.TryGetValue( "height", out height ); if ( found ) { if ( ( x == 0 ) && ( y == 0 ) && ( width == 0 ) && ( height == 0 ) ) { regionAllZero++; } else { points[ 0 ] = new Point( x, y ); points[ 1 ] = new Point( x + width, y ); points[ 2 ] = new Point( x + width, y + height ); points[ 3 ] = new Point( x, y + height ); DataSetEnums.FeatureName featureName; bool featureNameFound = tryGetFeatureNameFor( me.Key.ToString(), out featureName ); if ( featureNameFound ) { features.Add( new LabelledFeature( featureName, new MultiPointPath( points, true ) ) ); } else { unrecognisedFeatureName++; } } } else { notFoundRegionCorners++; } } } return features; }
private YamlStream ParseYamlStream(out bool success) { int errorCount = Errors.Count; YamlStream yamlStream = new YamlStream(); int start_position = position; while (true) { ParseComment(out success); if (!success) { break; } } success = true; while (true) { int seq_start_position1 = position; YamlDocument yamlDocument = ParseImplicitDocument(out success); if (success) { yamlStream.Documents.Add(yamlDocument); } success = true; while (true) { yamlDocument = ParseExplicitDocument(out success); if (success) { yamlStream.Documents.Add(yamlDocument); } else { break; } } success = true; break; } success = !Input.HasInput(position); if (!success) { Error("Failed to parse end of YamlStream."); position = start_position; } if (success) { ClearError(errorCount); } return yamlStream; }
private IEnumerable<ILabelledFeature> extractContoursFromYaml( YamlStream yamlStream ) { List<ILabelledFeature> features = new List<ILabelledFeature>(); //return features; // Pull out the contours from the YAML files foreach ( YamlDocument doc in yamlStream.Documents ) { Assert.IsInstanceOf( typeof( Mapping ), doc.Root ); Mapping m = ( Mapping ) doc.Root; // Validate that this refers to the correct file 'Image file' should be '00011010.jpg' for example Assert.True( m.Enties[ 0 ].Key.ToString().CompareTo( "Image file" ) == 0, "Unexpected order of Mapping elements" ); Assert.True( m.Enties[ 1 ].Key.ToString().CompareTo( "Contours count" ) == 0, "Unexpected order of Mapping elements" ); Assert.True( m.Enties[ 2 ].Key.ToString().CompareTo( "Contours" ) == 0, "Unexpected order of Mapping elements" ); // m.Enties[ 2 ].Value; Assert.IsInstanceOf( typeof( Sequence ), m.Enties[ 2 ].Value ); Sequence seqContours = ( Sequence ) m.Enties[ 2 ].Value; foreach ( DataItem di in seqContours.Enties ) { Assert.IsInstanceOf( typeof( Mapping ), di ); Mapping mdi = ( Mapping ) di; Assert.True( mdi.Enties[ 0 ].Key.ToString().CompareTo( "Name" ) == 0, "Unexpected order of Mapping elements" ); Assert.True( mdi.Enties[ 1 ].Key.ToString().CompareTo( "Count" ) == 0, "Unexpected order of Mapping elements" ); Assert.True( mdi.Enties[ 2 ].Key.ToString().CompareTo( "Closed" ) == 0, "Unexpected order of Mapping elements" ); Assert.True( mdi.Enties[ 3 ].Key.ToString().CompareTo( "Points" ) == 0, "Unexpected order of Mapping elements" ); DataSetEnums.FeatureName featureName; bool featureNameFound = tryGetFeatureNameFor( mdi.Enties[ 0 ].Value.ToString(), out featureName ); // Skip this one if unrecognised feature name if ( !featureNameFound ) { unrecognisedFeatureName++; continue; } // Skip this one if there is a zero 'Contours count' if ( Convert.ToInt32( mdi.Enties[ 1 ].Value.ToString() ) == 0 ) { noContours++; continue; } // Figure out if closed or open bool closed = ( mdi.Enties[ 2 ].Value.ToString().CompareTo( "1" ) == 0 ); // Now extract the sequence of x,y values Sequence pointSeq = ( Sequence ) mdi.Enties[ 3 ].Value; Point[] points = new Point[ pointSeq.Enties.Count ]; for ( int i = 0; i < pointSeq.Enties.Count; i++ ) { Mapping pointMap = ( Mapping ) pointSeq.Enties[ i ]; Dictionary<String, Int32> coords = MappingConverter.convertToDictionary( pointMap, MappingConverter.KeyCase.FORCE_LOWER ); bool found = true; int x = 0; int y = 0; found = found && coords.TryGetValue( "x", out x ); found = found && coords.TryGetValue( "y", out y ); Assert.True( found, "Could not convert coordinates" ); points[ i ] = new Point( x, y ); } // This feature is a multiple point path features.Add( new LabelledFeature( featureName, new MultiPointPath( points, closed ) ) ); } } return features; }