Soon I learned to capture videos through the webcam using Python with the OpenCV library. After that, I had the idea of making a script that automatically starts and stops recording a video. Once the program started, the capture would start, but would only begin to record if the captured frame satisfied certain condition I created (based on a detection function I did). Video recording would automatically terminate when the captured frames did not contain what the detection function is programmed to detect. The detector function returns True if the image satisfies my condition and False if it does not satisfy. The script is as follows:
def webvideo(path):
import sys,cv2
begin=False
cap=cv2.VideoCapture(0)
if not cap.isOpened():
print('Não foi possível abrir a web cam.')
sys.exit(-1)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(path,fourcc, 20.0, (640,480))
while True:
ret, frame = cap.read()
if ret:
cv2.imwrite('temp.jpg',frame)
if not begin:
if funcao_detectora('temp.jpg'):
begin=True
else:
if not funcao_detectora('temp.jpg'):
break
if begin:
out.write(frame)
else:
break
cap.release()
out.release()
webvideo('teste.avi')
The problem is that the detector function reads each frame, which takes a while. Thus, it takes a while for the next frame to be captured and the video looks like a sequence of fairly time-spaced photos. Question: How do I run the detecting function in a process other than the process of recording the frames so that the frames are recorded without interruption?
NOTE: It is not necessary to apply the multithreading knowledge to solve this specific case, but if someone shows an example that I understand and can apply to my script, I will also accept it.