-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester_nhang.py
64 lines (49 loc) · 1.4 KB
/
tester_nhang.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 6 18:19:28 2018
@author: tensorflow-cuda
"""
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.isAlive()
def shut_down(self):
self.thread_cancelled = True
#block while waiting for thread to terminate
while self.thread.isAlive():
time.sleep(1)
return True
if __name__ == "__main__":
url = 'rtsp://admin:kgisl@123@10.100.10.192/doc/page/preview.asp'
cam = Cam(url)
cam.start()