Beispiel #1
0
        private static Tree InterpretSubchain(List <LayerWrapper> layers, int startInclusive, int endInclusive, Tree tree)
        {
            LayerWrapper currLayer = null;

            try
            {
                for (int i = startInclusive; i <= endInclusive; i++)
                {
                    currLayer = layers[i];
                    Tree.BatchTransform updater = new Tree.BatchTransform(tree);
                    InterpretArgs       args    = new InterpretArgs(tree, updater, currLayer.Intent);
                    currLayer.Layer.Interpret(args);
                    tree = updater.GetUpdatedTree();
                    currLayer.Layer.AfterInterpret(tree);
                }
            }
            catch (Exception e)
            {
                Tree.BatchTransform newupdater = new Tree.BatchTransform(tree);
                LayerException      exception  = new LayerException(currLayer, e);
                newupdater.Tag(tree, "interpretation_exception", exception);
                tree = newupdater.GetUpdatedTree();
            }

            return(tree);
        }
Beispiel #2
0
		public static Tree InterpretChain(IEnumerable<LayerWrapper> layers, Tree start){
            Tree.BatchTransform updater = new Tree.BatchTransform(start);
            Tree tree = start;
            LayerWrapper currLayer = null;
            try
            {
                
                
                foreach (LayerWrapper wrapper in layers)
                {
                    currLayer = wrapper;
                    InterpretArgs args = new InterpretArgs(tree, updater, wrapper.Intent);

                    wrapper.Layer.Interpret(args);
                    tree = updater.GetUpdatedTree();
                    wrapper.Layer.AfterInterpret(tree);
                    
                }


            }
            catch(Exception e)
            {
                Tree.BatchTransform newupdater = new Tree.BatchTransform(tree);
                LayerException exception = new LayerException(currLayer, e);
                newupdater.Tag(tree, "interpretation_exception", exception);
                tree = newupdater.GetUpdatedTree();
            }


            return tree;
		}
Beispiel #3
0
        public static Tree InterpretChain(IEnumerable <LayerWrapper> layers, Tree start)
        {
            Tree.BatchTransform updater = new Tree.BatchTransform(start);
            Tree         tree           = start;
            LayerWrapper currLayer      = null;

            try
            {
                foreach (LayerWrapper wrapper in layers)
                {
                    currLayer = wrapper;
                    InterpretArgs args = new InterpretArgs(tree, updater, wrapper.Intent);

                    wrapper.Layer.Interpret(args);
                    tree = updater.GetUpdatedTree();
                    wrapper.Layer.AfterInterpret(tree);
                }
            }
            catch (Exception e)
            {
                Tree.BatchTransform newupdater = new Tree.BatchTransform(tree);
                LayerException      exception  = new LayerException(currLayer, e);
                newupdater.Tag(tree, "interpretation_exception", exception);
                tree = newupdater.GetUpdatedTree();
            }


            return(tree);
        }
		public void Interpret(InterpretArgs args) {
			Bitmap pixels = (Bitmap)args.Tree["capturedpixels"];


            if (featureTree != null)
            {
                List<Tree> found = new List<Tree>();

                if (args.Tree.HasTag("invalidated"))
                {

                    IBoundingBox invalidated = args.Tree["invalidated"] as IBoundingBox;
                    if (invalidated != null && !BoundingBox.Equals(invalidated, args.Tree) 
                        && args.Tree.HasTag("previous") && args.Tree["previous"] != null)
                    {                        
                        featureTree.MatchInvalidatedRegion(pixels, found, _prevFeatures, invalidated);

                    }
                    else
                        featureTree.MultiThreadedMatch(pixels, found);
                }
                else
                {
                    featureTree.MultiThreadedMatch(pixels, found);
                }

                

                foreach (Tree f in found)
                {
                    args.SetAncestor(f, args.Tree);
                }
            }

		}
