wu_xinjun
2022-02-10 d78e213656461834adb890b5f1c7d0789725a6de
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 concurrent.futures import thread
from itertools import count
import threading
import cv2
import sched
import time
 
IMAGE_WIDTH = 3840
IMAGE_HEIGHT = 2880
 
class CameraTest:
 
    def __init__(self, interval: int):
        self.last_frame = None
        self.count = 0
        self.last_time = 0
        self.interval = interval
        self.next_time = 0
        self.cameras = []
 
    def get_image(self) -> None:
        # cv2.namedWindow("preview", cv2.WINDOW_NORMAL)
        # cv2.namedWindow("diff", cv2.WINDOW_NORMAL)
        while True:
            this_time = time.time()
            if this_time > self.next_time:
                for i, camera in enumerate(self.cameras):
                    ret, frame = camera.read()
                    fn = "./Images/{}-{}.jpg".format(i, self.count)
                    # print(fn, type(frame), frame.shape)
                    encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 95]
                    result, encimg = cv2.imencode('.jpg', frame, encode_param)
                    print(fn, type(encimg), encimg.shape)
                    # cv2.imshow("preview", frame)
                    # encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 95]
                #cv2.imwrite(fn, frame, encode_param)
                # if not self.last_frame is None:
                #     diff = cv2.absdiff(frame, self.last_frame)
                #     print(self.count, this_time, diff.sum())
                #     cv2.imshow("diff", diff)
                # self.last_frame = frame1
                self.count += 1
                self.next_time += self.interval
                print("frame ", self.count)
            # time.sleep(0.1)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
            if self.count > 50:
                break
        for camera in self.cameras:
            camera.release()
        # cv2.destroyAllWindows()
 
    def open_cameras(self) -> bool:
        """
        Test the ports and returns a tuple with the available ports and the ones that are working.
        """
        for i in range(0,4):
            camera = cv2.VideoCapture(i, cv2.CAP_DSHOW)
            #camera = cv2.VideoCapture(i, cv2.CAP_DSHOW)
            # 设置摄像头设备分辨率
            # camera.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))
            camera.set(cv2.CAP_PROP_FRAME_WIDTH, IMAGE_WIDTH)
            camera.set(cv2.CAP_PROP_FRAME_HEIGHT, IMAGE_HEIGHT)
            camera.set(cv2.CAP_PROP_FPS, 1)
            if camera.isOpened():
                is_reading, img = camera.read()
                w = camera.get(3)
                h = camera.get(4)
                if is_reading:
                    print("Port %s is working and reads images (%s x %s)" % (i, w, h))
                    self.cameras.append(camera)
 
    def start_capture(self):
        self.next_time = time.time() + self.interval
        threading.Thread(target=self.get_image).start()
 
if __name__ == '__main__':
    image = {}
    camera = CameraTest(1)
    camera.open_cameras()
    camera.start_capture()