-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_balls.py
executable file
·194 lines (157 loc) · 6.67 KB
/
add_balls.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# python3 add_balls.py
# adds rectangles on top of given plates
"""
1)Read selected picture
2) make rectangle to it by mouse lef button
3) by right mouse button save selected rectangle in given accuracy in pixels,
4 go for the next picture (goto 1) ).
Finnish car number plate: 118 mm x 442 mm
try here 20x80 pixel
"""
import sys
from PyQt5.QtWidgets import (QMainWindow, QTextEdit,
QAction, QFileDialog, QApplication, QWidget, QVBoxLayout, QPushButton, QLabel)
from PyQt5.QtGui import QIcon
import numpy as np
import cv2
from matplotlib import pyplot as plt
class MouseRectangle():
""" get a rectangle by mouse"""
def __init__(self):
#super().__init__()
self.refPt = []
self.cropping = False
self.image = None
def set_image(self, image):
self.image = image
def reset(self):
self.refPt = []
self.cropping = False
def get_refPt(self):
return self.refPt
def plot_xy(self,event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDBLCLK:
print("xy", x, y)
def click_and_crop(self,event, x, y, flags, param):
# grab references to the global variables
# if the left mouse button was clicked, record the starting
# (x, y) coordinates and indicate that cropping is being
# performed
if event == cv2.EVENT_LBUTTONDOWN:
self.refPt = [(x, y)]
self.cropping = True
# check to see if the left mouse button was released
elif event == cv2.EVENT_LBUTTONUP:
# record the ending (x, y) coordinates and indicate that
# the cropping operation is finished
self.refPt.append((x, y))
self.cropping = False
# draw a rectangle around the region of interest
cv2.rectangle(self.image, self.refPt[0], self.refPt[1], (255, 255, 255), -2)
cv2.imshow("image", self.image)
class Example(QWidget):
def __init__(self):
super().__init__()
self.mouse = MouseRectangle()
layout = QVBoxLayout()
self.btn = QPushButton("QFileDialog static method demo")
self.btn.clicked.connect(self.showDialog())
layout.addWidget(self.btn)
self.le = QLabel("Hello")
layout.addWidget(self.le)
self.btn1 = QPushButton("QFileDialog object")
self.btn1.clicked.connect(self.showDialog())
layout.addWidget(self.btn1)
self.contents = QTextEdit()
layout.addWidget(self.contents)
self.setLayout(layout)
self.setWindowTitle("File Dialog demo")
def getNewName(self, oldname, subdir='img'):
"""
get new filename with extra path
"""
import os
#mydir = os.path.dirname(oldname)
mydir = os.getcwd()
name = os.path.basename(oldname)
#generate new dir if it doesnot exist
newdir = mydir + '/'+ subdir
print("newdir:", newdir)
if not os.path.exists(newdir):
os.makedirs(newdir)
return newdir+'/'+'sample_'+name
def showDialog(self):
import glob
import math
fnames =glob.glob('*jpg')
fnames = fnames + glob.glob('*png')
for fname in fnames:
try:
img = cv2.imread(fname,0)
print("size:", img.shape[0], img.shape[1])
print(fname)
# cv2.namedWindow('image')
if (img.shape[0] > 1000):
img = cv2.resize(img, (int(img.shape[0]/2), int(img.shape[1]/2)))
clone = img.copy()
self.mouse.set_image(image=img)
cv2.imshow('image', img)
#cv2.setMouseCallback('image', plot_xy)
#cv2.setMouseCallback('image', self.mouse.plot_xy)
cv2.setMouseCallback('image', self.mouse.click_and_crop)
#plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
#plt.xticks([]), plt.yticks([]) # to hide tick values on X,Y axis
#plt.show()
# keep looping until the 'd or c' key is pressed
mydelete = False
myprint = False
while True:
# display the image and wait for a keypress
cv2.imshow("image", img)
key = cv2.waitKey(1) & 0xFF
# if the 'r' key is pressed, reset the cropping region
if key == ord("r"):
img = clone.copy()
self.mouse.reset()
self.mouse.set_image(image=img)
# if the 'c' key is pressed, break from the loop
elif key == ord("c"):
break
# if the 'k' key is pressed, keep the original
elif key == ord("k"):
myprint = True
break
# if the 'd' key is pressed, do not write image (it is virtually deleted)
elif key == ord("d"):
mydelete = True
break
# if there are two reference points, then crop the region of interest
# from teh image and display it
refPt = self.mouse.get_refPt()
newname = self.getNewName(fname,'HumanProcessed')
if not(mydelete):
print(newname)
if myprint:
print("writing original:", newname)
cv2.imwrite(newname,clone)
elif len(refPt) >= 2:
center = (int((refPt[0][0]+refPt[1][0])/2), int((refPt[0][1]+refPt[1][1])/2))
radius = int(0.5*math.sqrt(\
(refPt[1][1]-refPt[0][1])*\
(refPt[1][1]-refPt[0][1])+\
(refPt[1][0]-refPt[0][0])*\
(refPt[1][0]-refPt[0][0])))
print("writing with added rectangle :", newname)
#cv2.rectangle(clone, refPt[0], refPt[1], (255, 255, 255), -2)
cv2.circle(clone, center, radius, (255, 255, 255), thickness=-2)
cv2.imwrite(newname,clone)
else:
print("not writing:", newname)
except:
print ("skipping: ", fname)
# close all open windows
cv2.destroyAllWindows()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())