FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-thingserver/example/mjpeg-stream.py at master · labthings/python-thingserver · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Jun 7, 2021. It is now read-only.
labthings
/
python-thingserver
Public archive
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
1
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-thingserver
/
example
/
mjpeg-stream.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
152 lines (126 loc) · 4.38 KB
Breadcrumbs
python-thingserver
/
example
/
mjpeg-stream.py
Copy path
File metadata and controls
152 lines (126 loc) · 4.38 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from
__future__
import
division
import
asyncio
import
io
import
logging
import
sys
import
time
from
datetime
import
datetime
from
PIL
import
Image
,
ImageDraw
from
thingserver
import
Property
,
Thing
,
Value
,
WebThingServer
if
(
sys
.
version_info
[
0
]
==
3
and
sys
.
version_info
[
1
]
>=
8
and
sys
.
platform
.
startswith
(
"win"
)
):
asyncio
.
set_event_loop_policy
(
asyncio
.
WindowsSelectorEventLoopPolicy
())
"""
PIL spams the logger with debug-level information. This is a pain when debugging api.app.
We override the logging settings in api.app by setting a level for PIL here.
"""
pil_logger
=
logging
.
getLogger
(
"PIL"
)
pil_logger
.
setLevel
(
logging
.
INFO
)
class
StreamGenerator
:
"""An example image streamer class"""
def
__init__
(
self
):
self
.
stream
=
io
.
BytesIO
()
# Byte stream to hold the latest JPEG frame
self
.
running
=
False
# Is the stream frame generator running
self
.
event
=
asyncio
.
Event
()
# Event to signal a new frame is ready
def
_start_runner
(
self
):
print
(
"Starting frame runner"
)
# Run frame generator loop in a coroutine task
task
=
asyncio
.
create_task
(
self
.
frame_loop
())
self
.
running
=
True
return
task
def
generate_new_dummy_image
(
self
):
# Create a dummy image to serve in the stream
image
=
Image
.
new
(
"RGB"
, (
640
,
480
),
color
=
(
0
,
0
,
0
),)
draw
=
ImageDraw
.
Draw
(
image
)
draw
.
text
(
(
20
,
70
),
"Current time: {}"
.
format
(
datetime
.
now
().
strftime
(
"%d/%m/%Y, %H:%M:%S"
)),
)
# Save new image to the stream
image
.
save
(
self
.
stream
,
format
=
"JPEG"
)
async
def
frame_loop
(
self
):
while
True
:
# Only serve frames at 1fps
await
asyncio
.
sleep
(
1
)
# Signal we're in the middle of writing a new frame
self
.
event
.
clear
()
# Reset stream
self
.
stream
.
seek
(
0
)
self
.
stream
.
truncate
()
# Generate new dumm image
self
.
generate_new_dummy_image
()
# Signal a new frame is ready
self
.
event
.
set
()
async
def
stream_generator
(
self
):
if
not
self
.
running
:
self
.
_start_runner
()
served_image_timestamp
=
time
.
time
()
my_boundary
=
"--boundarydonotcross
\n
"
while
True
:
interval
=
1.0
if
served_image_timestamp
+
interval
<
time
.
time
():
# Wait for current frame to finish being generated
await
self
.
event
.
wait
()
# Get the current frame
img
=
self
.
stream
.
getvalue
()
# Add frame header data
served_image_timestamp
=
time
.
time
()
prefix
=
(
my_boundary
+
"Content-type: image/jpeg
\r
\n
"
+
"Content-length: %s
\r
\n
\r
\n
"
%
len
(
img
)
)
yield
prefix
.
encode
()
+
img
else
:
# Delay by interval before checking for next frame
await
asyncio
.
sleep
(
interval
)
async
def
snapshot
(
self
):
if
not
self
.
running
:
self
.
_start_runner
()
await
self
.
event
.
wait
()
return
self
.
stream
.
getvalue
()
def
make_thing
():
stream_generator
=
StreamGenerator
()
thing
=
Thing
(
"urn:dev:ops:my-lamp-1234"
,
"My Lamp"
,
[
"OnOffSwitch"
,
"Light"
],
"A web connected lamp"
,
)
thing
.
add_property
(
Property
(
thing
,
"snapshot"
,
Value
(
None
,
stream_generator
.
snapshot
,
None
),
metadata
=
{
"title"
:
"Snapshot"
,
"readOnly"
:
True
},
content_type
=
"image/jpeg"
,
)
)
thing
.
add_property
(
Property
(
thing
,
"stream"
,
Value
(
None
,
stream_generator
.
stream_generator
,
None
),
metadata
=
{
"title"
:
"Stream"
,
"readOnly"
:
True
},
content_type
=
"multipart/x-mixed-replace;boundary=--boundarydonotcross"
,
)
)
return
thing
def
run_server
():
thing
=
make_thing
()
server
=
WebThingServer
(
thing
,
port
=
8888
,
debug
=
True
)
try
:
logging
.
info
(
"starting the server"
)
server
.
start
()
except
KeyboardInterrupt
:
logging
.
info
(
"stopping the server"
)
server
.
stop
()
logging
.
info
(
"done"
)
if
__name__
==
"__main__"
:
logging
.
basicConfig
(
level
=
10
,
format
=
"%(asctime)s %(filename)s:%(lineno)s %(levelname)s %(message)s"
)
run_server
()
Back
|
FazBrowse Home
|
New Git URL