CPcapture.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # encoding: utf-8
  2. # This file create two thread, t1 capturing rstp video and save frame every interval, t2 read frame from database and
  3. # post to https server.
  4. import cv2
  5. import time
  6. import LogOut
  7. from mysqlDataBase import MysqldbOperational
  8. import configparser
  9. def capture_video(ipAddr, cameraName):
  10. try:
  11. rtspAddr = ipAddr
  12. # print(rtspAddr)
  13. cap = cv2.VideoCapture(rtspAddr)
  14. cap.set(cv2.CAP_PROP_BUFFERSIZE, 0)
  15. ret, image = cap.read()
  16. while ret:
  17. time.sleep(0.2)
  18. cv2.imwrite('%s.jpg' % cameraName, image)
  19. print('%s.jpg' % cameraName)
  20. break
  21. except Exception as e:
  22. print('read %s error!' % ipAddr, cameraName)
  23. _logger.error(e)
  24. return e
  25. def read_camera_configuration():
  26. list_camera = cameraSql.select('camera_configuration_table',
  27. fields=["camera_name", "camera_ip", "camera_pass", "order_no", "camera_id"])
  28. return list_camera
  29. def frequency_capture(frequency):
  30. lastTime = 0
  31. while True:
  32. newTime = int(time.time())
  33. time.sleep(1)
  34. if lastTime + frequency <= newTime:
  35. camera_list = read_camera_configuration()
  36. for x in range(0, len(camera_list)):
  37. capture_video(camera_list[x][1], camera_list[x][0])
  38. lastTime = newTime
  39. if __name__ == '__main__':
  40. time.sleep(10)
  41. _logger = LogOut.Log('CPcapture')
  42. # 创建读取配置文件对象
  43. config = configparser.ConfigParser()
  44. config.read("config.ini", encoding="utf-8")
  45. # 获取通用配置项
  46. section = "General" # 读取的section标签
  47. mysql_host = config.get(section, 'mysqlHost')
  48. mysql_username = config.get(section, 'mysqlUsername')
  49. mysql_password = config.get(section, 'mysqlPassword')
  50. mysql_port = config.getint(section, 'mysqlPort')
  51. # 获取特有配置项
  52. section = "CPcapture" # 读取的section标签
  53. mysql_database = config.get(section, 'mysqlDatabase')
  54. # 连接数据库
  55. cameraSql = MysqldbOperational(host=mysql_host,
  56. username=mysql_username,
  57. password=mysql_password,
  58. port=mysql_port,
  59. database=mysql_database,
  60. logger=_logger)
  61. frequency_capture(20)