helpers.camerahelper.take_picture()   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 32
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 27
nop 5
dl 0
loc 32
rs 7.8319
c 0
b 0
f 0
1
'''camera functions'''
2
import time
3
#import datetime
4
try:
5
    import picamera
6
except BaseException:
7
    try:
8
        import cv2
9
    except BaseException:
10
        pass
11
12
def take_picture(image_name, size="small", quality=10, brightness=50, contrast=50):
13
    try:
14
        with picamera.PiCamera() as camera:
15
            camera.start_preview()
16
            try:
17
                camera.resolution = (1280, 1024)
18
                camera.brightness = brightness
19
                camera.contrast = contrast
20
                #camera.anotate_text = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
21
                # Camera warm-up time
22
                time.sleep(2)
23
                resize = (320, 240)
24
                if size.startswith('m'):
25
                    resize = (640, 480)
26
                if size.startswith('l'):
27
                    resize = (1024, 768)
28
                if size.startswith('x'):
29
                    resize = (1280, 1024)
30
31
                camera.capture(image_name, resize=resize, quality=quality)
32
            finally:
33
                camera.stop_preview()
34
        return image_name
35
    except BaseException:
36
        #the problem with cv2 is that is appears to not work on rpi with python3
37
        camera = cv2.VideoCapture(0)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable cv2 does not seem to be defined.
Loading history...
38
        for _ in range(30):
39
            camera.read()
40
        _, frame = camera.read()
41
        cv2.imwrite(image_name, frame)
42
        camera.release()
43
        return image_name
44