我正在开发一个人脸识别webapp,但我遇到了一些问题,试图“修复”图像上的边界框使其响应。如果屏幕的大小是(正确的)结果:
但是一旦我尝试调整窗口浏览器的大小,使其变小,盒子就会失去它的位置:
.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>
在这种情况下EDIT Box属性:
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”是原始图片的大小,coords是以像素为单位的坐标。
由于您使用静态单位定位框,因此您的代码无法产生所需的效果。随着图片变小,盒子总是从顶部192px,从右边85px等。尝试使用百分比单位而不是相似(显然使用数字直到看起来正确):
top: 30%;
right: 5%;
left: 5%;
bottom: 15%;