python中执行其他的python脚本(一):
代码main.py:
1 from PyQt5 import QtCore, QtGui, QtWidgets 2 import faceSet 3 4 5 class Ui_MainWindow(object): 6 def setupUi(self, MainWindow): 7 MainWindow.setObjectName("MainWindow") 8 MainWindow.resize(519, 354) 9 self.centralwidget = QtWidgets.QWidget(MainWindow) 10 self.centralwidget.setObjectName("centralwidget") 11 self.pushButton = QtWidgets.QPushButton(self.centralwidget) 12 self.pushButton.setGeometry(QtCore.QRect(210, 140, 75, 23)) 13 self.pushButton.setObjectName("pushButton") 14 MainWindow.setCentralWidget(self.centralwidget) 15 self.retranslateUi(MainWindow) 16 QtCore.QMetaObject.connectSlotsByName(MainWindow) 17 def retranslateUi(self, MainWindow): 18 _translate = QtCore.QCoreApplication.translate 19 MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) 20 self.pushButton.setText(_translate("MainWindow", "Open")) 21 22 self.pushButton.clicked.connect(self.OpenClick) 23 24 def OpenClick(self): 25 #algorithm.FunctionAlgo() 26 faceSet.mySet() 27 28 29 30 if __name__ == "__main__": 31 import sys 32 app = QtWidgets.QApplication(sys.argv) 33 MainWindow = QtWidgets.QMainWindow() 34 ui = Ui_MainWindow() 35 ui.setupUi(MainWindow) 36 MainWindow.show() 37 sys.exit(app.exec_())
代码faceSet.py:
1 '''' 2 Capture multiple Faces from multiple users to be stored on a DataBase (dataset directory) 3 ==> Faces will be stored on a directory: dataset/ (if does not exist, pls create one) 4 ==> Each face will have a unique numeric integer ID as 1, 2, 3, etc 5 Based on original code by Anirban Kar: https://github.com/thecodacus/Face-Recognition 6 Developed by Marcelo Rovai - MJRoBot.org @ 21Feb18 7 ''' 8 9 import cv2 10 import os 11 12 def mySet(): 13 cam = cv2.VideoCapture(0) 14 cam.set(3, 640) # set video width 15 cam.set(4, 480) # set video height 16 17 face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') 18 19 # For each person, enter one numeric face id 20 face_id = input('\n enter user id end press <return> ==> ') 21 22 print("\n [INFO] Initializing face capture. Look the camera and wait ...") 23 # Initialize individual sampling face count 24 count = 0 25 26 while(True): 27 28 ret, img = cam.read() 29 img = cv2.flip(img, 1) # flip video image vertically 30 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 31 faces = face_detector.detectMultiScale(gray, 1.3, 5) 32 33 for (x,y,w,h) in faces: 34 35 cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2) 36 count += 1 37 38 # Save the captured image into the datasets folder 39 cv2.imwrite("dataset/User." + str(face_id) + '.' + str(count) + ".jpg", gray[y:y+h,x:x+w]) 40 41 cv2.imshow('image', img) 42 43 k = cv2.waitKey(100) & 0xff # Press 'ESC' for exiting video 44 if k == 27: 45 break 46 elif count >= 30: # Take 30 face sample and stop video 47 break 48 49 # Do a bit of cleanup 50 print("\n [INFO] Exiting Program and cleanup stuff") 51 cam.release() 52 cv2.destroyAllWindows()
运行:
弹出一个有运行按键的界面,单击界面,执行我需要的程序;
结果为:
enter user id end press <return> ==> QCoreApplication::exec: The event loop is already running 1 [INFO] Initializing face capture. Look the camera and wait ... OpenCV(3.4.1) Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/pi/opencv-3.4.1/modules/imgproc/src/color.cpp, line 11147 Traceback (most recent call last): File "main.py", line 29, in OpenClick faceSet.mySet() File "/home/pi/pro1/faceSet.py", line 30, in mySet gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.error: OpenCV(3.4.1) /home/pi/opencv-3.4.1/modules/imgproc/src/color.cpp:11147: error: (-215) scn == 3 || scn == 4 in function cvtColor 已放弃
这就对了
人就像是被蒙着眼推磨的驴子,生活就像一条鞭子;当鞭子抽到你背上时,你就只能一直往前走,虽然连你也不知道要走到什么时候为止,便一直这么坚持着。