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
已放弃
复制代码

这就对了

posted @   叕叒双又  阅读(532)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2019-10-17 更加方便的使用git上传自己的代码
2019-10-17 win10下子系统的倒腾记录
2019-10-17 世间有一种坏
2019-10-17 单纯指望运动减肥的是几乎不可能的?
2017-10-17 模块化编程实例(一)
点击右上角即可分享
微信分享提示