record_pic.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter service
  5. name:record_pic.py 处理摄像头的图片数据
  6. author:rabin
  7. key:pic/图片名_图片总数_当前序号/gateway/device
  8. """
  9. import time
  10. import json
  11. import uuid
  12. from datetime import *
  13. from demeter.core import *
  14. class Record_pic(object):
  15. pic = {}
  16. def handle(self, record, config, value):
  17. self.record = record
  18. method = config['type'].split('_')
  19. name = method[0] + '.jpg'
  20. if name not in self.pic:
  21. self.pic[name] = {}
  22. total = int(method[1])
  23. index = int(method[2])
  24. self.pic[name][index] = value
  25. picLen = len(self.pic[name])
  26. if picLen >= total:
  27. self.create(name, config)
  28. def create(self, name, config):
  29. pic = sorted(self.pic[name].items(), key=lambda e:e[0], reverse=False)
  30. data = []
  31. day = str(date.today())
  32. day = day.split('-')
  33. file_name = str(uuid.uuid5(uuid.uuid1(), 'file'))
  34. file_path = day[0] + '/' + day[1] + '/' + day[2]
  35. file_path = File.mkdirs(os.path.join(Demeter.path, 'runtime','camera', file_path)) + '/' + Demeter.md5(file_name) + '.jpg'
  36. for value in pic:
  37. data.append(value[1])
  38. with open(file_path, 'wb') as f:
  39. f.writelines(data)
  40. del self.pic[name]
  41. file_path = file_path.replace(Demeter.path + 'runtime', '')
  42. self.record.save(config, file_path, name)