'Keep an thread that contains an infinite loop, that updates a variable, and another thread that contains a timer that closes both threads when it ends
I need a main while() loop, which updates the screenshot frame all the time, but when I get to a part of the code because the sync needs to be very precise, what I need is to create 2 threads or subprocess (I think using subprocess is better in this case).
One that keeps updating the frames and the other thread or subprocess that makes a delay of 3 seconds, only then to start working with the last frame that was updated (because of this delay it is so important to wait for the frames to be updated).
This is my code:
import multiprocessing
import time
import cv2
import numpy as np
#library for Optical Character Recognition (OCR)
import pytesseract #pip install pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract'
# Create a VideoCapture object
cap = cv2.VideoCapture(1)
# Check if camera opened successfully
if (cap.isOpened() == False):
print("Unable to read camera feed")
.
# We convert the resolutions from float to integer.
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
while(True):
ret, frame = cap.read()
if ret == True:
#HERE SHOULD BE THE FORK IN 2 INDEPENDENT PROCESSES
text = pytesseract.image_to_string(frame) #OCR in subprocess_2
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break #Close main loop
This is the flowchart of how the program should work. Showing how the subprocess_1 repeats updating the value of the variable frame until subprocess_2 finishes executing (in this particular case, in principle I plan to try 3 seconds delay).
I thought of using a separate function but I'm really having trouble implementing it. I would also like to know if it is possible to implement all frame updates in a single loop while() .
def handle_frame_requests(conn1):
try:
while True:
request = conn1.recv()
conn1.send(frame) # The frame must be pickle-able
except EOFError:
pass
def capture_cam(conn1):
global frame
frame = None
Thread(target=handle_frame_requests, args=(conn1,), daemon=True).start()
cap = cv2.VideoCapture(1) #the same webcam
if (cap.isOpened() == False):
print("Unable to read camera!")
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
while(True):
ret, frame = cap.read() #here load the frame variable
if ret == True:
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'): break
else:
break
But regardless of how many nested while loops I use, the problem is that I can't get one process to keep updating the webcam, while another process keeps a timer, so that when the timer indicates it, both processes will exit and return to the webcam. main line, where the main while loop can continue to receive data from the webcam via the first loop while(True):
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

