-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcam.py
More file actions
54 lines (43 loc) · 1.46 KB
/
cam.py
File metadata and controls
54 lines (43 loc) · 1.46 KB
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
import numpy as np
import cv2
import time
import requests
import threading
from threading import Thread, Event, ThreadError
class Cam():
def __init__(self, url):
self.stream = requests.get(url, stream=True)
self.thread_cancelled = False
self.thread = Thread(target=self.run)
print("camera initialised")
def start(self):
self.thread.start()
print("camera stream started")
def run(self):
bytes = ''
while not self.thread_cancelled:
try:
bytes += self.stream.raw.read(1024)
a = bytes.find('\xff\xd8')
b = bytes.find('\xff\xd9')
if a != -1 and b != -1:
jpg = bytes[a:b + 2]
bytes = bytes[b + 2:]
img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
cv2.imshow('cam', img)
if cv2.waitKey(1) == 27:
exit(0)
except ThreadError:
self.thread_cancelled = True
def is_running(self):
return self.thread.is_alive()
def shut_down(self):
self.thread_cancelled = True
# block while waiting for thread to terminate
while self.thread.is_alive():
time.sleep(1)
return True
if __name__ == "__main__":
url = 'rtsp://kardopoly:v1c4l1v3@10.0.0.124:554/cam/realmonitor?channel=1&subtype=0'
cam = Cam(url)
cam.start()