12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- # encoding: utf-8
- # This file create two thread, t1 capturing rstp video and save frame every interval, t2 read frame from database and
- # post to https server.
- import cv2
- import time
- import LogOut
- from mysqlDataBase import MysqldbOperational
- import configparser
- def capture_video(ipAddr, cameraName):
- try:
- rtspAddr = ipAddr
- # print(rtspAddr)
- cap = cv2.VideoCapture(rtspAddr)
- cap.set(cv2.CAP_PROP_BUFFERSIZE, 0)
- ret, image = cap.read()
- while ret:
- time.sleep(0.2)
- cv2.imwrite('%s.jpg' % cameraName, image)
- print('%s.jpg' % cameraName)
- break
- except Exception as e:
- print('read %s error!' % ipAddr, cameraName)
- _logger.error(e)
- return e
- def read_camera_configuration():
- list_camera = cameraSql.select('camera_configuration_table',
- fields=["camera_name", "camera_ip", "camera_pass", "order_no", "camera_id"])
- return list_camera
- def frequency_capture(frequency):
- lastTime = 0
- while True:
- newTime = int(time.time())
- time.sleep(1)
- if lastTime + frequency <= newTime:
- camera_list = read_camera_configuration()
- for x in range(0, len(camera_list)):
- capture_video(camera_list[x][1], camera_list[x][0])
- lastTime = newTime
- if __name__ == '__main__':
- time.sleep(10)
- _logger = LogOut.Log('CPcapture')
- # 创建读取配置文件对象
- config = configparser.ConfigParser()
- config.read("config.ini", encoding="utf-8")
- # 获取通用配置项
- section = "General" # 读取的section标签
- mysql_host = config.get(section, 'mysqlHost')
- mysql_username = config.get(section, 'mysqlUsername')
- mysql_password = config.get(section, 'mysqlPassword')
- mysql_port = config.getint(section, 'mysqlPort')
- # 获取特有配置项
- section = "CPcapture" # 读取的section标签
- mysql_database = config.get(section, 'mysqlDatabase')
- # 连接数据库
- cameraSql = MysqldbOperational(host=mysql_host,
- username=mysql_username,
- password=mysql_password,
- port=mysql_port,
- database=mysql_database,
- logger=_logger)
- frequency_capture(20)
|