-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.py
More file actions
executable file
·93 lines (85 loc) · 3.17 KB
/
Camera.py
File metadata and controls
executable file
·93 lines (85 loc) · 3.17 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
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
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
# encoding:utf-8
import sys
sys.path.append('/home/pi/ArmPi/')
import cv2
import time
import threading
import numpy as np
from CameraCalibration.CalibrationConfig import *
if sys.version_info.major == 2:
print('Please run this program with python3!')
sys.exit(0)
class Camera:
def __init__(self, resolution=(640, 480)):
self.cap = None
self.width = resolution[0]
self.height = resolution[1]
self.frame = None
self.opened = False
#加载参数
self.param_data = np.load(calibration_param_path + '.npz')
#获取参数
self.mtx = self.param_data['mtx_array']
self.dist = self.param_data['dist_array']
self.newcameramtx, roi = cv2.getOptimalNewCameraMatrix(self.mtx, self.dist, (self.width, self.height), 0, (self.width, self.height))
self.mapx, self.mapy = cv2.initUndistortRectifyMap(self.mtx, self.dist, None, self.newcameramtx, (self.width,self.height), 5)
self.th = threading.Thread(target=self.camera_task, args=(), daemon=True)
self.th.start()
def camera_open(self):
try:
self.cap = cv2.VideoCapture(-1)
self.cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('Y', 'U', 'Y', 'V'))
self.cap.set(cv2.CAP_PROP_FPS, 30)
self.cap.set(cv2.CAP_PROP_SATURATION, 40)
self.opened = True
except Exception as e:
print('打开摄像头失败:', e)
def camera_close(self):
try:
self.opened = False
time.sleep(0.2)
if self.cap is not None:
self.cap.release()
time.sleep(0.05)
self.cap = None
except Exception as e:
print('关闭摄像头失败:', e)
def camera_task(self):
while True:
try:
if self.opened and self.cap.isOpened():
ret, frame_tmp = self.cap.read()
if ret:
frame_resize = cv2.resize(frame_tmp, (self.width, self.height), interpolation=cv2.INTER_NEAREST)
self.frame = cv2.remap(frame_resize, self.mapx, self.mapy, cv2.INTER_LINEAR)
else:
print(1)
self.frame = None
cap = cv2.VideoCapture(-1)
ret, _ = cap.read()
if ret:
self.cap = cap
elif self.opened:
print(2)
cap = cv2.VideoCapture(-1)
ret, _ = cap.read()
if ret:
self.cap = cap
else:
time.sleep(0.01)
except Exception as e:
print('获取摄像头画面出错:', e)
time.sleep(0.01)
if __name__ == '__main__':
my_camera = Camera()
my_camera.camera_open()
while True:
img = my_camera.frame
if img is not None:
cv2.imshow('img', img)
key = cv2.waitKey(1)
if key == 27:
break
my_camera.camera_close()
cv2.destroyAllWindows()