-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathptl.py
More file actions
executable file
·118 lines (100 loc) · 4.24 KB
/
Copy pathptl.py
File metadata and controls
executable file
·118 lines (100 loc) · 4.24 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
import argparse
import signal
import sys
import os
from cv2 import VideoCapture, imwrite, IMWRITE_PNG_COMPRESSION, IMWRITE_JPEG_QUALITY, IMWRITE_WEBP_QUALITY,\
CAP_PROP_FRAME_HEIGHT, CAP_PROP_FRAME_WIDTH
from time import sleep
class TimeLapse:
def __init__(self, config):
"""__init__(self):"""
self.interval = config.interval
self.shots = config.count
self.output = config.output
self.camera = VideoCapture(config.device)
self.startFrom = config.startfrom
self.currnetShot = 0
self.imageName = config.name
self.imageType = config.type
self.imageQuality = config.quality
self.verbose = config.verbose
def getResolution(self):
"""getResolution"""
width = self.camera.get(CAP_PROP_FRAME_WIDTH)
height = self.camera.get(CAP_PROP_FRAME_HEIGHT)
return width, height
def setMaxResolution(self):
"""setMaxResololution"""
self.setResolution(10000, 10000)
def setResolution(self, width, height):
"""setMaxResolution"""
self.camera.set(CAP_PROP_FRAME_WIDTH, width)
self.camera.set(CAP_PROP_FRAME_HEIGHT, height)
def run(self):
"""run"""
if self.startFrom:
self.currnetShot = self.startFrom
if self.shots:
self.shots = self.startFrom + self.shots
while True:
if self.shots and self.shots <= self.currnetShot:
break
ok, img = self.takePhoto()
if ok:
self.saveImage(img)
self.currnetShot += 1
sleep(self.interval)
def takePhoto(self):
"""takePhoto"""
return self.camera.read()
def saveImage(self, img):
"""saveImage"""
name = '{name}_{index:02d}.{type}'.format(name=self.imageName,
index=self.currnetShot,
type=self.imageType)
imagePath = os.path.join(self.output, name)
if self.verbose:
print('Save a photo {} to: {}'.format(img.shape, imagePath))
imwrite(imagePath, img, self.imageOpts())
def imageOpts(self):
if self.imageType == 'jpg':
if self.imageQuality < 0 or self.imageQuality > 100:
self.imageQuality = 95
return IMWRITE_JPEG_QUALITY, self.imageQuality
elif self.imageType == 'png':
if self.imageQuality < 0 or self.imageQuality > 9:
self.imageQuality = 3
return IMWRITE_PNG_COMPRESSION, self.imageQuality
elif self.imageType == 'webp':
if self.imageQuality < 0 or self.imageQuality > 100:
self.imageQuality = 95
return IMWRITE_WEBP_QUALITY, self.imageQuality
def stop(self):
"""stop"""
self.shots = self.currnetShot
def __del__(self):
self.camera.release()
def stop_program(signal, frame):
"""stop_program"""
print('\nPrograms stop on next photo')
tl.stop()
sys.exit(0)
if __name__ == "__main__":
signal.signal(signal.SIGINT, stop_program)
parser = argparse.ArgumentParser(usage='%(prog)s [options]')
parser.add_argument('-v', '--verbose', action="store_true", help="verbose")
parser.add_argument('-i', '--interval', default=1, type=int, help='time interval in sec [default = 1]')
parser.add_argument('-d', '--device', default=0, type=int, help='Device number [default = 0]')
parser.add_argument('-c', '--count', type=int, help='Number of shots to take')
parser.add_argument('-s', '--startfrom', type=int, help='start from photo index (image_01.jpg)')
parser.add_argument('-n', '--name', type=str, default='image', help='Base name of taken photos ')
parser.add_argument('-t', '--type', choices=['jpg', 'png', 'webp'], default='jpg', help='Image type')
parser.add_argument('-q', '--quality', type=int, default=-1, help='Image quality jpg 0-100, webm 0-100, png 9-0')
parser.add_argument('-o', '--output', default=".", type=str,
help='Output directory for captured images [default = ./ ]')
config = parser.parse_args()
tl = TimeLapse(config)
print('Press Ctrl+C to stop')
tl.setMaxResolution()
tl.run()