I'm trying to create a program that knows what number is on an image with the following function:
def img_in_img(big_picture, small_picture, tamper):
big_picture = str(big_picture)
small_picture = str(small_picture)
if os.path.isfile(big_picture) == False or os.path.isfile(small_picture) == False:
return "Image does not exist"
img = cv2.imread(big_picture,0)
templace_loc = small_picture
template = cv2.imread(templace_loc,0)
w, h = template.shape[::-1]
method = cv2.TM_CCOEFF
tamper = int(tamper)
res = cv2.matchTemplate(img,template,method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
height, width, channels = cv2.imread(templace_loc).shape
if int(top_left[0]) < int(height + tamper) and int(top_left[0]) > int(height - tamper) and int(top_left[1]) < int(width + tamper) and int(top_left[1]) > int(width - tamper):
return True
else:
return False
But then when I check if 7.png
is in img.png
with the code
nur = "7"
if img_in_img("2020-01-14-17-36-08.537043/verification_image2.png", "verifynr/" + "old_orange" + "/" + nur + ".png", 25):
print(Style.BRIGHT + Fore.GREEN + "Color: " + "old_orange" + ", Num: " + nur + Fore.RESET)
else:
print(Style.BRIGHT + Fore.RED + "Color: " + "old_orange" + ", Num: " + nur + Fore.RESET)
it gives me in RED: Color: old_orange, Num: 7
but then if I check if 6.png
is in img.png
by changing nur
from 7
to 6
it gives me in Green: Color: old_orange, Num: 6
, but thats the wrong image.
I've also tried the following code:
img_rgb = cv2.imread("2020-01-14-17-36-08.537043/verification_image2.png")
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread('verifynr/old_orange/7.png',0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray,template,cv2.TM_SQDIFF)
threshold = 0.8
loc = np.where( res >= threshold)
pt = list(zip(*loc[::-1]))
if len(pt) >= 1:
print("True")
wich prints True
, but it does that for every number png I saved.
How do I make my program recognize the 7.png
in the img.png
without recognizing every single number png?
img.png:
6.png:
7.png:
See Question&Answers more detail:
os