비디오 스트림의 각 프레임에 함수를 적용하기 위해 병렬 스레드를 실행하려면 어떻게해야합니까?

삼바 브 쿠마르

플라스크로 응용 프로그램을 만들려고합니다.

  • 웹캠에서 비디오 스트림 캡처
  • 각 프레임에 물체 감지 알고리즘 적용
  • 위의 기능에서 제공하는 경계 상자와 데이터가있는 프레임을 비디오 형태로 동시에 표시

문제는 함수 연산으로 인해 결과가 약간의 지연으로 렌더링된다는 것입니다. 이를 극복하기 위해 다른 스레드에서 함수를 실행하려고했습니다. 비디오의 각 프레임에 대해 함수를 실행하고 결과 프레임을 비디오 형식으로 표시하는 데 도움을 주시겠습니까? 내 주요 기능은 다음과 같습니다.

def detect_barcode():
global vs, outputFrame, lock


total = 0

# loop over frames from the video stream
while True:
    frame = vs.read()
    frame = imutils.resize(frame, width=400)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (7, 7), 0)
    timestamp = datetime.datetime.now()
    cv2.putText(frame, timestamp.strftime(
        "%A %d %B %Y %I:%M:%S%p"), (10, frame.shape[0] - 10),
        cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
    if total > frameCount:
        # detect barcodes in the image
        barcodes = md.detect(gray)

        # check to see if barcode was found
        if barcode in barcodes:
            x,y,w,h = cv2.boundRect(barcode)
        cv2.rectangle(frame,x,y,(x+w),(y+h),(255,0,0),2)
    total += 1
    # lock
    with lock:
        outputFrame = frame.copy()
if __name__ == '__main__':

# start a thread that will perform motion detection
t = threading.Thread(target=detect_barcode)
t.daemon = True
t.start()
app.run(debug=True)

vs.stop()
파스칼

나는 말 그대로 이런 일을했습니다. 내 솔루션은 asyncio의 concurrent.futures 모듈이었습니다.

import concurrent.futures as futures

기본적으로 실행기를 만들고 차단 방법을 매개 변수로 '제출'합니다. 모든 프레임의 결과를 확인할 수있는 Future라는 것을 반환합니다.

executor = futures.ThreadPoolExecutor()

집행자를 초기화합니다

future_obj = executor.submit(YOUR_FUNC, *args)

실행을 시작합니다. 언젠가 당신은 그 상태를 확인할 수 있습니다

if future_obj.done():
    print(future_obj.result())
else:
    print("task running")
    # come back next frame and check again

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관