-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcamera.py
76 lines (60 loc) · 2.27 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
import cv2
import numpy as np
import json
def getAvailableCameraIndexes():
index = 0
arr = []
non = 0
while True:
cap = cv2.VideoCapture(index)
if not cap.read()[0]:
non += 1
if non == 5:
break
else:
arr.append(index)
non = 0
cap.release()
index += 1
return arr
class Camera:
def __init__(self, cameraIndex: int = 0, resolution = [640, 480]):
self.cameraIndex = cameraIndex
self.cameraStream: cv2.VideoCapture = cv2.VideoCapture(cameraIndex)
self.frame = None
self.mtx = []
self.dist = []
self.params = ()
self.cameraStream.set(cv2.CAP_PROP_FRAME_WIDTH, resolution[0])
self.cameraStream.set(cv2.CAP_PROP_FRAME_HEIGHT, resolution[1])
def updateCameraIndex(self, cameraIndex: int = 0):
self.cameraIndex = cameraIndex
self.cameraStream = cv2.VideoCapture(cameraIndex)
def getCalibrationInfo(self):
with open("calibData/cam" + str(self.cameraIndex) + ".json", "r") as calibDataJson:
calibDataDict = json.load(calibDataJson)
try:
mtx = calibDataDict['mtx']
dist = calibDataDict['dist']
self.mtx = cv2.Mat(np.array(mtx, dtype=np.float32))
self.dist = cv2.Mat(np.array(dist, dtype=np.float32))
self.params = tuple(calibDataDict['params'])
# print("calibration data found")
except:
print("no calibration data found")
def getLatestFrame(self):
ret, self.frame = self.cameraStream.read()
return self.frame
def convertFrameToBytes(self, frame):
ret, jpeg = cv2.imencode('.jpg', frame)
return jpeg.tobytes()
def getLatestFrameCalibrated(self):
ret, frame = self.cameraStream.read()
h, w = frame.shape[:2]
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(self.mtx, self.dist, (w,h), 1, (w,h))
calibParams = cv2.undistort(frame, self.mtx, self.dist, None, newcameramtx)
x, y, w, h = roi
self.frame = calibParams[y:y+h, x:x+w]
return self.frame
def renderCameraStream(self, frame, title: str = "Camera: "):
cv2.imshow(title + str(self.cameraIndex), frame)