1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- import gc
- import cv2
- import imutils
- import threading
- from AIDetector_pytorch import Detector
- def start_camera_detector(camera_id, detector):
- name = 'Demo Camera {}'.format(camera_id)
- cap = cv2.VideoCapture(camera_id, cv2.CAP_V4L2)
- if not cap.isOpened():
- print('Error: Unable to open camera {}.'.format(camera_id))
- return
- cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
- cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 640)
- fps = cap.get(cv2.CAP_PROP_FPS)
- if fps <= 0:
- fps = 30
- t = int(1000 / fps)
- print(f'{name} fps:', fps)
- frame_count = 0
- while True:
- ret, im = cap.read()
- if not ret or im is None:
- break
- if frame_count % 3 == 0:
- result = detector.feedCap(im)
- result = result['frame']
- result = imutils.resize(result, height=500)
- cv2.imshow(name, result)
- if cv2.waitKey(t) & 0xFF == ord('q'):
- break
- frame_count += 1
- if frame_count % 30 == 0:
- gc.collect()
- cap.release()
- cv2.destroyWindow(name)
- def main():
- detector = Detector()
- threads = []
- for i in range(6): # camera 数量
- thread = threading.Thread(target=start_camera_detector, args=(i, detector))
- thread.start()
- threads.append(thread)
- for thread in threads:
- thread.join()
- if __name__ == "__main__":
- main()
|