-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcamera.py
77 lines (68 loc) · 3.08 KB
/
camera.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
from cv2 import VideoCapture, waitKey, resize, putText, FONT_HERSHEY_SIMPLEX, LINE_AA, getTextSize, flip, getRotationMatrix2D, warpAffine
from numpy import zeros
import threading
from time import time, sleep
import numpy as np
RESIZETO_WIDTH = 480
RESIZETO_HEIGHT = 360
class Camera:
def begin(index = 0):
Camera.camIdx = index
Camera.camera = VideoCapture(index)
Camera.__waitFrameEvent = threading.Event()
Camera.__stopEvent = threading.Event()
Camera.__currImg = zeros((RESIZETO_HEIGHT, RESIZETO_WIDTH, 3))
Camera.__last_correct_frame = time()
Camera.readThread = threading.Thread(target = Camera.__update)
Camera.readThread.start()
return Camera.readThread
def end():
Camera.__stopEvent.set()
Camera.readThread.join()
def rotate_bound(image, angle):
# grab the dimensions of the image and then determine the
# center
(h, w) = image.shape[:2]
(cX, cY) = (w // 2, h // 2)
# grab the rotation matrix (applying the negative of the
# angle to rotate clockwise), then grab the sine and cosine
# (i.e., the rotation components of the matrix)
M = getRotationMatrix2D((cX, cY), -angle, 1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
# compute the new bounding dimensions of the image
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
# adjust the rotation matrix to take into account translation
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
# perform the actual rotation and return the image
return warpAffine(image, M, (nW, nH))
def __centerText(text):
font = FONT_HERSHEY_SIMPLEX
textsize = getTextSize(text, font, 1, 2)[0]
textX = int((Camera.__currImg.shape[1] - textsize[0]) / 2)
textY = int((Camera.__currImg.shape[0] + textsize[1]) / 2)
putText(Camera.__currImg, text, (textX, textY), font, 1, (8, 16, 255), 2, LINE_AA)
def __update():
while not Camera.__stopEvent.is_set():
ret, img = Camera.camera.read()
if ret:
img = Camera.rotate_bound(img, 90)
#img = resize(img, (RESIZETO_WIDTH, RESIZETO_HEIGHT))
Camera.__currImg = img
Camera.__last_correct_frame = time()
else:
if time() - Camera.__last_correct_frame > 1.0:
Camera.__currImg = zeros((RESIZETO_HEIGHT, RESIZETO_WIDTH, 3))
Camera.__centerText("No camera detected!")
Camera.__last_correct_frame = time()
if not Camera.camera.isOpened():
Camera.camera = VideoCapture(Camera.camIdx)
sleep(0.02)
Camera.__waitFrameEvent.set()
def waitFrame(w = RESIZETO_WIDTH, h = RESIZETO_HEIGHT):
while True:
Camera.__waitFrameEvent.wait()
Camera.__waitFrameEvent.clear()
yield resize(Camera.__currImg, (w, h))