-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture.py
140 lines (106 loc) · 3.94 KB
/
capture.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python3
'''
jpgstream.py : display live ArduCAM JPEG images using OpenCV
Copyright (C) Simon D. Levy 2017
This file is part of BreezyArduCAM.
BreezyArduCAM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BreezyArduCAM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BreezyArduCAM. If not, see <http://www.gnu.org/licenses/>.
'''
import time
import serial
import serial.tools.list_ports
from sys import stdout
from PIL import Image
# Modifiable params --------------------------------------------------------------------
BAUD = 921600 # Change to 115200 for Due
# helpers --------------------------------------------------------------------------
def num2bytes(n):
return [n&0xFF, (n>>8)&0xFF, (n>>16)&0xFF, (n>>24)&0XFF]
def dump(msg):
stdout.write(msg)
stdout.flush()
def getack(port):
stdout.write(port.readline().decode())
def sendbyte(port, value):
port.write(bytearray([value]))
def ackcheck(port, msg):
line = port.readline().decode()
assert(msg in line)
OUTFILENAME = './card'
def capture_card(cardIndex):
print('\nWait camera')
# Send "start capture" message
# Open output file
outfile = open(OUTFILENAME + str(cardIndex) + '.jpg', 'wb')
# Loop over bytes from Arduino for a single image
written = False
prevbyte = None
done = False
while not done:
# Read a byte from Arduino
currbyte = port.read(1)
# If we've already read one byte, we can check pairs of bytes
if prevbyte:
# Start-of-image sentinel bytes: write previous byte to temp file
if ord(currbyte) == 0xd8 and ord(prevbyte) == 0xff:
outfile.write(prevbyte)
written = True
# Inside image, write current byte to file
if written:
outfile.write(currbyte)
# End-of-image sentinel bytes: close temp file and display its contents
if ord(currbyte) == 0xd9 and ord(prevbyte) == 0xff:
outfile.close()
done = True
# Track previous byte
prevbyte = currbyte
# Send "stop" message
#sendbyte(port, 0)
print('\nDone capturing')
def average_image_color(filename):
i = Image.open(filename)
h = i.histogram()
# split into red, green, blue
r = h[0:256]
g = h[256:256*2]
b = h[256*2: 256*3]
# perform the weighted average of each channel:
# the *index* is the channel value, and the *value* is its weight
return (
sum( i*w for i, w in enumerate(r) ) / sum(r),
sum( i*w for i, w in enumerate(g) ) / sum(g),
sum( i*w for i, w in enumerate(b) ) / sum(b)
)
# main ------------------------------------------------------------------------------
if __name__ == '__main__':
# Find Arduino port
arduino_ports = [
p.device
for p in serial.tools.list_ports.comports()
if p.manufacturer is not None and 'Arduino' in p.manufacturer
]
if not arduino_ports:
raise IOError("No Arduino found")
if len(arduino_ports) > 1:
warnings.warn('Multiple Arduinos found - using the first')
# Open connection to Arduino with a timeout of two seconds
port = serial.Serial(arduino_ports[0], BAUD, timeout=None)
# Report acknowledgment from camera
getack(port)
# Wait a spell
time.sleep(0.2)
INDEX = 0
while(1):
#input('Press enter to capture img')
capture_card(INDEX + 1)
#print("TEST")#average_image_color(OUTFILENAME))
time.sleep(0.1)
INDEX = INDEX + 1