Tips for adding a bounding box to an image received from the server

I've got a python server that is returning a collection of bounding boxes post OCR processing (using Tesseract or similar).

"bbox": [{
          "x1": 223,
          "y1": 426,
          "x2": 1550,
          "y2": 1736
        }..]

Currently, I am trying to display this bounding box on the client side using the Q-img tag and incorporating a div tag within it as follows:

 <q-img :src="imageData.src">
        <div
          style="
            position: absolute;
            background-color: rgba(0, 100, 0, 0.1);
            left: 223px;
            top: 426px;
            height: 1310px;
            width: 1327px;
            border: 0.5px solid blue;
          "
        />
      </q-img>

The issue I'm facing is that the drawn bounding box on the client mismatches with the actual location of the object shown in the image returned by the server.

Answer №1

It appears that there is a discrepancy in the coordinate systems used between your Python server's bounding boxes and the coordinate system utilized by the Q-img tag on your frontend.

To address this issue, ensure that the image dimensions provided by the server (imageData.width and imageData.height) align with the actual dimensions of the image displayed by the Q-img tag. Any resizing or scaling done on the client-side could potentially disrupt the coordinates.

In addition, verify whether the server yields bounding box coordinates based on the image dimensions (such as percentages) or in absolute pixels. Currently, your CSS styles are set to use absolute pixel values. If the server supplies absolute pixel coordinates and your div tag requires relative positioning, you will need to calculate the percentages:

  left: ((box.x1 / imageData.width) * 100) + '%', 
  top: ((box.y1 / imageData.height) * 100) + '%', 
  width: ((box.x2 - box.x1) / imageData.width) * 100) + '%', 
  height: ((box.y2 - box.y1) / imageData.height) * 100) + '%'

Be sure to log both the bounding box coordinates received from the server and the computed styles of the div element in your browser's console using console.log. Compare the two sets of data to identify any discrepancies. Utilize your browser's developer tools to inspect the image and overlaying div, confirming if the div tag is positioned correctly and has the accurate dimensions. Further investigation may be required.

If assuming that your server returns coordinates as percentages ranging from 0.0 to 1.0, consider modifying the template as follows:

<q-img :src="imageData.src">
  <div v-for="box in bboxes" :key="box.x1"
       style="
         position: absolute;
         background-color: rgba(0, 100, 0, 0.1);
         left: {{ box.x1 * 100 }}%; 
         top: {{ box.y1 * 100 }}%;
         width: {{ (box.x2 - box.x1) * 100 }}%;
         height: {{ (box.y2 - box.y1) * 100 }}%;
         border: 0.5px solid blue;
       "
  >
  </div>
</q-img>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best approach to reverse the selection of <li> elements by utilizing :not() and :contains

