我正在开发人脸识别网络应用程序,但在尝试“修复”图像上的边界框使其响应时遇到了一些问题。如果屏幕是全尺寸的,那是(正确的)结果:
但是,一旦我尝试调整窗口浏览器的大小,使其更小,框就会像这样失去它的位置:
.image-container {
position: relative;
display: inline-block;
img {
display: block;
max-width: 100%;
height: auto;
margin-left: auto;
margin-right: auto;
}
}
.box {
display: flex;
flex-wrap: wrap;
justify-content: center;
position: absolute;
border: 2px solid red;
}
<div>
{this.setRecognitionType()}
<div className={styles["image-container"]}>
{this.state.box ? (
<div
className={styles.box}
style={{
top: this.state.box.top,
right: this.state.box.right,
bottom: this.state.box.bottom,
left: this.state.box.left
}}
/>
) : null}
<img id="image" src={this.props.imageUrl} alt="" />
</div>
</div>
在这种情况下,编辑框的属性:
top: 192px;
right: 85px;
bottom: 118px;
left: 92px;
编辑:解决方案正如已经回答的那样,唯一的方法似乎是使用百分比而不是 px。
我整理的方式:
left: (coords.left / imageWidth) * 100,
top: (coords.top / imageHeight) * 100,
right: ((width - coords.right) / imageWidth) * 100,
bottom: ((height - coords.bottom) / imageHeight) * 100
“imageHeight”和“imageWidth”是原始图片的大小,坐标是以像素为单位的坐标。