-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotate.py
84 lines (79 loc) · 3.08 KB
/
annotate.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
import cv2
lst=[]
box_width=0
box_height=0
centerx=0
centery=0
click=0
#Function to draw bounding rectangle using mouse clicks
'''
def click_event(event, x, y, flags, params):
global lst,box_width,box_height,centerx,centery,click
# checking for left mouse clicks
if event == cv2.EVENT_LBUTTONDOWN:
# displaying the coordinates
# on the Shell
print('(',x, ',',y,')')
lst.append([x,y])
click+=1
# displaying the coordinates
# on the image window
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, '(' +str(x) + ',' +str(y)+ ')' , (x,y), font,1, (255, 0, 0), 2)
if(click%2==0):
cv2.rectangle(img, (lst[0][0],lst[0][1]), (lst[1][0],lst[1][1]), (255,240,0), thickness=3)
box_width=lst[1][0]-lst[0][0]
box_height=lst[1][1]-lst[0][1]
centerx=lst[0][0]+box_width/2
centery=lst[0][1]+box_height/2
cv2.imshow('image', img)
with open('dog.txt','w')as f:
f.write('0'+' '+str(centerx)+' '+str(centery)+' '+str(box_width)+' '+str(box_height))
'''
#Function to draw bounding rectangle by dragging the mouse
def click_event(event, x, y, flags, params):
global lst,box_width,box_height,centerx,centery,click
# checking for left mouse clicks
if event == cv2.EVENT_LBUTTONDOWN:
#if the left mouse button was clicked record the starting coordinates
# displaying the coordinates
# on the Shell
print('(',x, ',',y,')')
lst.append([x,y])
# displaying the coordinates
# on the image window
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, '(' +str(x) + ',' +str(y)+ ')' , (x,y), font,1, (255, 0, 0), 2)
elif event==cv2.EVENT_LBUTTONUP:
#check if the left mouse button was released
#record the ending coordinates
lst.append([x,y])
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, '(' +str(x) + ',' +str(y)+ ')' , (x,y), font,1, (255, 0, 0), 2)
if(click%2==0):
cv2.rectangle(img, (lst[0][0],lst[0][1]), (lst[1][0],lst[1][1]), (255,240,0), thickness=3)
box_width=lst[1][0]-lst[0][0]
box_height=lst[1][1]-lst[0][1]
centerx=lst[0][0]+box_width/2
centery=lst[0][1]+box_height/2
cv2.imshow('image', img)
with open('dog.txt','w')as f:
f.write('0'+' '+str(centerx)+' '+str(centery)+' '+str(box_width)+' '+str(box_height))
# driver function
if __name__=="__main__":
# reading the image
img = cv2.imread('dog.jpg', 1)
# displaying the image
cv2.imshow('image', img)
# setting mouse handler for the image
# and calling the click_event() function
cv2.setMouseCallback('image', click_event)
# wait for a key to be pressed to exit
cv2.waitKey(0)
# close the window
cv2.destroyAllWindows()
print(lst)
print("box_width:",box_width)
print("box_height:",box_height)
print("center_x:",centerx)
print("center_y:",centery)