Я использую классификатор haar для обнаружения объектов и использую 3 потока для канала ip-камеры. Первый поток (класс VideoGet), второй поток (класс Classify) и третий поток (класс VideoShow). Однако замечено, что в программе активны только два потока, в то время как три потока обнаружения no twe ie активны только один раз в начале программы, и все. пожалуйста, помогите мне исправить. код ниже:
from threading import Thread
import threading
import cv2,time
class VideoGet:
def __init__(self, src=0):
# global capture
self.capture=cv2.VideoCapture(src)
#self.capture.set(cv2.CAP_PROP_BUFFERSIZE, 1)
self.stopped=False
(self.status, self.frame) = self.capture.read()
# FPS = 1/X
# X = desired FPS
#self.FPS = 1/10
#self.FPS_MS = int(self.FPS * 1000)
def start(self):
# Start frame retrieval thread
Thread(target=self.update, args=()).start()
# self.thread.daemon = True
return self
def stop(self):
self.stopped=True
self.capture.release()
cv2.destroyAllWindows()
def update(self):
while not self.stopped:
if not self.status:
self.stop()
else:
(self.status, self.frame) = self.capture.read()
print("Video Read Thread")
#self.frame=cv2.resize(self.frame,None,fx=0.25,fy=0.25,interpolation=cv2.INTER_AREA)
#time.sleep(self.FPS)
# def show_frame(self):
# cv2.imshow('frame', self.frame)
# self.gray=cv2.cvtColor(self.frame,cv2.COLOR_BGR2GRAY)
#self.body_detect_coord=self.body_detect.detectMultiScale(self.gray,1.2,3)
#for (x,y,w,h) in self.body_detect_coord:
# cv2.rectangle(self.frame,(x,y),(x+w,y+h),(0,255,0),2)
#cv2.waitKey(self.FPS_MS)
# if cv2.waitKey(1)==27:
# self.capture.release()
# cv2.destroyAllWindows()
# self.thread_flag=0
class Cascade:
def __init__(self,src= None,body_detect=None):
self.capture=cv2.VideoCapture(src)
#self.capture.set(cv2.CAP_PROP_BUFFERSIZE, 1)
self.stopped=False
(self.status, self.frame) = self.capture.read()
#self.frame=frame
self.body_detect=body_detect
# self.stopped=False
def start(self):
Thread(target=self.classify, args=()).start()
return self
def classify(self):
self.gray=cv2.cvtColor(self.frame,cv2.COLOR_BGR2GRAY)
print("Classify Thread")
#self.body_detect_coord=self.body_detect.detectMultiScale(self.frame,1.3,4)
#for (x,y,w,h) in self.body_detect_coord:
# cv2.rectangle(self.frame,(x,y),(x+w,y+h),(0,255,0),2)
# time.sleep(1)
if cv2.waitKey(1) == 27:
self.stopped = True
def stop(self):
self.stopped = True
class VideoShow:
def __init__(self, frame=None):
self.frame = frame
#self.body_detect=cv2.CascadeClassifier('C:/Users/Mahesh/Pictures/human_features/haarcascade_fullbody.xml')
#if self.body_detect.empty():
# raise IOError('Unable to read haarcascade_fullbody.xml file')
self.stopped = False
def start(self):
Thread(target=self.show, args=()).start()
return self
def show(self):
while not self.stopped:
cv2.imshow("Video", self.frame)
#self.gray=cv2.cvtColor(self.frame,cv2.COLOR_BGR2GRAY)
#self.body_detect_coord=self.body_detect.detectMultiScale(self.gray,1.2,3)
#for (x,y,w,h) in self.body_detect_coord:
# cv2.rectangle(self.frame,(x,y),(x+w,y+h),(0,255,0),2)
if cv2.waitKey(1) == 27:
self.stopped = True
print("VideoShow Thread")
def stop(self):
self.stopped = True
if __name__ == '__main__':
# global thread_flag
src = 'rtsp://admin:admin@123@192.168.1.240:554/cam/realmonitor?channel=1&subtype=0'
body_detect=cv2.CascadeClassifier('C:/Users/Mahesh/Pictures/human_features/haarcascade_fullbody.xml')
video_getter = VideoGet(src).start()
video_classify = Cascade(src,body_detect).start()
video_shower = VideoShow(video_getter.frame).start()
while True:
try:
#threaded_camera.show_frame()
if video_getter.stopped or video_shower.stopped or video_classify.stopped:
video_shower.stop()
video_getter.stop()
video_classify.stop()
break
frame=video_getter.frame
video_classify.frame=frame
video_shower.frame=video_classify.frame
except AttributeError:
print("Error")
pass
video_getter.capture.release()
cv2.destroyAllWindows()