def update(self):
self.stream_fps.start()
# keep looping infinitely until the thread is stopped
for frameBuf in self.stream:
self.stream_fps.update()
# grab the frame from the stream and clear the stream in preparation for the next frame
self.frame = np.rot90(frameBuf.array)
self.rawCapture.truncate(0)
# if the thread indicator variable is set, stop the thread
if self.running == False:
self.camera.led = False
return
python类array()的实例源码
def update(self):
# keep looping infinitely until the thread is stopped
for frameBuf in self.stream:
# grab the frame from the stream and clear the stream in preparation for the next frame
self.frame = np.rot90(frameBuf.array)
self.rawCapture.truncate(0)
# if the thread indicator variable is set, stop the thread
if self.running == False:
self.camera.led = False
return
def update(self):
# keep looping infinitely until the thread is stopped
for frameBuf in self.stream:
# grab the frame from the stream and clear the stream in preparation for the next frame
self.frame = frameBuf.array
self.rawCapture.truncate(0)
# if the thread indicator variable is set, stop the thread
if self.running == False:
return
def update(self):
# keep looping infinitely until the thread is stopped
for frameBuf in self.stream:
# grab the frame from the stream and clear the stream in preparation for the next frame
self.frame = frameBuf.array
self.rawCapture.truncate(0)
# if the thread indicator variable is set, stop the thread
if self.running == False:
self.camera.led = False
return
def resolution_pixels_xy(self):
"""The size of a photo in pixels"""
return np.array(config.CAMERA_RESOLUTION)
def pixels_to_mm_scale_factors_xy(self):
return np.array([config.X_PIXELS_TO_MILLIMETRE_SCALE, config.Y_PIXELS_TO_MILLIMETRE_SCALE])
def capture_images():
"""
print "Staring capture thread"
global frame
while True:
print "Attempting capture"
(grabbed, f) = stream.read()
if grabbed:
print "Captured"
lock.acquire()
frame = imutils.resize(f, width=resolution[0], height=resolution[1])
lock.release()
"""
print "started capturing thread"
global frame
with picamera.PiCamera() as camera:
camera.resolution = resolution
camera.shutter_speed = 250
time.sleep(0.5) # Shutter speed is not set instantly. This wait allows time for changes to take effect.
print "Initialized camera..."
with picamera.array.PiRGBArray(camera) as stream:
for foo in camera.capture_continuous(stream, format="bgr", use_video_port=True):
print "Captured an image"
stream.seek(0)
stream.truncate()
lock.acquire()
frame = stream.array
lock.release()
print "Converted image data to array"
def __init__(self, cam, height, width, cropAroundOffset=False, proccessingHeight=0, proccessingWidth=0):
self.camera = cam
self.stream = picamera.array.PiYUVArray(self.camera)
# When this boolean is True, The Frame taken will be cropped down to a 96x96 image for proccessing
# the center of the cropped down image will be the expected position of the object
self.cropAroundOffset = cropAroundOffset
self.height = height
self.width = width
if self.cropAroundOffset == False:
self.proccessingHeight = self.height
self.proccessingWidth = self.width
else:
self.proccessingHeight = proccessingHeight
self.proccessingWidth = proccessingWidth
self.camera.resolution = (self.height, self.width)
self.pixelObjList = []
self.objIDCntr = 0
self.pixelObjList.append(PixelObj.PixelObj(self.getNextObjId(), self.proccessingHeight))
# - - - - - - - - - - - - - - - -
# - - - GET NEXT OBJ ID - - - - -
# - - - - - - - - - - - - - - - -
def captureFrame(self, offset = None, folderName = 0):
self.folderName = folderName
self.stream = picamera.array.PiYUVArray(self.camera)
self.camera.capture(self.stream, 'yuv')
self.camera._set_led(True)
self.pixelObjList = []
self.objIDCntr = 0
self.pixelObjList.append(PixelObj.PixelObj(self.getNextObjId(), self.proccessingHeight))
rows = []
for _ in range(self.height):
rows.append(range(self.width))
for j, j_ in enumerate(range(self.width - 1,-1,-1)): #flip image horizontally
for i, i_ in enumerate(range(self.height - 1, -1, -1)): #flip vertically
rows[j][i] = self.stream.array[j_][i_][0]
# We need to crop the image to a 96 x 96 image if the cropAroundOffset value it True
if self.cropAroundOffset and offset is not None:
croppedRows = []
for _ in range(self.proccessingHeight):
croppedRows.append(range(self.proccessingWidth))
for j in range(self.proccessingHeight):
for i in range(self.proccessingWidth):
croppedRows[j][i] = rows[j + offset.y][i + offset.x]
rows = croppedRows
self.savePNG('raw.png', rows)
self.processFrame_5(self.processFrame_4(self.processFrame_3(self.processFrame_1(rows), self.processFrame_2(rows))))
# - - - - - - - - - - - - - - - -
# - - - PROCESS FRAME 1 - - - - -
# - - - - - - - - - - - - - - - -
def __init__(self, height, width):
"""Constructor"""
self.camera = picamera.PiCamera()
self.stream = picamera.array.PiYUVArray(self.camera)
self.height = height
self.width = width
self.camera.resolution = (self.height, self.width)
# - - - - - - - - - - - - - - - -
# - - - - Take Picture - - - - -
# - - - - - - - - - - - - - - - -
def demosaic(self):
if self._demo is None:
# XXX Again, should take into account camera's vflip and hflip here
# Construct representation of the bayer pattern
bayer = np.zeros(self.array.shape, dtype=np.uint8)
bayer[1::2, 0::2, 0] = 1 # Red
bayer[0::2, 0::2, 1] = 1 # Green
bayer[1::2, 1::2, 1] = 1 # Green
bayer[0::2, 1::2, 2] = 1 # Blue
# Allocate output array with same shape as data and set up some
# constants to represent the weighted average window
window = (3, 3)
borders = (window[0] - 1, window[1] - 1)
border = (borders[0] // 2, borders[1] // 2)
# Pad out the data and the bayer pattern (np.pad is faster but
# unavailable on the version of numpy shipped with Raspbian at the
# time of writing)
rgb = np.zeros((
self.array.shape[0] + borders[0],
self.array.shape[1] + borders[1],
self.array.shape[2]), dtype=self.array.dtype)
rgb[
border[0]:rgb.shape[0] - border[0],
border[1]:rgb.shape[1] - border[1],
:] = self.array
bayer_pad = np.zeros((
self.array.shape[0] + borders[0],
self.array.shape[1] + borders[1],
self.array.shape[2]), dtype=bayer.dtype)
bayer_pad[
border[0]:bayer_pad.shape[0] - border[0],
border[1]:bayer_pad.shape[1] - border[1],
:] = bayer
bayer = bayer_pad
# For each plane in the RGB data, construct a view over the plane
# of 3x3 matrices. Then do the same for the bayer array and use
# Einstein summation to get the weighted average
self._demo = np.empty(self.array.shape, dtype=self.array.dtype)
for plane in range(3):
p = rgb[..., plane]
b = bayer[..., plane]
pview = as_strided(p, shape=(
p.shape[0] - borders[0],
p.shape[1] - borders[1]) + window, strides=p.strides * 2)
bview = as_strided(b, shape=(
b.shape[0] - borders[0],
b.shape[1] - borders[1]) + window, strides=b.strides * 2)
psum = np.einsum('ijkl->ij', pview)
bsum = np.einsum('ijkl->ij', bview)
self._demo[..., plane] = psum // bsum
return self._demo
def project_on_road(self, image_input):
image = image_input[self.remove_pixels:, :]
image = self.trans_per(image)
self.im_shape = image.shape
self.get_fit(image)
if self.detected_first & self.detected:
# create fill image
temp_filler = np.zeros((self.remove_pixels,self.im_shape[1])).astype(np.uint8)
filler = np.dstack((temp_filler,temp_filler,temp_filler))
# create an image to draw the lines on
warp_zero = np.zeros_like(image).astype(np.uint8)
color_warp = np.dstack((warp_zero, warp_zero, warp_zero))
ploty = np.linspace(0, image_input.shape[0]-1, image_input.shape[0] )
left_fitx = self.best_fit_l[0]*ploty**2 + self.best_fit_l[1]*ploty + self.best_fit_l[2]
right_fitx = self.best_fit_r[0]*ploty**2 + self.best_fit_r[1]*ploty + self.best_fit_r[2]
# recast the x and y points into usable format for cv2.fillPoly()
pts_left = np.array([np.transpose(np.vstack([left_fitx, ploty]))])
pts_right = np.array([np.flipud(np.transpose(np.vstack([right_fitx, ploty])))])
pts = np.hstack((pts_left, pts_right))
# draw the lane onto the warped blank image
cv2.fillPoly(color_warp, np.int_([pts]), (0,255, 0))
# warp the blank back to original image space using inverse perspective matrix (Minv)
newwarp = cv2.warpPerspective(color_warp, self.Minv, color_warp.shape[-2:None:-1])
left_right = cv2.warpPerspective(self.left_right, self.Minv, color_warp.shape[-2:None:-1])
# combine the result with the original image
left_right_fill = np.vstack((filler,left_right))
result = cv2.addWeighted(left_right_fill,1, image_input, 1, 0)
result = cv2.addWeighted(result, 1, np.vstack((filler,newwarp)), 0.3, 0)
# get curvature and offset
self.calculate_curvature_offset()
# plot text on resulting image
img_text = "radius of curvature: " + str(round((self.left_curverad + self.right_curverad)/2,2)) + ' (m)'
if self.offset< 0:
img_text2 = "vehicle is: " + str(round(np.abs(self.offset),2)) + ' (m) left of center'
else:
img_text2 = "vehicle is: " + str(round(np.abs(self.offset),2)) + ' (m) right of center'
result2 = cv2.resize(result, (0,0), fx=self.enlarge, fy=self.enlarge)
cv2.putText(result2,img_text, (15,15), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255,255,255),1)
cv2.putText(result2,img_text2,(15,40), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255,255,255),1)
return result2
# if lanes were not detected output source image
else:
return cv2.resize(image_input,(0,0), fx=self.enlarge, fy=self.enlarge)