Back in 2022 I worked on a project with an ESP32 board with two sensors - an OV2640 optical camera and an MLX90640 low-resolution thermal sensor. The camera will give an MJPEG stream, and the thermal sensor returned a 32 x 24 array of temperatures on every HTTP request. On the server side, a Python app read the stream with OpenCV, ran a TensorFlow Lite object detection model on each frame, and showed the result in a small Flask UI. See the YouTube video i uploaded

The goal was to detect the trained object and sense its temperature. We initially had to train the target object, I started with a glass of hot water.

The model gives a bounding box in pixel coordinates on a 1024 x 768 frame (because with the camera - it is what is sees). But the temperature it detected is in a 32 x 24 grid coordinate space. So the real problem was this: if the object is at a certain point in the camera frame, which cell in the thermal grid is that same spot contaning the object.

The device: front of the box with both sensors, and the ESP32 board inside

The camera can recognise the glass but can’t measure heat. The thermal sensor measures heat but, at 32 x 24, we can’t tell which object’s temperature it’s giving as output - at this small resolution, unless we point it to right object, it can pick up other surrounding things . In the camera it might be (590, 482), and in the thermal grid it’s (18, 15). If both had the same resolution and were perfectly aligned, there would be nothing to convert.

The answer turned out to be a basic arithmetic problem. But it only clicked once I understood something I honestly did not know at the time: an image is just an array of numbers, Pixels are represented by numerical values in a NumPy array. This changed the whole prespective how i can approach the problem.

An image is a NumPy array

I was reading frames from the camera stream with OpenCV. When we call cap.read(), we get a NumPy ndarray. I’ve already explained this in my YouTube video uploaded at the time (mentioned above^).

Here, I’m reading the props of the captured frame.

success, frame = cap.read()
print(frame.shape)   # (768, 1024, 3)
print(frame.dtype)   # uint8

The frame is 768 rows tall and 1024 columns wide. Each element at a row and column is a pixel. Each pixel holds 3 values, one per colour channel. OpenCV stores them in BGR order (blue, green, red), not RGB. The dtype is uint8, so every value is an integer from 0 to 255.

If you print a small slice of the array, it looks like this:

row 0:  [12, 40, 200]  [13, 41, 198]  [15, 44, 190] ...
row 1:  [11, 39, 201]  [14, 42, 196]  [16, 45, 188] ...
row 2:  ...

That’s the whole image. Just Rows and columns of numbers.

  flowchart LR
    A[Camera stream] --> B["OpenCV cap.read()"]
    B --> C["NumPy ndarray<br/>shape (768, 1024, 3)<br/>uint8, BGR"]

Once this clicked for me, a lot of things got simpler. If an image is an array, then a position in the image is just an index: a row and a column. And you can do plain arithmetic on indexes.

The thermal sensor also returns an array

So the thermal sensor gives me data in a much rawer form. Every request returned one long comma-separated string of 768 values. Each value is a single temperature reading from the whole frame.

A flat list of 768 numbers doesn’t mean much on its own. But the sensor sees the scene as a grid of 24 rows and 32 columns, and 24 x 32 is exactly 768. So I parsed the string and reshaped it into a 2D array. Just to format and do some ops on it.

temps = np.array([float(v) for v in raw.split(",")])   # shape (768,)
grid = temps.reshape(24, 32)                            # shape (24, 32)

reshape doesn’t change the data. It just tells NumPy how to read the same 768 numbers: fill row 0 with the first 32 values, row 1 with the next 32, and so on. This is called row-major order.

Below is just the same glass water temperature in degrees, reshaped (as above) from the flat 768 comma seperated values.

row 0:  [84.1, 84.3, 84.0, 85.2, ...]
row 1:  [84.0, 84.4, 86.8, 81.5, ...]
row 2:  ...

So now I had two arrays describing the same scene.

  flowchart LR
    S[Same scene] --> C[Optical camera]
    S --> H[Thermal sensor]
    C --> G1["frame<br/>(768, 1024, 3)<br/>colour"]
    H --> G2["grid<br/>(24, 32)<br/>temperature"]

Plotting it on X and Y

