Home>

I have 3 files. The first is responsible for the webcam and displays an image, also starts a stream and writes data to a global variable.

from threading import Thread
import cv2
from pushData import push
from data import *
def webcam ():
    global gFrame
    cap= cv2.VideoCapture (0)
    # Start stream
    p1= Thread (target= push, args= (getFrame,))
    p1.start ()
    while True:
        # Take picture from camera
        ret, frame= cap.read ()
        # Copy frame
        gFrame= frame.copy ()
        # Display image
        cv2.imshow ('frame', frame)
        if cv2.waitKey (1) &
 0xFF== ord ('q'):
            break
    cap.release ()
    cv2.destroyAllWindows ()
if __name__== "__main__":
    webcam ()

There is a file just for storing information

# Storage space
gFrame= []
def getFrame ():
    global gFrame
    return gFrame

And the whole problem is flowing. When I try to take data from variables, they are empty. That is, print (getFrame ()) outputs an empty array

import time
def push (getFrame):
    while True:
        print ()
        # Outputs a frame
        print (getFrame ())
        time.sleep (1)
        print ("===================")

and show us the place in which the gFrame variable changes somehow

alex2021-02-24 00:12:10

in 1 file in a loop. gFrame= frame.copy ()

L4legenda2021-02-24 00:12:10
  • Answer # 1
    from threading import Thread
    import cv2
    from pushData import push
    from data import *
    def webcam ():
        cap= cv2.VideoCapture (0)
        # Start stream
        p1= Thread (target= push, args= (getFrame,))
        p1.start ()
        while True:
            # Take picture from camera
            ret, frame= cap.read ()
            # Copy frame
            gFrame [0]= frame.copy ()
            # Image output
            cv2.imshow ('frame', frame)
            if cv2.waitKey (1) &
     0xFF== ord ('q'):
                break
        cap.release ()
        cv2.destroyAllWindows ()
    if __name__== "__main__":
        webcam ()
    

    list is a mutable data type. and take away global everywhere.

    # Storage space
    gFrame= [None]
    def getFrame ():
        return gFrame