-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopencv.py
224 lines (193 loc) · 8.45 KB
/
opencv.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtGui import QImage
import cv2, imutils
import time
import numpy as np
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
import numpy as np
import cv2
import pyshine as ps
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(498, 522)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName("gridLayout_2")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("images/H.png"))
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.verticalSlider = QtWidgets.QSlider(self.centralwidget)
self.verticalSlider.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider.setObjectName("verticalSlider")
self.gridLayout.addWidget(self.verticalSlider, 0, 0, 1, 1)
self.verticalSlider_2 = QtWidgets.QSlider(self.centralwidget)
self.verticalSlider_2.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider_2.setObjectName("verticalSlider_2")
self.gridLayout.addWidget(self.verticalSlider_2, 0, 1, 1, 1)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1)
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setAlignment(QtCore.Qt.AlignCenter)
self.label_3.setObjectName("label_3")
self.gridLayout.addWidget(self.label_3, 1, 1, 1, 1)
self.horizontalLayout.addLayout(self.gridLayout)
self.gridLayout_2.addLayout(self.horizontalLayout, 0, 0, 1, 2)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout_2.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_2.addWidget(self.pushButton_2)
self.gridLayout_2.addLayout(self.horizontalLayout_2, 1, 0, 1, 1)
spacerItem = QtWidgets.QSpacerItem(313, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout_2.addItem(spacerItem, 1, 1, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.verticalSlider.valueChanged['int'].connect(self.brightness_value)
self.verticalSlider_2.valueChanged['int'].connect(self.blur_value)
self.pushButton_2.clicked.connect(self.loadImage)
self.pushButton.clicked.connect(self.savePhoto)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
# Added code here
self.filename = 'Snapshot '+str(time.strftime("%Y-%b-%d at %H.%M.%S %p"))+'.png' # Will hold the image address location
self.tmp = None # Will hold the temporary image for display
self.brightness_value_now = 0 # Updated brightness value
self.blur_value_now = 0 # Updated blur value
self.fps=0
def loadImage(self):
""" This function will load the camera device, obtain the image
and set it to label using the setPhoto function
"""
cam = True # True for webcam
if cam:
vid = cv2.VideoCapture(0)
else:
vid = cv2.VideoCapture('video.mp4') # place path to your video file here
cnt=0
frames_to_count=20
st = 0
fps=0
while(vid.isOpened()):
QtWidgets.QApplication.processEvents()
img, self.image = vid.read()
self.image = imutils.resize(self.image ,height = 480 )
gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.15,
minNeighbors=7,
minSize=(80, 80),
flags=cv2.CASCADE_SCALE_IMAGE)
for (x, y, w, h) in faces:
cv2.rectangle(self.image, (x, y), (x + w, y + h), (10, 228,220), 5)
if cnt == frames_to_count:
try: # To avoid divide by 0 we put it in try except
print(frames_to_count/(time.time()-st),'FPS')
self.fps = round(frames_to_count/(time.time()-st))
st = time.time()
cnt=0
except:
pass
cnt+=1
self.update()
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
def setPhoto(self,image):
""" This function will take image input and resize it
only for display purpose and convert it to QImage
to set at the label.
"""
self.tmp = image
image = imutils.resize(image,width=640)
frame = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = QImage(frame, frame.shape[1],frame.shape[0],frame.strides[0],QImage.Format_RGB888)
self.label.setPixmap(QtGui.QPixmap.fromImage(image))
def brightness_value(self,value):
""" This function will take value from the slider
for the brightness from 0 to 99
"""
self.brightness_value_now = value
print('Brightness: ',value)
self.update()
def blur_value(self,value):
""" This function will take value from the slider
for the blur from 0 to 99 """
self.blur_value_now = value
print('Blur: ',value)
self.update()
def changeBrightness(self,img,value):
""" This function will take an image (img) and the brightness
value. It will perform the brightness change using OpenCv
and after split, will merge the img and return it.
"""
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv)
lim = 255 - value
v[v>lim] = 255
v[v<=lim] += value
final_hsv = cv2.merge((h,s,v))
img = cv2.cvtColor(final_hsv,cv2.COLOR_HSV2BGR)
return img
def changeBlur(self,img,value):
""" This function will take the img image and blur values as inputs.
After perform blur operation using opencv function, it returns
the image img.
"""
kernel_size = (value+1,value+1) # +1 is to avoid 0
img = cv2.blur(img,kernel_size)
return img
def update(self):
""" This function will update the photo according to the
current values of blur and brightness and set it to photo label.
"""
img = self.changeBrightness(self.image,self.brightness_value_now)
img = self.changeBlur(img,self.blur_value_now)
# Here we add display text to the image
text = 'FPS: '+str(self.fps)
img = ps.putBText(img,text,text_offset_x=20,text_offset_y=30,vspace=20,hspace=10, font_scale=1.0,background_RGB=(10,20,222),text_RGB=(255,255,255))
text = str(time.strftime("%H:%M %p"))
img = ps.putBText(img,text,text_offset_x=self.image.shape[1]-180,text_offset_y=30,vspace=20,hspace=10, font_scale=1.0,background_RGB=(228,20,222),text_RGB=(255,255,255))
text = f"Brightness: {self.brightness_value_now}"
img = ps.putBText(img,text,text_offset_x=80,text_offset_y=425,vspace=20,hspace=10, font_scale=1.0,background_RGB=(20,210,4),text_RGB=(255,255,255))
text = f'Blur: {self.blur_value_now}: '
img = ps.putBText(img,text,text_offset_x=self.image.shape[1]-200,text_offset_y=425,vspace=20,hspace=10, font_scale=1.0,background_RGB=(210,20,4),text_RGB=(255,255,255))
self.setPhoto(img)
def savePhoto(self):
""" This function will save the image"""
self.filename = 'Snapshot '+str(time.strftime("%Y-%b-%d at %H.%M.%S %p"))+'.png'
cv2.imwrite(self.filename,self.tmp)
print('Image saved as:',self.filename)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "PyShine video process"))
self.pushButton_2.setText(_translate("MainWindow", "Start"))
self.label_2.setText(_translate("MainWindow", "Brightness"))
self.label_3.setText(_translate("MainWindow", "Blur"))
self.pushButton.setText(_translate("MainWindow", "Take picture"))
# Subscribe to PyShine Youtube channel for more detail!
# WEBSITE: www.pyshine.com
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())