When the detection model finds an object, it returns a bounding box. Let me conclude what we’re doing again:

  1. The camera gives a full 1024 x 768 image.
  2. The detection model looks at that full image and returns the bounding box as two corner points, for example (529, 409) and (651, 555)
  3. We take the centre of the box, which is (590, 482).
  4. We divide those two numbers by 32 and get (18, 15).
  5. We read the temperature at row 15, column 18 of the thermal array: grid[15, 18].
  • Width: the photo is 1024 pixels wide, split into 32 columns. Each column is 1024 ÷ 32 = 32 pixels wide.
  • Height: the photo is 768 pixels tall, split into 24 rows. Each row is 768 ÷ 24 = 32 pixels tall.

Camera plane, 1024 x 768, with the bounding box and its centre point

x = (529 + 651) // 2 = 590
y = (409 + 555) // 2 = 482

So the object is at (590, 482) on the camera plane. Now we need the same point on the thermal plane.

Our premise is that the centre of the glass will tell the water/glass tempeature. But we can change to any point of an object depends on what’s needs to be done.

Coordinate mapping

At this point we have two things: a point on the camera plane, (590, 482), and the scale factor between the two planes, 32. Now we need to make these points to agree upon a point where we’ll get the temp w.r.t the thermal sensor.

My first instinct was that I’d have to resize something. Maybe upscale the thermal grid to 1024 x 768 so it matched the camera frame. But that wouldn’t give me any new information. The sensor only measured 768 temperatures. Upscaling would just fill in made-up values between the real readings.

The better move was the other way round. I didn’t need to resize either array. I only needed to scale the coordinates down. Just divide by the scale factor and drop the decimals. In Python that’s floor division, //:

x = 590 // 32 = 18
y = 482 // 32 = 15

So the camera point (590, 482) lands on thermal cell (18, 15). Here it is on the 32 x 24 plane. I’ve also scaled the box corners the same way, (16, 12) to (20, 17), so you can see where the object sits:

Thermal plane, 32 x 24, with the scaled-down box and the centre cell

Each small square is one temperature reading. The box that covered thousands of pixels on the camera plane now covers a handful of cells.

The last step is to read that cell. One thing to watch: NumPy indexes as [row, column], which is [y, x]. So y comes first.

temperature = grid[15, 18]   # row 15, column 18

That’s the object’s temperature.

  flowchart TD
    A["Camera frame<br/>1024 x 768"] --> B[Object detection]
    B --> C["Bounding box<br/>(529, 409) to (651, 555)"]
    C --> D["Centre point<br/>(590, 482)"]
    D --> E["Floor divide by 32<br/>(18, 15)"]
    E --> F["grid[15, 18]"]
    F --> G[Temperature]

Calibration: Incase if you need

So far, we assumed that the camera and the thermal sensor see exactly the same view, with the same (0, 0) corner and the same area. But as the image i’ve shown of the device, both are in different positions - top one is thermal sensor, below that the camera. It’s a 3D printed box, roughly 8 x 5 cm. So the two are looking at the scene from two slightly different spots, and at slightly different angles.

So after dividing by 32, we land roughly on the right cell, but not exactly. Here’s one of the captured frames. The red box is the detection after dividing by 32. The glass’s heat, the white spot, sits a little below and to the left of it. (The plot is smoothed for display; the sensor still only gives 768 readings.) The dark yelloish is where the glass with hot water is.

The 32 x 24 thermal grid with a scaled-down bounding box drawn on it

Calibration means measuring that offset once and correcting for it every time. In my code it was just two numbers in a config file, tuned by hand:

YSHIFT = 3   # rows
XSHIFT = 1   # columns

temperature = grid[y + YSHIFT, x - XSHIFT]

After the division, move 3 rows down and 1 column left, to where the glass really shows up in the thermal grid. For our example, that moves the reading from grid[15, 18] to grid[18, 17]. This is something you need to keep in mind if you’re trying this.

Final thoughts

I never thought that I’d need some math for this to get to this point. The idea of downscaling coordinates came from Stack Overflow and other sources, and it really helped. Completing the project left me feeling motivated and proud of what I’d achieved. It took almost a month to get here, from training the object-detection model and building the frontend to setting everything up and getting the final readings. While what i’ve written might seem like a toy problem - this is a small step of solving much more complex problems out there. Let me know if you’re going to try or have any questions! :)