我正在尝试用javascript制作多边形直骨架。我选择的方法是运行python脚本。我选择的Python脚本在命令行中工作,但在使用npm的pythonshell运行时失败。你知道吗
我正在尝试用Javascript创建多边形的骨架,我找到的唯一真正的方法是通过一个名为Polyskel的python脚本。你知道吗
我要做很多骨架,所以通过node运行它是有利的(所有的多边形都是JSON格式的,我不知道python),所以我决定从javascript运行python。我使用Python-shell作为节点。你知道吗
现在,在测试我的代码时,我创建了一个与其中一个示例文件相同的文件,并运行python demo_2.py polyskel-master/currentfile
(下面是demo\u 2.py)。输出是格式正确的骨架。你知道吗
其次,我运行了node python_runner.js
,这给了我一个又一个错误。我修正了一些错误,但现在我被一个无法修正的错误困住了。你知道吗
<dir>\python_runner.js:10
if (err) throw err;
^
Error: TypeError: '<' not supported between instances of 'Point2' and 'Point2'
at PythonShell.parseError (<dir>\node_modules\python-shell\index.js:246:21)
at terminateIfNeeded (<dir>\node_modules\python-shell\index.js:129:32)
at ChildProcess.<anonymous> (<dir>\node_modules\python-shell\index.js:121:13)
at emitTwo (events.js:126:13)
at ChildProcess.emit (events.js:214:7)
at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12)
----- Python Traceback -----
File "polyskel-master\runner.py", line 17, in <module>
ret = polyskel.skeletonize(nested_lst_of_tuples,[])
File "<dir>\polyskel-master\polyskel.py", line 440, in skeletonize
prioque.put(vertex.next_event())
File "C<dir>\polyskel-master\polyskel.py", line 405, in put
heapq.heappush(self.__data, item)
因此,长话短说,相同的脚本在命令行中工作,在pythonshell中失败。你知道吗
有没有办法让pythonshell对错误不那么敏感?你知道吗
这是可以复制的(我想!)通过从上面的链接下载polyskel,安装皮欧几里德.py在同一文件夹中,然后输入以下代码:
Python_跑步者.js你知道吗
ps = require("python-shell")
fs = require("fs");
let options = {
mode: 'text', // get print results in real-time
scriptPath: '',
args: "polyskel-master\\currentfile"
};
polygon = [[40, 40],[40, 310],[520, 310],[520, 40]]
str = ""
for (var i=0; i<polygon.length; i++)
{
str+= polygon[i][0]+", "+polygon[i][1]
str+= "\n"
}
fs.writeFileSync("polyskel-master/currentfile",str)
ps.PythonShell.run('polyskel-master/demo_2.py', options, function (err, results) {
if (err) throw( err);
// results is an array consisting of messages collected during execution
console.log('results: %j', results);
});
演示2.py
import logging
import argparse
import re
import polyskel
if __name__ == "__main__":
logging.basicConfig()
argparser = argparse.ArgumentParser(description="Construct the straight skeleton of a polygon. The polygon is to be given as a counter-clockwise series of vertices specified by their coordinates: see the example files for the exact format.")
argparser.add_argument('polygon_file', metavar="<polygon-file>", type=argparse.FileType('r'), help="text file describing the polygon ('-' for standard input)")
argparser.add_argument('--verbose', '--v', action="store_true", default=False, help="Show construction of the skeleton")
argparser.add_argument('--log', dest="loglevel", choices=['DEBUG', 'INFO', 'WARNING', 'ERROR'], default='WARNING', help="Set log level")
args = argparser.parse_args()
polyskel.log.setLevel(getattr(logging, args.loglevel))
polygon_line_pat = re.compile(r"\s*(?P<coord_x>\d+(\.\d+)?)\s*,\s*(?P<coord_y>\d+(\.\d+)?)\s*(#.*)?")
contours = []
poly = []
for line in args.polygon_file:
line = line.strip()
if not line or line.startswith('#'):
continue
if line.startswith('-'):
contours.append(poly)
poly = []
continue
match = polygon_line_pat.match(line)
poly.append((float(match.group("coord_x")), float(match.group("coord_y"))))
if not args.polygon_file.isatty():
args.polygon_file.close()
contours.append(poly)
poly = contours[0]
holes = contours[1:] if len(contours) > 0 else None
bbox_end_x = int(max(poly, key=lambda x: x[0])[0]+20)
bbox_end_y = int(max(poly, key=lambda x: x[1])[1]+20)
print(poly)
print(holes)
skeleton = polyskel.skeletonize(poly, holes)
print(skeleton)
再次感谢你的帮助。你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