-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathmjpeg_reader.py
116 lines (91 loc) · 3.58 KB
/
mjpeg_reader.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
"""
mjpeg_reader.py
IIT : Istituto italiano di tecnologia
Pattern Analysis and Computer Vision (PAVIS) research line
Description: Social distancing alternative mjpeg reader
Disclaimer:
The information and content provided by this application is for information purposes only.
You hereby agree that you shall not make any health or medical related decision based in whole
or in part on anything contained within the application without consulting your personal doctor.
The software is provided "as is", without warranty of any kind, express or implied,
including but not limited to the warranties of merchantability,
fitness for a particular purpose and noninfringement. In no event shall the authors,
PAVIS or IIT be liable for any claim, damages or other liability, whether in an action of contract,
tort or otherwise, arising from, out of or in connection with the software
or the use or other dealings in the software.
LICENSE:
This project is licensed under the terms of the MIT license.
This project incorporates material from the projects listed below (collectively, "Third Party Code").
This Third Party Code is licensed to you under their original license terms.
We reserves all other rights not expressly granted, whether by implication, estoppel or otherwise.
The software can be freely used for any non-commercial applications and it is useful
for maintaining the safe social distance among people in pandemics. The code is open and can be
improved with your support, please contact us at [email protected] if you will to help us.
"""
from turbojpeg import TurboJPEG, TJPF_GRAY, TJSAMP_GRAY, TJFLAG_PROGRESSIVE
'''
MjpegReader
Read mjpeg multipart streaing file
'''
class MjpegReader:
def __init__(self, filename):
try:
# Open mjpeg file
self.mjpeg = open(filename, "rb")
# Initialize TurboJpeg (7x faster than opecv embedded jpeg decoder)
self.tjpeg = TurboJPEG()
# File is open
self.opened = True
except FileNotFoundError:
print ("File {0} not found".format(filename))
self.opened = False
'''
Read header line
'''
def read_line(self):
ln = ""
# Fine end of line
while True:
# Read each byte and converto to character
character = self.mjpeg.read(1).decode("utf-8")
# Finded end of line, then break loop
if character=='\n':
break
# Concatenate string
ln += character
# Return string
return ln
'''
Read Image
'''
def get_image(self):
# If file is not opened, return None
if not self.opened:
return None
# Read first line (--myboudary) and trash it
self.read_line()
# Read second line X-Timestamp and store timestamp
self.ts = int(self.read_line().split(' ')[1])
# Read content type and remove trash it
self.read_line()
# Read content lenght and get it to read image
self.lenght = int(self.read_line().split(' ')[1])
# Skip blank
self.read_line()
# Return decoded image (numpy image format)
return self.tjpeg.decode(self.mjpeg.read(self.lenght))
'''
Get Timestamp
'''
def get_ts(self):
return self.ts
'''
Get lenght
'''
def get_lenght(self):
return self.lenght
'''
Get file status
'''
def is_opened(self):
return self.opened