/
device.py
119 lines (98 loc) · 3.64 KB
/
device.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import time
from onvif import ONVIFCamera
import urllib.parse as url
from multiprocessing import Process
import context
import cv2
from camdetect import read_anno_config, entry_detect, release_detect
class DeviceManager(object):
"""docstring for DeviceManager"""
def __init__(self):
self.__devices = {}
def addDevices(self, deviceInfos):
print('[DeviceManager]addDevices()', deviceInfos)
for info in deviceInfos:
dev = Device(info)
self.__devices[info.urn] = dev
dev.run()
def stop(self):
for k, v in self.__devices.items():
v.stop()
release_detect()
class DeviceInfo(object):
"""docstring for DeviceInfo"""
def __init__(self, urn, xaddr):
self.urn = urn
self.xaddr = xaddr
class Device(object):
"""docstring for Device"""
def __init__(self, info):
self.__urn = info.urn
self.__xaddr = info.xaddr
self.__proc = Process(target=self.__deviceProc, args=())
self.__rtsp = None
self.__cam = None
def run(self):
print('Device %s running...', self.__urn)
self.__proc.start()
def stop(self):
if self.__proc.is_alive():
self.__proc.stop()
if self.__rtsp is not None:
self.__rtsp.close()
print('Device %s stopped...', self.__urn)
def __deviceProc(self):
res = url.urlparse(self.__xaddr)
print(res)
tmp = res[1].split(':')
ip = tmp[0]
if len(tmp) > 1:
port = tmp[1]
else:
port = 80
num, matrix = read_anno_config('./Elec_Solution2/config/anno0.json')
# get camera instance
cam = ONVIFCamera(ip, port, '', '')
# create media service
media_service = cam.create_media_service()
token = '000'
# set video configuration
configurations_list = media_service.GetVideoEncoderConfigurations()
video_encoder_configuration = configurations_list[0]
options = media_service.GetVideoEncoderConfigurationOptions({'ProfileToken':token})
video_encoder_configuration.Encoding = 'H264'
video_encoder_configuration.Resolution = options.H264.ResolutionsAvailable[0]
request = media_service.create_type('SetVideoEncoderConfiguration')
request.Configuration = video_encoder_configuration
request.ForcePersistence = True
media_service.SetVideoEncoderConfiguration(request)
# get video stream
streamSetup = {
'StreamSetup': {
'Stream': 'RTP-Unicast',
'Transport': {
'Protocol': 'TCP'
}
},
'ProfileToken': token
}
res = media_service.GetStreamUri(streamSetup)
self.__rtsp = cv2.VideoCapture(res.Uri)
reporter = context.getContext().reporter
# capture and detect
while self.__rtsp.isOpened():
print('%s capture start...' % ip)
start = time.time()
print('start: %d' % start)
ret, frame = self.__rtsp.read()
print('capture: %d' % time.time())
# img = cv2.cvtColor(numpy.asarray(frame),cv2.COLOR_RGB2BGR)
print('convert: %d' % time.time())
tmp = self.__urn.split('-')
name = tmp[-1] + '.jpg'
cv2.imwrite(name, frame)
detect_result = entry_detect(frame, num, matrix)
print(detect_result)
print('%s capture end %d. duration:%d' % (ip, time.time(), time.time() - start))
reporter.publish('hm_test', detect_result)
time.sleep(10)