一、功能
1、实现多个摄像头同时获取图像,并保存到个各自目录
2、摄像头类别包括海康RSTP协议网络摄像头和普通USB摄像头
3、可自动和手动保存图片
二、注意事项
1.python可以使用multiprocessing多进程读取多个摄像头,但是multiprocessing自带的队列在交换数据时内存不会释放(没搞明白),所以使用了python threading多线程配合python自带的队列queue交换数据。
三、代码与演示
"""
@ capture multiple cameras images to different folder
"""
import cv2
import numpy as np
import time
import os
import threading
from time import ctime,sleep
import queue
abspath = os.path.abspath(os.path.dirname(__file__))
class threadCameraRSTP (threading.Thread):
"""Hikvision camera
@user User name.
@passwd User password.
@ip Camera ip name.
@queue Output queue.
"""
def __init__(self, user, passwd, ip, queue):
threading.Thread.__init__(self)
self.user = user
self.passwd = passwd
self.ip = ip
self.q = queue
def run(self):
access = "rtsp://%s:%s@%s//Streaming/Channels/1" % (self.user, self.passwd, self.ip)
cap = cv2.VideoCapture(access)
if cap.isOpened():
print('camera ' + self.ip + " connected.")
while True:
ret, img = cap.read()
if ret:
self.q.put(img)
self.q.get() if self.q.qsize() > 2 else time.sleep(0.01)
class threadCameraUSB (threadin