-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlandmark.py
119 lines (94 loc) · 3.47 KB
/
landmark.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os
import time
import cv2 as cv
import pandas as pd
from save_csv import save
class Main:
def __init__(self, imagePath, w_name='image',image_name=None, iteration=False):
self.iteration = iteration
self.image_name = image_name
self.imagePath = imagePath
self.windowName = w_name
self.font = cv.FONT_HERSHEY_SIMPLEX
self.line_type = cv.LINE_AA
self.count = 0
self.color = (255, 255, 255)
self.co_ordinate = []
cv.namedWindow(self.windowName)
# Create a track bar for the point
cv.createTrackbar('colorP', self.windowName, 0, 3, self.trackbar)
# Callback function for trackbar that select the color
def trackbar(self, x):
print(x)
if x == 0:
self.color = (100, 0, 0)
elif x == 1:
self.color = (255, 0, 0)
elif x == 2:
self.color = (0, 255, 0)
elif x == 3:
self.color = (0, 0, 255)
else:
self.color = (255, 255, 255)
def run(self):
while True:
#Reading the image
self.image = cv.imread(self.imagePath, 1)
#Resizing the image if it's not (512, 512)
if self.image.shape[0] != 200 and self.image.shape[1] !=200:
self.image = cv.resize(self.image, (200, 200))
print('Shape: {}'.format(self.image.shape))
# Showing image
cv.imshow(self.windowName, self.image)
#Mouse call back
self.mouseCallback()
#Waiting for response
response = self.waitkey()
if response:
break
def mouseCallback(self):
cv.setMouseCallback(self.windowName, self.onclick)
def onclick(self, event, x, y, flags, param):
if event == cv.EVENT_LBUTTONDOWN:
print('Left Button: {}, {}'.format(x, y))
#Saving the coordinate in temporory list
self.co_ordinate.append((x, y))
print(f'Co-ordinates: {self.co_ordinate}')
# Show the point in image
self.image = cv.circle(self.image, (x, y), 2, self.color, -1)
# Showing the images
self.showImage(self.image)
def waitkey(self):
key = cv.waitKey(0)
if key == 27:
quit()
elif key == ord('r'):
self.reset()
else:
# This will save co-ordinates in csv
if self.co_ordinate:
save(self.image_name, self.co_ordinate, True if self.iteration == 0 else False)
cv.destroyAllWindows()
return True
def showImage(self, image):
cv.imshow(self.windowName, image)
def reset(self):
self.co_ordinate.clear()
print('Finished Reset!')
if __name__ == "__main__":
# imagesPath = input('ImageDir: ')
# savePath = input('SaveDir: ')
# print(os.listdir('../Scale/images'))
print("""
Instruction: 'r' for reset.
Any key for next.
Esc for exit.
""")
time.sleep(2)
for i, image in enumerate(os.listdir('Upload_image_here')):
ext = ['jpg', 'jpeg', 'png']
file_split = image.split('.')
if len(file_split) == 2:
if file_split[1] in ext:
print('Image: {}'.format(image))
Main(os.path.join(os.getcwd(), f'Upload_image_here/{image}'), iteration = i, image_name = image).run()