Search code examples
pythonoopimage-processingpython-multithreading

python image process with multithreading problem


i have a smart door lock project with using mimic passwords and i can take mimic with camera i can detect fingers, smile, eyebrows etc. but all in one script. I want to use class things for it. I construct class build but i have to use multithreading because i have to run more than one method for detect all mimics in one cam. With my code, it opens 2 cam one by one, first opened cam and detect fingers and when i close it open another cam and detect face. how can i make it with one cam?

camera.py https://www.pythonmorsels.com/p/33trh/

FaceLandMarks.py https://www.pythonmorsels.com/p/28jmh/

HandLandMarks.py https://www.pythonmorsels.com/p/33ffw/

HandTrackingModule.py https://www.pythonmorsels.com/p/39jw2/

test.py https://www.pythonmorsels.com/p/3a2e5/


Solution

  • The simplest is to create camera outside classes and send it to classes as parameter

    captureMode = cv2.VideoCapture(0)
    
    FaceLandMarks(captureMode).run()
    HandLandMarks(captureMode).run()
    # etc.
    
    captureMode.release()
    cv2.destroyAllWindows()
    
    class LandMarksFace:  # without `Camera
        
        def __init__(self, camera):
    
            self.camera = camera
    
            # ... code ...
    
        def run(self):
    
            # ... code ...
    
            success, img = self.camera.read()
    
            # ... code ...
    

    And if you really need to use class Camera then create it in __init__ and set instace to all classes

    class Camera():
    
        def __init__(self):
            self.camera = cv2.VideoCapture(0)
    
        def getCamera(self):
            return self.camera
    
    cam = Camera()
    
    FaceLandMarks(cam).run()
    HandLandMarks(cam).run()
    # etc.
    
    cam.getCamera().release()
    cv2.destroyAllWindows()
    
    class LandMarksFace:  # without `Camera
        
        def __init__(self, camera):
    
            self.camera = camera
    
            # ... code ...
    
        def run(self):
    
            # ... code ...
    
            success, img = self.camera.getCamera().read()
    
            # ... code ...