def addPhoto(taskID, sequence, imageFile): #taskID = x["taskID"] isOriginal = True storedDir = '/home/ec2-user/root/www/static/' if not os.path.exists(storedDir): storedDir = '%s/static/%s/' % (os.getcwd(),taskID) else: storedDir = '/home/ec2-user/root/www/static/'+taskID+'/' makeIfNone(storedDir) web.debug('final stored dir:%s, %s' % (storedDir,isOriginal)) hostName = "www.pin3d.cn" baseURL = 'http://'+ hostName +'/static/'+taskID+'/' #filePath = x['myfile'].filename.replace('\\','/').split('/')[-1] postFix = replace.getSubfix(imageFile) hashedName = hashlib.md5(str(datetime.now(chinaTime))).hexdigest() + '.' + postFix destname = os.path.join(storedDir,hashedName) #cpCmd = "cp %s %s" % (shellquote(imageFile), shellquote(destname)) #cmdResult = util.executeCmd(cpCmd) #print "cmd:%s, result:%s" % (cpCmd, cmdResult) ImageUtil.squareCrop(imageFile, destname) ImageUtil.resize(destname, 80, 'tb') ImageUtil.resize(destname, 180, 'cv') ImageUtil.resize(destname, 640, 'nm') #storedPhoto['screenURL'] = baseURL+hashedName remoteURL = baseURL + hashedName storedPhoto = { 'taskID':taskID, 'sequence':int(sequence), 'remoteURL':remoteURL, 'originalURL':remoteURL} MongoUtil.save('StoredPhoto', storedPhoto) return remoteURL
def draw_line(self, window_line_buf, window, y, block_x): if y >= self.height: return if self._tiled: width = min(self._width, window.width - block_x) else: width = min(self._bitmap_width, self._width, window.width - block_x) start = self._current * self._bitmap_width * self._bitmap_height \ + self._bitmap_width * (y % self._bitmap_height) cx = start for x in range(start, start + width): index = self._data[cx] col = block_x + x - start if self._mask_color is None: color = self.get_color(index) if 0 <= col < window.width and color != self._transparent_color: window_line_buf[col] = color else: color = self.get_color(self._mask_color) intensity = index if 0 <= col < window.width and intensity > 0: window_line_buf[col] = ImageUtil.blend_pixel( window_line_buf[col], color, intensity) cx = cx + 1 if cx - start >= self._bitmap_width: cx = start
def __init__(self, window, id, bitmaps, transparent_color=None, current_bitmap=0): self._object_index = None self._palette = window.palettes[1] self._window = window self._id = id self._data = [] if isinstance(bitmaps, str): bitmaps = [bitmaps] assert (isinstance(bitmaps, list)) self._bitmap_count = len(bitmaps) self._transparent_color = transparent_color self._current_bitmap = current_bitmap color_data = [] for bitmap in bitmaps: self._width, self._height, data = ImageUtil.load( bitmap, transparent_color) color_data.extend(data) self._data = self._palette.extend(color_data, transparent_color) self._current = 0
def POST(self): x = web.input() taskID = x["taskID"] photoID = x.get("photoID") sequence = x.get("sequence") isOriginal = x.get("isOriginal") #x = web.input(myfile={}) #web.debug('data:%s', web.data()[:100]); pos = web.data()[:50].find('base64,'); data = web.data()[pos+7:].decode('base64') storedDir = '/home/ec2-user/root/www/static/' if not os.path.exists(storedDir): storedDir = '%s/static/%s/' % (os.getcwd(),taskID) else: storedDir = '/home/ec2-user/root/www/static/'+taskID+'/' makeIfNone(storedDir) web.debug('final stored dir:%s' % storedDir) baseURL = 'http://%s/static/%s/' % (web.ctx.env.get('HTTP_HOST'), taskID) #filePath = x['myfile'].filename.replace('\\','/').split('/')[-1] postFix = 'jpg'#filePath.split('.')[-1] hashedName = hashlib.md5('raw' + str(datetime.now(chinaTime))).hexdigest() + '.' + postFix imageFileName = storedDir+hashedName; fout = open(imageFileName, 'w') fout.write(data) fout.close() #ImageUtil.resize(imageFileName, 60, 'tb') ImageUtil.resize(imageFileName, 60, 'tb') #storedPhoto['screenURL'] = baseURL+hashedName remoteURL = baseURL + hashedName storedPhoto = None if photoID: storedPhoto = MongoUtil.fetchByID('StoredPhoto', ObjectId(photoID)) oldRemoteURL = storedPhoto['remoteURL'] storedPhoto['remoteURL'] = remoteURL if isOriginal and int(isOriginal) == 1: storedPhoto['originalURL'] = remoteURL elif not storedPhoto.get('originalURL'): storedPhoto['originalURL'] = oldRemoteURL MongoUtil.update('StoredPhoto',storedPhoto) else: storedPhoto = {'taskID':taskID, 'sequence':int(sequence), 'remoteURL':remoteURL, 'originalURL':remoteURL} MongoUtil.save('StoredPhoto', storedPhoto) #task = MongoUtil.fetchByID('PhotoTask', ObjectId(taskID)) return simplejson.dumps(cleanStoredPhoto(storedPhoto))
def process_frame(frame_img, debug=False): """ Main method for processing each image frame of a video file """ text_output = [] # Step1: Undistort image using the camera calibration undistorted_img = _camera_calibrator.undistort(frame_img) # Step2: Apply perspective transformation to create a warped image color_warped = _perspective_transformer.warp_image(undistorted_img, M) # Step3: Use color transforms, gradients, etc., to create a thresholded binary image binary_warped, binary_arr = ImageUtil.binary_thresholded_image(color_warped) image_shape = binary_warped.shape lane_detection_output = visualize_binary_thresholded_image(color_warped, binary_warped, binary_arr[0], binary_arr[1]) # Step4: Get the sliding window polynomials for the left and right line leftx, lefty, rightx, righty = _lane.find_lane_pixels(binary_warped) ploty = np.linspace(0, image_shape[0] - 1, image_shape[0]) left_fitx = _lane.left.polyfit_lines(leftx, lefty, image_shape) right_fitx = _lane.right.polyfit_lines(rightx, righty, image_shape) # Step5: Overlay the warped image to the original image overlay_img = _lane.overlay_image(binary_warped, frame_img, Minv, left_fitx, right_fitx, ploty) result = overlay_to_original_image(overlay_img, frame_img) # Step6: curvature_and_vehicle_position vehicle_position = _lane.get_vehicle_position(image_shape) radius_of_curvature = _lane.get_radius_of_curvature() text_output.append('Radius of curvature: {} m'.format(radius_of_curvature)) text_output.append('Vehicle: {}'.format(vehicle_position)) text_output.append(''); text_output.append('Left line curve:') text_output.append('{:.2}, {:.2}, {:.2}'.format(_lane.left.line_fit0_queue[0], _lane.left.line_fit1_queue[0], _lane.left.line_fit2_queue[0])) text_output.append(''); text_output.append('Right line curve:') text_output.append('{:.2}, {:.2}, {:.2}'.format(_lane.right.line_fit0_queue[0], _lane.right.line_fit1_queue[0], _lane.right.line_fit2_queue[0])) text_output.append(''); if _lane.left.detected == False: text_output.append('!!!Left lane not detected!!!') if _lane.right.detected == False: text_output.append('!!!Right lane not detected!!!') console_img = ImageUtil.image_console(result, frame_img, undistorted_img, lane_detection_output, overlay_img, text_output) return console_img
def pixel(self, pixel_offset, color): start = self._width * self._height * self._current_bitmap start += pixel_offset color_index = self._data[pixel_offset] color = self._palette.color(color_index) if ImageUtil.alpha(color) == 0xFF: return None else: return color
def extend(self, color_data, transparent_color): data = [] for color in color_data: if color == transparent_color: color = ImageUtil.set_alpha(color, 0xFF) index = self.find_color(color) if index is None: data.append(self.add_color(color)) else: data.append(index) return data
def test_image_frame(): _, image = test_images[0] text_output = [] # Step1: Undistort image using the camera calibration undistorted_img = _camera_calibrator.undistort(image) # Step2: Apply perspective transformation to create a warped image color_warped = _perspective_transformer.warp_image(undistorted_img, M) # Step3: Use color transforms, gradients, etc., to create a thresholded binary image binary_warped, binary_arr = ImageUtil.binary_thresholded_image(color_warped) image_shape = binary_warped.shape lane_detection_output = visualize_binary_thresholded_image(color_warped, binary_warped, binary_arr[0], binary_arr[1])
def createAllImage(fullPath): print "generate image for path:%s" % fullPath try: ImageUtil.resize(fullPath, 80, 'tb') ImageUtil.resize(fullPath, 180, 'cv') ImageUtil.resize(fullPath, 640, 'nm') except: print 'encounter error'
def __init__(self, window, id, bitmaps, palette=None, mask_color=None, width=None, height=None, tiled=False, transparent_color=None, mutable=False, current_bitmap=0): super().__init__(window, id, palette, mutable) self._data = [] if isinstance(bitmaps, str): bitmaps = [bitmaps] assert (isinstance(bitmaps, list)) self._bitmap_count = len(bitmaps) self._width = width self._height = height self._tiled = tiled self._mask_color = mask_color self._bitmap_width = 0 self._bitmap_height = 0 self._transparent_color = transparent_color self._current_bitmap = current_bitmap color_data = [] for bitmap in bitmaps: self._bitmap_width, self._bitmap_height, data = ImageUtil.load( bitmap) color_data.extend(data) assert self._bitmap_width > 0 and self._bitmap_height > 0 self._palette, self._data = window.extend_color( color_data, self._palette) self._current = 0 assert (len(self._data) == self._bitmap_width * self._bitmap_height * self._bitmap_count) if self._width is None: self._width = self._bitmap_width if self._height is None: self._height = self._bitmap_height
def _fill_rect(self, window_line_buf, window, y, block_x): width = window.width if self._width == -1 else self._width height = window.height if self._height == -1 else self._height bg_color_start = self.color(window, self._bgcolor_start) bg_color_end = self.color(window, self._bgcolor_end) steps = 1 if self._gradient_mode == GradientMode.LEFT_TO_RIGHT: steps = width - self._border_weight * 2 elif self._gradient_mode == GradientMode.TOP_TO_BOTTOM: steps = height - self._border_weight * 2 elif self._gradient_mode == GradientMode.TOP_LEFT_TO_BOTTOM_RIGHT or \ self._gradient_mode == GradientMode.BOTTOM_LEFT_TO_TOP_RIGHT: steps = (width - self._border_weight * 2) * ( height - self._border_weight * 2) R_delta = ((bg_color_end >> 16 & 0xFF) - (bg_color_start >> 16 & 0xFF)) / steps G_delta = ((bg_color_end >> 8 & 0xFF) - (bg_color_start >> 8 & 0xFF)) / steps B_delta = ((bg_color_end >> 0 & 0xFF) - (bg_color_start >> 0 & 0xFF)) / steps color = bg_color_start step = 0 for x in range(block_x + self._border_weight, block_x + width - self._border_weight): if self._gradient_mode == GradientMode.NONE: factor = 0 elif self._gradient_mode == GradientMode.LEFT_TO_RIGHT: factor = x - block_x + self._border_weight elif self._gradient_mode == GradientMode.TOP_TO_BOTTOM: factor = y elif self._gradient_mode == GradientMode.TOP_LEFT_TO_BOTTOM_RIGHT: factor = (x - block_x + self._border_weight) * y elif self._gradient_mode == GradientMode.BOTTOM_LEFT_TO_TOP_RIGHT: factor = (x - block_x + self._border_weight) * ( height - self._border_weight - y) else: raise Exception('Unknown gradient mode <%s>' % self._gradient_mode.name) color = ImageUtil.color_add(bg_color_start, R_delta * factor, G_delta * factor, B_delta * factor) window_line_buf[x] = color
def draw_line(self, window_line_buf, window, y, block_x): top = self.top_line() color = self.get_color(self._color) left = block_x for glyph in self._glyphs: col = left + glyph.left left += glyph.advance_x if not 0 <= y - glyph.top + top < glyph.height: continue offset = (y - glyph.top + top) * glyph.pitch for x in range(glyph.width): if glyph.monochrome: bit = glyph.data[offset + (x >> 3)] bit = bit & (128 >> (x & 7)) if 0 <= col < window.width and bit > 0: window_line_buf[col] = color else: intensity = glyph.data[offset + x] if 0 <= col < window.width and intensity > 0: window_line_buf[col] = ImageUtil.blend_pixel( window_line_buf[col], color, intensity) col += 1
def _fill_rect(self, window_line_buf, window, y, block_x): if self._gradient_mode == GradientMode.NONE: return width = window.width if self._width == 0 else self._width height = window.height if self._height == 0 else self._height bg_color_start = self.get_color(self._bgcolor_start) bg_color_end = self.get_color(self._bgcolor_end) color_steps = 1 fill_width = width - self._border_weight * 2 fill_height = height - self._border_weight * 2 if self._gradient_mode == GradientMode.LEFT_TO_RIGHT: color_steps = fill_width elif self._gradient_mode == GradientMode.TOP_TO_BOTTOM: color_steps = fill_height elif self._gradient_mode == GradientMode.TOP_LEFT_TO_BOTTOM_RIGHT or \ self._gradient_mode == GradientMode.BOTTOM_LEFT_TO_TOP_RIGHT: color_steps = fill_width * fill_height elif self._gradient_mode == GradientMode.CORNER_TO_CENTER: color_steps = (fill_width * fill_height) >> 2 r_start, g_start, b_start = ImageUtil.rgb(bg_color_start) r_end, g_end, b_end = ImageUtil.rgb(bg_color_end) r_delta = (r_end - r_start) / color_steps g_delta = (g_end - g_start) / color_steps b_delta = (b_end - b_start) / color_steps for x in range(block_x + self._border_weight, block_x + width - self._border_weight): fill_x = x - (block_x + self._border_weight) fill_y = y - self._border_weight if self._gradient_mode == GradientMode.SOLID: factor = 0 elif self._gradient_mode == GradientMode.LEFT_TO_RIGHT: factor = fill_x elif self._gradient_mode == GradientMode.TOP_TO_BOTTOM: factor = y elif self._gradient_mode == GradientMode.TOP_LEFT_TO_BOTTOM_RIGHT: factor = fill_x * fill_y elif self._gradient_mode == GradientMode.BOTTOM_LEFT_TO_TOP_RIGHT: factor = fill_x * (fill_height - fill_y) elif self._gradient_mode == GradientMode.CORNER_TO_CENTER: if fill_x <= (fill_width >> 1): factor_x = fill_x else: factor_x = fill_width - fill_x if fill_y <= (fill_height >> 1): factor_y = fill_y else: factor_y = (fill_height - fill_y) factor = factor_x * factor_y else: raise Exception('Unknown gradient mode <%s>' % self._gradient_mode.name) color = ImageUtil.color_add(bg_color_start, r_delta * factor, g_delta * factor, b_delta * factor) if 0 <= x < window.width: window_line_buf[x] = color
if show_title: ax4.set_title('Combined thresholds', fontsize=16) # ax4.imshow(combined_binary, cmap='gray') # plt.show() f.canvas.draw() data = np.fromstring(f.canvas.tostring_rgb(), dtype=np.uint8, sep='') data = data.reshape(f.canvas.get_width_height()[::-1] + (3,)) return data for image_path in glob.glob(TEST_IMAGES_PATH): image = cv2.imread(image_path) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) undistorted_img = _camera_calibrator.undistort(image) color_warped = _perspective_transformer.warp_image(undistorted_img, M) binary_warped, binary_arr = ImageUtil.binary_thresholded_image(color_warped) # visualize_binary_thresholded_image(color_warped, binary_warped, binary_arr[0], binary_arr[1], True) def visualize_peaks_in_binary_warped_image(): f, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4)) f.tight_layout(w_pad=4.0) ax1.set_title('Warped binary image', fontsize=16) # ax1.imshow(binary_warped, cmap='gray') histogram = np.sum(binary_warped[binary_warped.shape[0]/2:,:], axis=0) ax2.plot(histogram) ax2.set_title('Histogram of lower half image', fontsize=16) ax2.set_xlabel('Pixel positions') ax2.set_ylabel('Counts') # plt.show() # visualize_peaks_in_binary_warped_image()