0

I have a long program (script: face.py, main function: livevideo()).

  • upon running the program, it shows webcam live video (display 1),
  • I press 'm' to pause + capture webcam image + add detected faces in the webcam image into an averaged face (display 2).

face.py program works well with 2 python windows for the 2 displays.

Now, I am building a simple user interface (script: interface.py). I want to fit the 2 displays into 2 QLabel/ QFrame panels in the interface. I am very new to PyQt, and I am really confused by examples with PyQt code endlessly inter-referencing.

In interface.py, I have already imported face.livevideo. I would prefer to modify only interface.py and leave face.py untouched. How can I do it? Thanks.

face.py (hundreds of lines of code): livevideo() (showing only relevant lines)

def livevideo():
    cap=cv2.VideoCapture(0)
        cv2.imshow("Live Video",image)
        if cv2.waitKey(1)==ord('m'):
            break
# lines for face averaging process are excluded
    cv2.imshow("Averaged Face",output)
    cv2.waitKey(1)

interface.py:

import sys
import face
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class HelloWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        menu = self.menuBar().addMenu('Add Frames to Averaged Face')
        action1 = menu.addAction('Start Webcam')
        action1.triggered.connect(face.livevideo)

        liveVideoImage = QLabel(self)
        liveVideoImage.setGeometry(50,100,500,375)
        liveVideoImage.setFrameShape(QFrame.Panel)
        averagedFaceOutput = QLabel(self)
        averagedFaceOutput.setGeometry(600,100,375,375)
        averagedFaceOutput.setFrameShape(QFrame.Panel)

if __name__ == "__main__":
    def run_app():
        app = QApplication(sys.argv)
        mainWin = HelloWindow()
        mainWin.show()
        app.exec_()
    run_app()
Syd Xyn
  • 1
  • 1
  • @eyllanesc, your recommended prior question and answer is not the same. While there is only one script containing everything in your recommended post, here I have two scripts and I am asking how to bridge them. – Syd Xyn May 01 '18 at 00:35
  • Is that the 2 scripts can not be joined because you can not have a different window than the one that PyQt handles because the event loops would be blocked. I have marked it as a duplicate because your final goal is to show the result of opencv in the PyQt window, which is answered in the other question, in conclusion, your current approach is incorrect. – eyllanesc May 01 '18 at 01:10
  • @eyllanesc, you mentioned that "my current approach is incorrect", yet you didn't guide me with the steps to convert it into "your right way". I have tried merging two scripts with no success, and the combined script turned out really messy when every function was added "self". There is a way to do it separately in 2 scripts. Isn't it a good practice to organize interface and main program (hundreds of lines of code) in separate scripts? You did not want to answer my question, yet you shut down the opportunities for others to help with this particular question. – Syd Xyn May 01 '18 at 14:48
  • I never said that separating the code was bad, what it indicates is that you should not use the windows created by opencv through imshow with the windows created by PyQt, both will be locked between them. – eyllanesc May 01 '18 at 14:56
  • What you must do is perform the task of opencv in another thread and send the information to the main thread by signals. Separating files has nothing to do with good practices, programming does not interest the files, but the things that were created in them, that is, the classes, functions, etc. – eyllanesc May 01 '18 at 14:56
  • Many beginners think that ordering a project is just to separate parts of a giant code in several files, and that is not true, the software is like a living being, not only is cutting one leg and sending it to another file. What you must do is separate your code into classes that receive something and produce something, similar to a factory that receives certain materials and produces others. And then in the main file create an object of that kind, and at that moment you pass the inputs and collect what it produces to perform the main task. – eyllanesc May 01 '18 at 14:57
  • @eyllanesc thanks for elaboration. can i confirm with you the main idea? There is no easy way to just fit images in python display windows into pyqt display panels? – Syd Xyn May 01 '18 at 15:38
  • I will create an example where the camera is read and faces detected (this will be done in a different file) and I will send them to the main file. What I just described is not correct because as I told you a software is not a set of files but for practical questions it is useful, but this will take a little time, I will notify you when I have it. – eyllanesc May 01 '18 at 15:43
  • @eyllanesc noted with thanks :) – Syd Xyn May 03 '18 at 01:44

0 Answers0