Beispiel #5
0
        private void InterpretHelper(InterpretArgs args, Tree currNode)
        {
            if(currNode.Height > 3 && currNode["type"] != null && currNode["type"].Equals("content"))
            {
                args.Tag(currNode, "is_text", true);
            }

            foreach (Tree child in currNode.GetChildren())
                InterpretHelper(args, child);
        }
		/// <summary>
		/// Finds the unpredictable content using the backround regions of elements.
		/// </summary>
		/// <param name="node"></param>
		/// <param name="image"></param>
		/// <param name="background"></param>
		/// <param name="isForeground"></param>
		private static List<Tree> FindAndAddContent(InterpretArgs args, Tree node, Bitmap image, Bitmap isForeground){
			Ptype ptype = (Ptype)node["ptype"];
			List<Tree> allFound = new List<Tree>();
			if(ptype != null){
				ptype.Model.Finder.SetForeground(node, image, isForeground);
			}
			//recurse. if no siblings are overlapping (the majority case) then we can run each child in parallel.
//        if (!anyOverlapping(node.children())){
//            List<Future<ICollection<Tree>>> asyncResults = new List<Future<ICollection<Tree>>>();
//            for(final Tree child : node.children()){
//                Callable<ICollection<Tree>> callable = new Callable<ICollection<Tree>>(){
//                   public ICollection<Tree> call(){
//                        return findAndAddContent(args, ptype, child, image, isForeground);
//                  }
//                };
//                Future<ICollection<Tree>> results =  Utils.service.submit(callable);
//                asyncResults.add(results);
//            }
//
//            for(Future<ICollection<Tree>> res : asyncResults){
//
//                try {
//                    allFound.addAll(res.get());
//                } catch (InterruptedException e) {
//                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//                } catch (ExecutionException e) {
//                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//                }
//            }
//        }
//        else //we can't run children in parallel if any nodes overlap.
//        //nodes only overlap when there's a false positive. so we might be able
//        //to trigger something here and automatically correct that.
//        {
			foreach (Tree child in node.GetChildren())
				FindAndAddContent(args, child, image, isForeground);


			if(ptype != null){
				ptype.Model.Finder.FindContent(node, image, isForeground, allFound);
				foreach(Tree found  in allFound)
					args.SetAncestor(found, node);

                allFound.AddRange(node.GetChildren());
                PrototypeDetectionLayer.SetAncestorsByContainment(allFound, args);
			}



			// }

			return allFound;
		}
		public static int Main(string[] args){
			FeatureDetectionLayer layer = new FeatureDetectionLayer();
			layer.Init(null);
			Bitmap bmp = Bitmap.FromFile("../prefab/AdobePreferences.png");
			Dictionary<string, object> tags = new Dictionary<string, object>();
			Tree tree = Tree.FromPixels(bmp, tags);

			InterpretArgs iargs = new InterpretArgs(tree, new Tree.BatchTransform(tree), null);

			layer.Interpret(iargs);

			return 0;
		}
        public static void SetAncestorsByContainment(List<Tree> nodes, InterpretArgs args)
        {
            nodes.Sort(CompareByAreaAndFeaturesFirst);

            for (int i = 0; i < nodes.Count - 1; i++)
            {
                Tree curr = nodes[i];

                for (int j = i + 1; j < nodes.Count; j++)
                {

                    Tree next = nodes[j];
                    if (BoundingBox.IsInsideInclusive(curr, next))
                        args.SetAncestor(curr, next);
                }
            }
        }
Beispiel #9
0
        public void InterpretHelper(InterpretArgs args, Tree currNode)
        {
            IEnumerable<Tree> textChildren = currNode.GetChildren().Where(c => c.ContainsTag("is_text") && (bool)c["is_text"]);

            List<Tree> readingOrder = SortNodesInReadingOrder(textChildren);

            for (int i = 0; i < readingOrder.Count - 1; i++)
            {
                Tree curr = readingOrder[i];
                Tree next = readingOrder[i + 1];

                args.Tag(curr, "group_next", GroupNext(curr, next));
            }

            //recurse
            foreach (Tree child in currNode.GetChildren())
                InterpretHelper(args, child);
        }
		public void Interpret(InterpretArgs args) {

			ptypes = (List<Ptype>)shared[Features.FeatureDetectionLayer.SHARED_PTYPES_KEY];
			found.Clear();

			//Finding prototype occurrences
			Bitmap pixels = (Bitmap)args.Tree["capturedpixels"];
			IEnumerable<Tree> features = args.Tree.GetChildren();

			foreach(Ptype p in ptypes){
				p.Model.Finder.FindOccurrences(p, pixels, features, found);
			}


			//Creating the spatial hierarchy
			Tree.AddNodesToCollection(args.Tree, found);
            SetAncestorsByContainment(found, args);

		}
Beispiel #11
0
        private void InterpretHelper(InterpretArgs args, Tree node)
        {
            if (node.ContainsTag("type") && node["type"].Equals("ptype"))
            {
                args.Tag(node, "is_target", "true");
            }
            else if (node.ContainsTag("type") && !node["type"].Equals("ptype"))
            {
                foreach (Tree child in node.GetChildren())
                {
                    if (child.ContainsTag("type") && !child["type"].Equals("feature") && child.GetChildren().Count() == 0)
                    {
                        args.Tag(child, "is_target", "true");
                    }
                }
            }

            foreach (Tree child in node.GetChildren())
                InterpretHelper(args, child);
        }