I am looking to implement a live search feature using jQuery. Below is the code snippet I have written: $("#searchInput").on("keyup", function () { var searchTerm = $("#searchInput").val(); $('li:contains("' + searchTerm + ' ...

What is the process for making an Ajax request in Rails?

I'm attempting to send a variable through jQuery using the POST method, saving it in a controller, and then utilizing that same variable in Rails HTML to query a table. It seems like the variable isn't being passed to the controller. jQuery: v ...

Launching a centered pop-up window to display a submitted form message

I am attempting to create a page that displays a confirmation message in a center-aligned pop-up window after the user submits a form. I have managed to open the page in a pop-up window, but I am struggling to center the window using my current code (I pre ...

Click anywhere outside the sidemenu to close it

I want the menu to behave like the side menu on Medium. When the side menu is open and the user clicks outside of #sidebar-wrapper, the side menu should close. Currently, I have to click the toggle X to close the menu. html <a id="menu-toggle" href="# ...

Transform the blob data into an Excel file for easy download, but beware the file is not accessible

My API returns a response containing the content of an excel file, which is displayed in the image below. Now, I am looking to convert this content into an excel file and make it downloadable for the user. Below is the API function: [HttpGet] [Ign ...

Developing a React-based UI library that combines both client-side and server-side components: A step-by-step

I'm working on developing a library that will export both server components and client components. The goal is to have it compatible with the Next.js app router, but I've run into a problem. It seems like when I build the library, the client comp ...

Tips on how to automatically rearrange a jQuery UI sortable list

Currently, I am working with 2 jQuery UI sortable lists which can be found at http://jqueryui.com/demos/sortable/#connect-lists. The process involves dragging items from list A (catalog) to list B (basket). I have a specific requirement where I need the ...

Tips for stopping Angular from rendering a directive that is producing incorrect results after an error is thrown

My directives have a template and a compile function that modifies the template. Occasionally, Angular fails to recognize jQuery (and defaults to using jqLite), causing my compile to encounter errors. Despite this, Angular continues to render the directive ...

JSX Script issue: the input prompt is missing

Greetings! I am currently working on a script to export JSON files from Adobe Illustrator. // Custom function for exporting prefixed objects function exportPrefixedObjects(doc) { // User input for prefixes and reference points var prefixInput = prompt( ...

Unable to display elements from an array in the dropdown menu generated by v-for

Having just started learning Vue.js, I am facing a challenge in rendering the following array: countries: ["US", "UK", "EU" ] I want to display this array in a select menu: <select> <option disabled value="">Your Country</option& ...

Leveraging useContext to alter the state of a React component

import { createContext, useState } from "react"; import React from "react"; import axios from "axios"; import { useContext } from "react"; import { useState } from "react"; import PermIdentityOutlinedIcon f ...

Exploring the new features of utilizing buttons with the onClick method in the updated nextJS version 14.1.3

"implement customer" import React, { useState } from "react"; import { FaChevronLeft, FaChevronRight } from "react-icons/fa"; export default function HeroSlider() { const images = [ "/images/homepage/home-1.jpeg&qu ...

Is it possible to customize the close icons on the autocomplete feature in Material UI?

Is there a solution to change the icon while keeping its function when clicked? I am looking to replace this Icon <Autocomplete multiple id="checkboxes-tags-demo" options={top100Films} disableCloseOnSelect getOpt ...

How to prevent selecting future dates in Material UI date picker

Is there a way to prevent selecting future dates in the material UI datepicker? I noticed that it doesn't seem to have any prop options like disableFuture or past. For those interested, here's the link to the github repository sandboxlink ...

Would this code effectively disable the right-clicking menu for MathJax?

My current approach involves utilizing the following code snippet: <script type="tet/x-mathjax-config"> MathJax.Hub.Config({ showMathMenu: false }); </script> I intended for this code to disable the right-click menu on my math webs ...

Encountering an error in Vue.js Meteor: Unable to read properties of undefined when trying to access 'find'

Recently, I've been following a tutorial at However, even when trying to install the default Vue Meteor app, I encounter an error. meteor: { $subscribe: { 'experiments': [], }, experiments () { return Experiments.find({}).fetch(); }, } ...

Error: Vue Prop is undefined and cannot be utilized within a v-for loop when using TypeScript and decorators

Hey there, I'm currently utilizing Vue along with typescript and facing an issue with props in v-for where it's not rendering anything. Check out the code snippet below for reference I've experimented with computed props, setting default va ...

Display an image when the link is hovered over within a table

I'm looking for a way to display a scanned receipt image when hovering over a link within a table. I've written some code but it's not working as expected. Can anyone help me figure out what's wrong? Here is the current state of my cod ...

Switching Primary Menu Selection When Scrolling Through Various Menu Options

I need help with changing the active class of links on my single page scrolling website. I found some useful code snippets for smooth scrolling and updating the active class on scroll or click events: $(document).ready(function () { $(document).on(" ...

Incorporate attributes into object properties in a dynamic manner

Currently, I am experimenting with bootstrap-multiselect and my goal is to incorporate data attributes into the dataprovider method. Existing Data Structure: var options = [ {label: 'Option 1', title: 'Option 1', value: ' ...