-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
51 lines (38 loc) · 1.5 KB
/
utils.py
File metadata and controls
51 lines (38 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import cv2
# Helper Method to calculate midpoit between two coordinates
def midpoint(p1, p2):
return int((p1.x + p2.x)/2), int((p1.y + p2.y)/2)
# Cheking if the image provided in the request is valid or not
def is_valid_image(file):
try:
# Check if the file has an allowed extension (e.g., 'jpg', 'jpeg', 'png', etc.)
allowed_extensions = {'jpg', 'jpeg', 'png'}
file_extension = file.filename.rsplit('.', 1)[1].lower()
if file_extension not in allowed_extensions:
return False
return True
except Exception:
return False
def template_matching(image_path,template_path):
img = cv2.resize(cv2.imread(image_path, 0), (0, 0), fx=0.8, fy=0.8)
template = cv2.imread(template_path, 0)
# Preprocess the template by applying guassian blur
# template = cv2.GaussianBlur(template, (5, 5), 0)
h, w = template.shape
# Choose
method = cv2.TM_CCORR_NORMED
img2 = img.copy()
result = cv2.matchTemplate(img2, template, method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
# threshold for matching
threshold = 0.8
if max_val > threshold:
location = max_loc
bottom_right = (location[0] + w, location[1] + h)
cv2.line(img2, location, bottom_right, 255, 5)
# cv2.rectangle(img2, location, bottom_right, 255, 5)
cv2.imshow('Match', img2)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
print("Template not found")