Beispiel #12
0
 public void Interpret(InterpretArgs args)
 {
     InterpretHelper(args, args.Tree);
 }
		private static Tree InterpretSubchain(List<LayerWrapper> layers, int startInclusive, int endInclusive, Tree tree){
			
			LayerWrapper currLayer = null;
			try
			{
				for (int i = startInclusive; i <= endInclusive; i++)
				{
					currLayer = layers[i];
					Tree.BatchTransform updater = new Tree.BatchTransform(tree);
					InterpretArgs args = new InterpretArgs(tree, updater, currLayer.Intent);
					currLayer.Layer.Interpret(args);
					tree = updater.GetUpdatedTree();
					currLayer.Layer.AfterInterpret(tree);
				}

			}
			catch (Exception e)
			{
                Tree.BatchTransform newupdater = new Tree.BatchTransform(tree);
                LayerException exception = new LayerException(currLayer, e);
                newupdater.Tag(tree, "interpretation_exception", exception);
                tree = newupdater.GetUpdatedTree();
			}

			return tree;
		}
		public void Interpret(InterpretArgs args) {
			Bitmap bitmap = (Bitmap)args.Tree["capturedpixels"];
			FindAndAddContent(args, args.Tree,bitmap, foreground);
		}
Beispiel #15
0
        private void InterpretHelper(InterpretArgs args, Tree node)
        {
            string path = PathDescriptor.GetPath(node, args.Tree);

            JObject data = (JObject)_runtimeIntent.GetData(path);

            if(data != null)
            {
                foreach (var key in data.Properties())
                {
                    bool parsedbool = false;

                    if (bool.TryParse(data[key.Name].Value<string>(), out parsedbool))
                    {
                        args.Tag(node, key.Name, parsedbool);
                    }else{
                        args.Tag(node, key.Name, data[key.Name].Value<string>());
                    }

                }
            }

            foreach (Tree child in node.GetChildren())
            {
                InterpretHelper(args, child);
            }
        }
Beispiel #16
0
 public void Interpret(InterpretArgs args)
 {
     List<Tree> features = FeatureDetectionLayer.GetFeaturesFromTree(args.Tree);
     foreach (Tree feature in features)
         args.Remove(feature);
 }
Beispiel #17
0
        private void InterpretHelper(InterpretArgs args, Tree currNode)
        {
            var children = currNode.GetChildren().Where( n => n["is_text"] != null && (bool)n["is_text"]);
            var sorted = GroupNextLayer.SortNodesInReadingOrder(children);

            bool createNew = true;
            List<Tree> togroup = new List<Tree>();
            for (int i = 1; i < sorted.Count; i++ )
            {

                Tree currChild = sorted[i];
                Tree prevChild = sorted[i - 1];
                if (prevChild["group_next"] != null && (bool)prevChild["group_next"])
                {
                    if (createNew)
                    {
                        createNew = false;
                        togroup.Add(prevChild);
                    }

                    togroup.Add(currChild);
                }
                else
                {
                    if (togroup.Count > 0)
                    {
                        IBoundingBox bounding = BoundingBox.Union(togroup);
                        var tags = new Dictionary<string, object>();
                        tags["type"] = "grouped_text";
                        tags["is_text"] = true;
                        Tree grouped = Tree.FromBoundingBox(bounding, tags);

                        foreach (Tree t in togroup)
                            args.SetAncestor(t, grouped);

                        args.SetAncestor(grouped, currNode);

                        togroup.Clear();
                    }

                    createNew = true;

                }
            }

            if (togroup.Count > 0)
            {
                IBoundingBox bounding = BoundingBox.Union(togroup);
                var tags = new Dictionary<string, object>();
                tags["type"] = "grouped_text";
                tags["is_text"] = true;
                Tree grouped = Tree.FromBoundingBox(bounding, tags);

                foreach (Tree t in togroup)
                    args.SetAncestor(t, grouped);

                args.SetAncestor(grouped, currNode);

                togroup.Clear();
            }

            foreach (Tree child in currNode.GetChildren())
                InterpretHelper(args, child);
        }
 public PythonInterpretArgs(InterpretArgs interpretArgs)
 {
     tree = interpretArgs.Tree;
     runtime_storage = new PythonRuntimeStorageWrapper(interpretArgs.RuntimeStorage);
     args = interpretArgs;
 }
Beispiel #19
0
        public void Interpret(InterpretArgs args)
        {
            try
            {
                PythonInterpretArgs pargs = new PythonInterpretArgs(args);
                if (_interpretFunc != null)
                {
                    _scope.Engine.Operations.Invoke(_interpretFunc, pargs);
                }

            }
            catch(Exception e)
            {
                throw PythonScriptHost.Instance.GetFormattedException(e, Name);
            }
            
        }