-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtake_shot.py
103 lines (86 loc) · 2.64 KB
/
take_shot.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
import cv2.cv as cv
import numpy
import os
CV_CAP_PROP_FRAME_WIDTH = 3
CV_CAP_PROP_FRAME_HEIGHT = 4
DIR_PREFIX = './pics'
DATE_X = 0
DATE_Y = 460
def init_camera(rx=640,ry=480):
setup_uvc_camera()
camera = cv.CaptureFromCAM(0)
# set camera properties
cv.SetCaptureProperty(camera, CV_CAP_PROP_FRAME_WIDTH, rx)
cv.SetCaptureProperty(camera, CV_CAP_PROP_FRAME_HEIGHT, ry)
return camera
def setup_uvc_camera():
command = 'uvcdynctrl -s '
command += 'Brightness 0'
os.system(command)
command = 'uvcdynctrl -s '
command += 'Contrast 13'
os.system(command)
command = 'uvcdynctrl -s '
command += '"White Balance Temperature, Auto" 0'
os.system(command)
command = 'uvcdynctrl -s '
command += '"White Balance Temperature" 6500'
os.system(command)
command = 'uvcdynctrl -s '
command += '"Backlight Compensation" 0'
os.system(command)
command = 'uvcdynctrl -s '
command += '"Exposure, Auto" 1'
os.system(command)
command = 'uvcdynctrl -s '
command += '"Exposure (Absolute)" 300'
os.system(command)
command = 'uvcdynctrl -s '
command += '"Exposure, Auto Priority" 0'
os.system(command)
def get_image(camera,filename=None):
im = cv.QueryFrame(camera)
# take greyscale and compute RMS value
im2 = cv.CreateImage(cv.GetSize(im),cv.IPL_DEPTH_32F,3)
cv.Convert(im,im2)
gray = cv.CreateImage(cv.GetSize(im),cv.IPL_DEPTH_32F,1)
cv.CvtColor(im2,gray,cv.CV_RGB2GRAY)
gray_mat = cv.GetMat(gray)
img = numpy.asarray(gray_mat)
power = numpy.sqrt(numpy.mean(img**2))
#save file
if filename:
font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.8, 0.8, 0, 2, cv.CV_AA)
cv.PutText(im,filename,(DATE_X,DATE_Y),font,cv.RGB(255,255,0))
filename = os.path.join(DIR_PREFIX,filename+'.jpg')
print filename
cv.SaveImage(filename,im)
del font
else:
filename = ''
#del(camera)
del im, im2, gray, img, gray_mat
return (power,filename)
if __name__ == '__main__':
if 1:
camera = init_camera()
print get_image(camera,'test_file')
else:
import pylab
pylab.ion()
fig = pylab.figure()
ax = fig.add_subplot(111)
p = []
camera = init_camera()
p.append(get_image(camera,'test_file')[0])
ix = []
ix.append(1)
line1, = ax.plot(ix,p,'o-')
while True:
p.append(get_image(camera,'test_file')[0])
print p[-1]
ix.append(ix[-1]+1)
line1.set_data(ix,p)
ax.relim()
ax.autoscale()
fig.canvas.draw()