├── 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 | best_match = None 29 | best_score = -float('inf') # For NCC, higher values are better 30 | 31 | template_mean = np.mean(template_array) 32 | template_std = np.std(template_array) 33 | 34 | for y in range(img_h - tmpl_h + 1): 35 | for x in range(img_w - tmpl_w + 1): 36 | sub_image = image_array[y:y+tmpl_h, x:x+tmpl_w] 37 | sub_image_mean = np.mean(sub_image) 38 | sub_image_std = np.std(sub_image) 39 | 40 | norm_sub_image = (sub_image - sub_image_mean) / sub_image_std 41 | norm_template = (template_array - template_mean) / template_std 42 | 43 | score = np.sum(norm_sub_image * norm_template) 44 | print(f"Matching region at ({x}, {y}) with score: {score}") 45 | 46 | # Save the best matching region for visualization 47 | if score > best_score: 48 | best_score = score 49 | best_match = (x, y) 50 | save_sub_image(image_array, (y, x), (tmpl_h, tmpl_w), 'best_match_region.jpg') 51 | 52 | return best_match, best_score 53 | 54 | # Provide the paths to your image and template 55 | image_path = "C:\\Users\\Lenovo\\Downloads\\download.jpeg" 56 | template_path = "C:\\Users\\Lenovo\\Downloads\\download (1).jpeg" 57 | 58 | # Load the image and template 59 | image = load_image(image_path) 60 | template = load_image(template_path) 61 | 62 | # Perform template matching 63 | best_match, best_score = match_template(image, template) 64 | 65 | # Display the result 66 | if best_match: 67 | print(f"Best match found at: {best_match} with score: {best_score}") 68 | else: 69 | print("No match found") 70 | print(f"Image size: {image.size}") 71 | print(f"Template size: {template.size}") 72 | 73 | 74 | 75 | 76 | 77 | 78 | output: 79 | print(f"Image size: {image.size}") 80 | print(f"Template size: {template.size}") 81 | 82 | 83 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Object-recognition-for-pre-trained-model --------------------------------------------------------------------------------