Threading issue w/ imread + matchTemplate

Greetings,

Given a base image, I would like to search it for multiple sub-images.
To accomplish this, I built an array with all of my .jpg filenames, and called cv.matchTemplate on each sub-image against the base image.

Run synchronously, the code works perfectly, but slowly due to the large volume of sub-images. Therefore, I called the ThreadPoolExecutor to speed things up. (I also tried multiprocessing, but both had the same problem)

Using threading for this task, I got a bunch of errors, specifically:

  1. Premature end of JPEG file
  2. Corrupt JPEG data: premature end of data segment
  3. Corrupt JPEG data: bad Huffman code
  4. Corrupt JPEG data: extraneous bytes before marker 0xd9

It’s like the code runs “too quickly,” not accurately reading the file then going onto the next one, and then matchTemplate doesn’t work as a result. Is there a better way to do this?

In general, what would be the best approach to quickly scan a base image for a high volume of sub-images and get the coordinates of each?

show code, please :wink:

snippet of code added, thanks for your time :slight_smile:

ah, NOOO, text, not an IMAGE of code, pleeeaaase

First time signing up for a code forum LOL sorry XD

 import cv2
 import myTools as mt
 import concurrent.futures
 # import time
 #import threading
 
 
 folder = r"C:\Users\KingKek\.spyder-py3\projects\testcv2\images"
 ext = ".jpg"
 images = mt.collectFiles(ext, folder)
 confidence = 0.8
 imgToSearch = cv2.imread('imgToSearch.jpg', cv2.IMREAD_UNCHANGED)
 
 def findImg(imgToFind):
     result = cv2.matchTemplate(imgToSearch, cv2.imread(imgToFind, cv2.IMREAD_UNCHANGED), cv2.TM_CCOEFF_NORMED)
     min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
     if max_val >= confidence:
         print(max_loc)
     else:
         print("not found")
   
 
 with concurrent.futures.ThreadPoolExecutor() as executor:
    executor.map(findImg, images)
 
 print("finished threading operation")
 
 # n = 0
 # for image in images:
 #     imgToFind2 = cv2.imread(image, cv2.IMREAD_UNCHANGED)
 #     mt.scanBackground(imgToFind2, confidence)
 #     n += 1 
 # print(n)

edit: Also, the commented part at the bottom works–I would just like to speed this whole operation up rather than looping through the images 1 by 1.

Ty again for taking a look!

1 Like

replacing
images = mt.collectFiles(ext, folder)
with
images = glob.glob(folder)

it works fine as expected, using .png files.
might be a problem with the jpg decoder

1 Like

ya know… i figured python should have an integrated way to dump the contents of a folder. Zzz

anyways, it works now! The jpg thing is still weird to me, but it’s easy enough to work with pngs instead. You rock! :joy:

1 Like