OpenCV 3 - Python 2.7 Anaconda - USB Camera aquire only black frames after a while

时间:2017-06-19 14:04:33

标签: python multithreading opencv image-processing

I am currently using Python 2.7 (Anaconda 4.4.0 - Windows 7) and OpenCV (3.2.0) to perform some online tracking tasks. While with a test video things work ok, I have a major issue when switching to on-line camera recording (a DMK 23UP1300).

I realized quickly that I needed to thead the process that was acquiring the frames, and now the program is split in half: the first class video_stream is a thread that opens the communication to the camera and puts the coming frames in a Queue().

#video_stream.py

import threading
import cv2

class video_stream(threading.Thread):
    def __init__(self, frame_queue, src = 0, DEBUG = False):
        threading.Thread.__init__(self)
        self.frames_out = frame_queue
        self.DEBUG = DEBUG
        self.kill = False

    def run(self):
        if self.DEBUG:
            self.cap = cv2.VideoCapture("test.avi")
        else:
            self.cap = cv2.VideoCapture(0)
            self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1024)  # Picture width
            self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 768)  # Picture height
            self.cap.set(cv2.CAP_PROP_FPS, 25)  # FPS
            self.cap.set(cv2.CAP_PROP_EXPOSURE, -6.0)  # Exposure
        print '[THREAD - Starting video stream]'
        while not self.kill:
            ret, frame_raw = self.cap.read()
            self.frames_out.put(frame_raw)
        print '[THREAD - Stopping video stream]'

and the main.py is in charge of taking the frames from the Queue and process them:

#main.py

import cv2
from Queue import Queue
from video_stream import video_stream

frame_queue = Queue()
video_stream_thread = video_stream(frame_queue, DEBUG = False);
video_stream_thread.start()

while(True):
    # Capture frame-by-frame from the Queue
    gray = frame_queue.get()

    # PROCESS THE FRAME HERE

    cv2.imshow('frame', gray)

    if cv2.waitKey(10) & 0xFF == ord('q'):
        video_stream_thread.kill = True;
        break

cv2.destroyAllWindows()

By doing this I see the first few frames (usually around ~25/30) and then the acquired frame becomes completely black. Did any of you experience something similar? This kind of behaviour was manifesting itself also when I did not use the thread to acquire the frames.

thanks for any advice!!

0 个答案:

没有答案