├── Object Recognition for Pre-trained model.py └── README.md /Object Recognition for Pre-trained model.py: -------------------------------------------------------------------------------- 1 | from PIL import Image 2 | import numpy as np 3 | 4 | def load_image(image_path): 5 | return Image.open(image_path).convert('L') # Convert to grayscale 6 | 7 | def image_to_array(image): 8 | return np.array(image) 9 | 10 | def normalize_image(image_array): 11 | mean = np.mean(image_array) 12 | std = np.std(image_array) 13 | return (image_array - mean) / std 14 | 15 | def save_sub_image(image_array, top_left, size, file_name): 16 | y, x = top_left 17 | h, w = size 18 | sub_image = image_array[y:y+h, x:x+w] 19 | Image.fromarray(sub_image).save(file_name) 20 | 21 | def match_template(image, template): 22 | image_array = normalize_image(image_to_array(image)) 23 | template_array = normalize_image(image_to_array(template)) 24 | 25 | img_h, img_w = image_array.shape 26 | tmpl_h, tmpl_w = template_array.shape 27 | 28 | # Use FFT to compute the correlation 29 | correlation = np.fft.fft2(image_array) * np.conj(np.fft.fft2(template_array, s=image_array.shape)) 30 | correlation = np.fft.ifft2(correlation).real # Inverse FFT to get the correlation result 31 | 32 | # Normalize the correlation map 33 | correlation = correlation / (tmpl_h * tmpl_w) # Adjust for the template size 34 | 35 | # Find the best match 36 | best_match = np.unravel_index(np.argmax(correlation), correlation.shape) 37 | best_score = correlation[best_match] 38 | 39 | # Save the best matching region for visualization 40 | save_sub_image(image_array, best_match, (tmpl_h, tmpl_w), 'best_match_region.jpg') 41 | 42 | return best_match, best_score 43 | 44 | # Provide the paths to your image and template 45 | image_path = "C:\\Users\\Lenovo\\Downloads\\download.jpeg" 46 | template_path = "C:\\Users\\Lenovo\\Downloads\\download (1).jpeg" 47 | 48 | # Load the image and template 49 | image = load_image(image_path) 50 | template = load_image(template_path) 51 | 52 | # Perform template matching 53 | best_match, best_score = match_template(image, template) 54 | 55 | # Display the result 56 | if best_match: 57 | print(f"Best match found at: {best_match} with score: {best_score}") 58 | else: 59 | print("No match found") 60 | print(f"Image size: {image.size}") 61 | print(f"Template size: {template.size}") 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Object-recognition-for-pre-trained-model --------------------------------------------------------------------------------