#python #opencv
Вопрос:
У меня есть две функции, speak
и main
.
внутри main
у меня есть
cap = cv2.VideoCapture(0)
для потоковой передачи видео и Я хочу, чтобы во время потока можно было вызывать функцию speak
но бывает, что видео замирает или приостанавливается во время работы функции speak, пожалуйста, помогите мне использовать многопроцессорную обработку, чтобы функция speak не нарушала видеопоток
import cv2 import datetime import imutils import numpy as np import import pyttsx3 import time import playsound import subprocess import sys import multiprocessing protopath = "MobileNetSSD_deploy.prototxt" modelpath = "MobileNetSSD_deploy.caffemodel" detector = cv2.dnn.readNetFromCaffe(prototxt=protopath, caffeModel=modelpath) engine = pyttsx3.init() def speak(): playsound.playsound('Door.mp3') engine.say("Welcome to Bread of life church") engine.say("please stand in front of the camera") engine.runAndWait() engine.stop() voices = engine.getProperty('voices') engine.setProperty('rate', 165) engine.setProperty('voice', voices[1].id) CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"] def main(): cap = cv2.VideoCapture(0) fps_start_time = datetime.datetime.now() fps = 0 total_frames = 0 counter = 0 while True: ret, frame = cap.read() frame = imutils.resize(frame, width=800) total_frames = total_frames 1 (H, W) = frame.shape[:2] blob = cv2.dnn.blobFromImage(frame, 0.007843, (W, H), 127.5) detector.setInput(blob) person_detections = detector.forward() for i in np.arange(0, person_detections.shape[2]): confidence = person_detections[0, 0, i, 2] if confidence gt; 0.5 and counter == 0 : idx = int(person_detections[0, 0, i, 1]) speak() time.sleep(5) if CLASSES[idx] != "person": continue person_box = person_detections[0, 0, i, 3:7] * np.array([W, H, W, H]) (startX, startY, endX, endY) = person_box.astype("int") cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 0, 255), 2) else: counter = 0 fps_end_time = datetime.datetime.now() time_diff = fps_end_time - fps_start_time if time_diff.seconds == 0: fps = 0.0 else: fps = (total_frames / time_diff.seconds) fps_text = "FPS: {:.2f}".format(fps) cv2.putText(frame, fps_text, (5, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1) cv2.putText(frame, f'Total Persons : {1}', (40,70), cv2.FONT_HERSHEY_DUPLEX, 0.8, (255,0,0), 2) cv2.imshow("Application", frame) key = cv2.waitKey(1) if key == ord('q'): break cv2.destroyAllWindows() main()