-
Notifications
You must be signed in to change notification settings - Fork 1
/
videostream.py
82 lines (61 loc) · 2.24 KB
/
videostream.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
from pyzbar.pyzbar import decode
from threading import Thread
from config import settings
import cv2
import time
import numpy as np
class VideoStream:
def __init__(self):
print("init")
# self.stream = cv2.VideoCapture(settings.RTMP_ADDRESS)
self.stream = cv2.VideoCapture(1)
fps = self.stream.get(cv2.CAP_PROP_FPS)
if fps == 0:
fps = 30.0
self.frame_duration = 1.0 / fps
(self.grabbed, self.frame) = self.stream.read()
self.stopped = False
self.qrcodes = []
time.sleep(2.0)
def start(self):
print("start thread")
t = Thread(target=self.update, args=())
t.daemon = True
t.start()
return self
def update(self):
print("read")
sync_counter = 0
sync_start_time = time.time()
ignore_sleep = False
while True:
if self.stopped:
return
start_time = time.time()
(self.grabbed, self.frame) = self.stream.read()
if not ignore_sleep:
sleep_time = max(0, self.frame_duration - (time.time() - start_time))
time.sleep(sleep_time)
if time.time() - sync_start_time >= 10:
sync_counter += 5
ignore_sleep = True
if sync_counter >= 100:
sync_counter = 0
sync_start_time = time.time()
ignore_sleep = False
def read(self):
return self.frame
def read_qr(self):
qr_codes = decode(self.frame)
frame_qr_code_data = []
for qr_code in qr_codes:
(x, y, w, h) = qr_code.rect
cv2.rectangle(self.frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
qr_data = qr_code.data.decode('utf-8')
cv2.putText(self.frame, qr_data, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
frame_qr_code_data.append(qr_data)
if len(frame_qr_code_data) >= 2:
self.qrcodes = frame_qr_code_data
return (self.frame, frame_qr_code_data)
def stop(self):
self.stopped = True