-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicamera_rgb_predictor.py
48 lines (47 loc) · 2.11 KB
/
picamera_rgb_predictor.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
import time
from picamera import PiCamera
import numpy as np
# picamera setup
h = 640 # change this to anything < 2592 (anything over 2000 will likely get a memory error when plotting
cam_res = (int(h),int(0.75*h)) # keeping the natural 3/4 resolution of the camera
cam_res = (int(16*np.floor(cam_res[1]/16)),int(32*np.floor(cam_res[0]/32)))
cam = PiCamera()
## making sure the picamera doesn't change white balance or exposure
## this will help create consistent images
cam.resolution = (cam_res[1],cam_res[0])
cam.framerate = 30
time.sleep(2) #let the camera settle
cam.iso = 100
cam.shutter_speed = cam.exposure_speed
cam.exposure_mode = 'off'
gain_set = cam.awb_gains
cam.awb_mode = 'off'
cam.awb_gains = gain_set
# prepping for analysis and recording background noise
# the objects should be removed while background noise is calibrated
data = np.empty((cam_res[0],cam_res[1],3),dtype=np.uint8)
noise = np.empty((cam_res[0],cam_res[1],3),dtype=np.uint8)
x,y = np.meshgrid(np.arange(np.shape(data)[1]),np.arange(0,np.shape(data)[0]))
rgb_text = ['Red','Green','Blue'] # array for naming color
input("press enter to capture background noise (remove colors)")
cam.capture(noise,'rgb')
noise = noise-np.mean(noise) # background 'noise'
# looping with different images to determine instantaneous colors
while True:
try:
print('===========================')
input("press enter to capture image")
cam.capture(data,'rgb')
mean_array,std_array = [],[]
for ii in range(0,3):
# calculate mean and STDev and print out for each color
mean_array.append(np.mean(data[:,:,ii]-np.mean(data)-np.mean(noise[:,:,ii])))
std_array.append(np.std(data[:,:,ii]-np.mean(data)-np.mean(noise[:,:,ii])))
print('-------------------------')
print(rgb_text[ii]+'---mean: {0:2.1f}, stdev: {1:2.1f}'.format(mean_array[ii],std_array[ii]))
# guess the color of the object
print('--------------------------')
print('The Object is: {}'.format(rgb_text[np.argmax(mean_array)]))
print('--------------------------')
except KeyboardInterrupt:
break