-1

I am trying to make a game where the user submits and image and then that image gets compared to a hard coded template. If the images are close enough then the user wins. I coded this segment about 3 months ago, and at that time it ran perfectly, however now it just refuses to run. I keep getting these two warnings: Cannot find reference 'imread' in '__init__.py' and Cannot find reference 'TM_CCOEFF_NORMED' in '__init__.py' here is my code:

from tkinter import *
from mainwin import level
import tkinter as tk
from PIL import Image, ImageTk
import cv2
from tkinter import filedialog
import numpy as np

template = "D:\Imagechecks\c12.png"
# ^ hardcoded in ^

root = tk.Tk()
# makes a window for the file selection
root.title("Image Selector")

# Open file dialog to select image
file_path = filedialog.askopenfilename()

# Open image using Pillow
image = Image.open(file_path)

# Start Tkinter event loop

img = cv2.imread(image)
#           ^^the problem is pointing here
temp = cv2.imread(template)
#           ^^here
resolution = cv2.matchTemplate(img, temp, cv2.TM_CCOEFF_NORMED)
#                                                 ^^ and here.
threshold = .6
loc = np.where(resolution >= threshold)
print(resolution)
if len(loc[0]) == 0:
    labl_results = Label(root, text="LEVEL FAILED YOU LOOSER!").grid(row=12, column=0, columnspan=2)
    print("fail")
labl_results = Label(root, text="CONGRANTS. YOU DONE IT! ").grid(row=12, column=0, columnspan=2)
print("pass")
root.mainloop()

when I tried to fix it, the code gave me a different error: TypeError: Can't convert object to 'str' for 'filename' any help would be greately appreciated

I have tried:

  • reinstalling pycharm
  • reinstalling python 3.10.1
  • making a new file and copy pasting the code
  • re-installing all of the packages
  • usign the black and white version of cv2
  • running it on a friend's computer. none of these worked, and they all just gave the same answer: problem with imread and TM_CCOEFF...
  • 3
    Is that your real code? Where are your imports? Also, you are reading the image with (I guess PIL) into an image object and then you pass that to imread, which of course is wrong because imread expects an image path as a string. – stateMachine May 09 '23 at 06:22
  • 1
    "usign the black and white version of cv2" -- what on earth is that supposed to mean? – Dan Mašek May 09 '23 at 09:39
  • imageGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) templateGray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY) resolution = cv2.matchTemplate(imageGray, templateGray, cv2.TM_CCOEFF_NORMED) – toyota Supra May 09 '23 at 19:14

1 Answers1

0
imageGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
templateGray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)

resolution = cv2.matchTemplate(imageGray, templateGray, cv2.TM_CCOEFF_NORMED)

result = cv2.matchTemplate(imageGray, templateGray,
    cv2.TM_CCOEFF_NORMED)

Change this:

if len(loc[0]) == 0:
    labl_results = Label(root, text="LEVEL FAILED YOU LOOSER!").grid(row=12, column=0, columnspan=2)
    print("fail")
labl_results = Label(root, text="CONGRANTS. YOU DONE IT! ").grid(row=12, column=0, columnspan=2)
print("pass")

to:

if len(loc[0]) == 0:
    results = "LEVEL FAILED YOU LOOSER!" 
    print("fail")
else:
    results = "CONGRANTS. YOU DONE IT! "


labl_results = Label(root, text=result)
labl_results.grid(row=12, column=0, columnspan=2) 
toyota Supra
  • 3,181
  • 4
  • 15
  • 19