我有一个程序正在运行,它读取一个 JSON 文件。这个 JSON 文件有一些内容:标题 和“框”。标题是图像的标题,方框是覆盖您提供给父程序(生成的这个 json 文件)的图像部分的小框架。这些框的作用并不完全重要,真的 - 所有需要知道的是,对于“标题”字段中的每个标题,“框”字段中都有一个框。盒子是四个数字的列表:x、y、宽度、高度。它形成一个矩形。
无论如何,我正在做一些非常简单的事情——我制作了一个简单的程序来读取这个 JSON 文件,并且通过一些不同的选项,它会要求用户为它提供一个“框”。如果任何 JSON 文件框与此框发生碰撞、相交、叠加(无论如何),它将为所述框生成标题。有助于组织。
到目前为止一切都很好,我希望。我有两个类:Point 和 Box。 “Point”只是空间中的一个点,“Box”是一个盒子,或者说是一个矩形。看看下面:
import json
class Point:
"""A point identified by (x,y) coordinates."""
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y
def as_tuple(self):
"""(x, y)"""
return (self.x, self.y)
和 BoxClass:
class Box:
"""A box identified by two points.
The box stores left, top, right, and bottom values.
Coordinates are based on screen coordinates.
origin top
+-----> x increases |
| left -+- right
v |
y increases bottom
set_points -- reset box coordinates
contains -- is a point inside?
overlaps -- does a box overlap?
top_left -- get top-left corner
bottom_right -- get bottom-right corner
expanded_by -- grow (or shrink)
"""
def __init__(self, pt1, pt2):
"""Initialize a box from two points."""
self.set_points(pt1, pt2)
def set_points(self, pt1, pt2):
"""Reset the box coordinates."""
(x1, y1) = pt1.as_tuple()
(x2, y2) = pt2.as_tuple()
self.left = min(x1, x2)
self.top = min(y1, y2)
self.right = max(x1, x2)
self.bottom = max(y1, y2)
self.lt = Point(self.left, self.top) #left-top point
self.rb = Point(self.right, self.bottom) #right-bottom point
self.rt = Point(self.right, self.top) #right-top point
self.lb = Point(self.left, self.bottom) #left-bottom point
def overlaps(self, other):
"""Return true if a box overlaps with this box."""
if(self.lt.x > other.rb.x or other.lt.x > self.rb.x):
return False
if(self.rb.y < other.lt.y or other.rb.y < self.lt.y):
return False
return True
def __str__( self ):
return "<Box (%s,%s)-(%s,%s)>" % (self.left,self.top,
self.right,self.bottom)
现在,这是令人困惑的部分。我只是在测试这个,这里是一些代码:
""" Opens and loads the JSON file."""
with open("results.json") as f:
data = json.load(f)
captions = data["captions"]
boxes = data["boxes"]
lt = Point([0, 5000])
rt = Point([5000, 0])
rw = Box(lt, rt)
rx = Box(Point([130.05253601074, 375.44775390625]), Point([130.05253601074+119.79272460938, 375.44775390625+26.875732421875]))
print rw.overlaps(rx)
这会将“True”打印到控制台。 正如预期的那样。
现在,当我打印这个时:
caption_counter = 0
for c, b in zip(captions, boxes):
# This wraps around each individual caption, and each of the boxes for each caption (four elements inside the tuple)
x, y, x2, y2 = b[0], b[1], b[0] + b[2], b[1] + b[3]
lt = Point(x, y)
rb = Point(x2, y2)
dc_box = Box(lt, rb)
caption_counter +=1
if rw.overlaps(dc_box) or dc_box.overlaps(rw):
print c
counter += 1
print caption_counter
没有打印!什么都没有!我以这种非常奇怪的方式定义了 x,y,x2,y2,因为我只想确定输入到 dc_box 中的所有内容都是我想要通过的 100%。而且,我希望您看一下我包含的这个 else
语句(在未打印任何内容之后):
else:
print b
print dc_box.__str__
print "\n"
输出很多东西,但最后一项(基本上是最后一个框)是这样的:
[130.05253601074, 375.44775390625, 119.79272460938, 26.875732421875]
<bound method Rect.__str__ of Rect(Point(130, 375), Point(249, 402))>
是不是很眼熟?看看我的第一个测试!它与框“rx”的参数完全相同。它与框“rw”重叠。为什么框“rw”不与 dc_boc 重叠?这是 json file .
最佳答案
感谢user2357112 .我正在将一个列表传递给 Point
,而它应该取两个点,但我从来没有意识到这一点。我的 overlaps
函数也有错误。
关于python - 读取 JSON 值不是正确的评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46332027/