def main(): args = parser.parse_args() images = pd.read_json(args.file) if (args.extract_histogram): images['histogram'] = np.empty if (args.extract_structure): images['structure'] = np.empty if (args.extract_features): images['keypoints'] = np.empty images['descriptors'] = np.empty for idx, row in images.iterrows(): print(f'Processing images: {idx+1} of {len(images)}', end='\r') image = helper.load_image(row['image']) image = helper.resize(image, width=args.width, height=args.height) if (args.extract_histogram): images.at[idx, 'histogram'] = helper.calcHist(image, args.bins) if (args.extract_structure): structure = image if args.color else helper.convert_to_black_white( image) images.at[idx, 'structure'] = helper.resize(structure, args.size, args.size) if (args.extract_features): key, desc = helper.extract_from_image(image, args.keypoints) images.at[idx, 'keypoints'], images.at[idx, 'descriptors'] = key, desc print('\nDone!') images.to_pickle(args.output)
def telemetry(sid, data): # The current steering angle of the car steering_angle = data["steering_angle"] # The current throttle of the car throttle = data["throttle"] # The current speed of the car speed = data["speed"] # The current image from the center camera of the car imgString = data["image"] image = Image.open(BytesIO(base64.b64decode(imgString))) image_array = np.asarray(image) image_array = helper.crop(image_array, 0.35, 0.1) image_array = helper.resize(image_array, new_dim=(64, 64)) transformed_image_array = image_array[None, :, :, :] # This model currently assumes that the features of the model are just the images. Feel free to change this. steering_angle = float(model.predict(transformed_image_array, batch_size=1)) # The driving model currently just outputs a constant throttle. Feel free to edit this. throttle = 0.3 print('{:.5f}, {:.1f}'.format(steering_angle, throttle)) send_control(steering_angle, throttle)
def telemetry(sid, data): if data: # The current steering angle of the car steering_angle = data["steering_angle"] # The current throttle of the car throttle = data["throttle"] # The current speed of the car speed = data["speed"] # The current image from the center camera of the car imgString = data["image"] image = Image.open(BytesIO(base64.b64decode(imgString))) image_array = np.asarray(image) image_array = helper.crop(image_array, 0.35, 0.1) image_array = helper.resize(image_array, new_dim=(64, 64)) transformed_image_array = image_array[None, :, :, :] steering_angle = float( model.predict(transformed_image_array, batch_size=1)) throttle = controller.update(float(speed)) print(steering_angle, throttle) send_control(steering_angle, throttle) # save frame if args.image_folder != '': timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3] image_filename = os.path.join(args.image_folder, timestamp) image.save('{}.jpg'.format(image_filename)) else: # NOTE: DON'T EDIT THIS. sio.emit('manual', data={}, skip_sid=True)
def document_warper(image, get_rgb=True): ratio = image.shape[0] / 500.0 orig = image.copy() image = resize(image, height=500) edged = edge_detection(image) cnts, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5] screenCnt = None for c in cnts: peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.01 * peri, True) # cv2.drawContours(image, [approx], -1, (0, 255, 0), 2) # cv2.imshow("Outline", image) # cv2.waitKey(0) # cv2.destroyAllWindows() if len(approx) == 4: screenCnt = approx break # print("STEP 2: Find contours of paper") # cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2) # cv2.imshow("Outline", image) # cv2.waitKey(0) # cv2.destroyAllWindows() try: warped = warper(orig, screenCnt, ratio, get_rgb) except AttributeError: raise FourPointException() return warped
def resize_image(self): width = self.lineEditWidth.text() height = self.lineEditHeight.text() if(width =='' or height == ''): return try: width = int(width) height = int(height) except TypeError as e: return if( width > 0 and height > 0): self._edited_image = helper.resize(self._edited_image, width, height) self.update_pixmap()
def telemetry(sid, data): image_array = np.asarray(image) image_array = helper.crop(image_array, 0.35, 0.1) image_array = helper.resize(image_array, new_dim=(64, 64)) transformed_image_array = image_array[None, :, :, :] # This model currently assumes that the features of the model are just the images. Feel free to change this. steering_angle = float(model.predict(transformed_image_array, batch_size=1)) # The driving model currently just outputs a constant throttle. Feel free to edit this. throttle = 0.3 print('{:.5f}, {:.1f}'.format(steering_angle, throttle)) send_control(steering_angle, throttle)
def telemetry(sid, data): if data: # The current steering angle of the car steering_angle = data["steering_angle"] # The current throttle of the car throttle = data["throttle"] # The current speed of the car speed = data["speed"] # The current image from the center camera of the car imgString = data["image"] image = Image.open(BytesIO(base64.b64decode(imgString))) image_array = np.asarray(image) image_array = helper.crop(image_array, 0.35, 0.1) image_array = helper.resize(image_array, new_dim=(64, 64)) transformed_image_array = image_array[None, :, :, :] # This model currently assumes that the features of the model are just the images. Feel free to change this. steering_angle = float( model.predict(transformed_image_array, batch_size=1)) # The driving model currently just outputs a constant throttle. Feel free to edit this. throttle = 0.3 print('{:.5f}, {:.1f}'.format(steering_angle, throttle)) send_control(steering_angle, throttle) # save frame if args.image_folder != '': timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3] image_filename = os.path.join(args.image_folder, timestamp) image.save('{}.jpg'.format(image_filename)) else: # NOTE: DON'T EDIT THIS. sio.emit('manual', data={}, skip_sid=True)
ap.add_argument("--image", required = True, help = "Path to the image to be scanned") # parsing the argument args = vars(ap.parse_args()) # loading the image image = cv2.imread(args["image"]) # making a copy of the image orig = image.copy() # computing the ratio of original image to new height ratio = image.shape[0] / 650.0 # resizing the image image = helper.resize(image, height = 650) # convert the image to grayscale, blur it gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5, 5), 0) # added # converting the image into black and white with # appropriate threshold value to remove illumination defect ret,gray = cv2.threshold(gray,122,255,cv2.THRESH_BINARY) # finding all the edges in the image edged = cv2.Canny(gray, 75, 200) # show the original image and the edge detected image print ("Detecting all the edges by canny edge detection.")
elif diff < 0: thisRobot.roverMovePointTurn_rel(-80) thisRobot.roverMoveForward(10) thisRobot.roverMoveStart() continue elif dist_route < ROUTE_WIDTH / 2: thisRobot.mode = VIS else: thisRobot.mode = GPS if thisRobot.mode is VIS: ret, frame = cap.read() image_array = np.asarray(frame) image_array = helper.crop(image_array, 0.5, 0.2) image_array = helper.resize(image_array, new_dim=(64, 64)) HSV_image_array = cv2.cvtColor(image_array, cv2.COLOR_BGR2HSV) transformed_image_array = HSV_image_array[None, :, :, :] transformed_image_array = tf.cast(transformed_image_array, tf.float32) steering_angle = float(model.predict(transformed_image_array, batch_size=1)) INT_steering = int(steering_angle * 320) if INT_steering > 250: INT_steering = 250 elif INT_steering < -250: INT_steering = -250 INT_steering += 250 # =======================Serial Write=========================== dataFormat = "<{},510,1>".format(INT_steering) thisRobot.ser_2.write(dataFormat.encode()) elif thisRobot.mode is GPS:
def with_shelf_bg(offset, fg_ids, grouped=False, stacked=False): if grouped: annotation = bg_annotation_grouped else: annotation = bg_annotation # Sample set of backgrounds bg_ids = np.random.choice(len(annotation), NUM_SHELF_BG_SAMPLE, replace=False) anno_sub = [annotation[bg_id] for bg_id in bg_ids] bg_img_sub = [bg_img[bg_id] for bg_id in bg_ids] for idx, anno_f in enumerate(anno_sub): with open(anno_f) as anno_file: annos = anno_file.readlines() np.random.shuffle(annos) # Read background images im_bg = cv2.imread(bg_img_sub[idx]) # Resize background images according to target sizes im_scale, im_bg = resize(im_bg, TARGET_SIZE, MAX_SIZE) im = im_bg row, col, ch = im_bg.shape print('Processing: {}'.format(anno_f)) # Put entry corresponding to this background im_name = 'im_{num:05}.jpg'.format(num=offset + idx) aug_annotation_file.write('#\n') aug_annotation_file.write(im_name + '\n') aug_annotation_file_with_cat.write('#\n') aug_annotation_file_with_cat.write(im_name + '\n') aug_annotation_file_small_boxes.write('#\n') aug_annotation_file_small_boxes.write(im_name + '\n') # For each annotated bounding box for cnt, anno in enumerate(annos): # Change the bounding box according to resized images bbox = np.array([float(cor) for cor in anno.strip().split()]) bbox[0] = bbox[0] * im_scale bbox[1] = bbox[1] * im_scale bbox[2] = bbox[2] * im_scale bbox[3] = bbox[3] * im_scale # Add jitter for randomness for box positions jitter1 = np.random.randint(-5, 5, 1) jitter2 = np.random.randint(-5, 5, 1) bbox[0] = min(max(int(bbox[0]) + jitter1, 0), col - 1) bbox[1] = min(max(int(bbox[1]) + jitter2, 0), row - 1) bbox[2] = min(max(int(bbox[2]) + jitter1, 0), col - 1) bbox[3] = min(max(int(bbox[3]) + jitter2, 0), row - 1) dst = np.array([[int(x), int(y)] for x in bbox[0::2] for y in bbox[1::2]]) # Place on type of product in this box im_fg_path, _ = fg_ids[cnt % len(fg_ids)].strip().split('.') bkg_reduced_file = im_fg_path + '_bkg_reduced.jpg' im, n_rects, _ = wrap_fg_bg(im, bkg_reduced_file, dst, stacked) # Write entry for small boxes for rect_id, rects in enumerate(n_rects): for rect in rects: im_fg_cat = os.path.dirname(im_fg_path) id = cat_map.index(im_fg_cat) + 1 aug_annotation_file_small_boxes.write( str(int(rect[0])) + ' ' + str(int(rect[1])) + ' ' + str(int(rect[2])) + ' ' + str(int(rect[3])) + ' ' + str(id) + '\n') n_rects = [[bbox]] # Write entry for big boxes for rect_id, rects in enumerate(n_rects): for rect in rects: aug_annotation_file.write( str(int(rect[0])) + ' ' + str(int(rect[1])) + ' ' + str(int(rect[2])) + ' ' + str(int(rect[3])) + '\n') im_fg_cat = os.path.dirname(im_fg_path) id = cat_map.index(im_fg_cat) + 1 aug_annotation_file_with_cat.write( str(int(rect[0])) + ' ' + str(int(rect[1])) + ' ' + str(int(rect[2])) + ' ' + str(int(rect[3])) + ' ' + str(id) + '\n') # cv2.rectangle(im, (int(rect[0]), int(rect[1])), (int(rect[2]), int(rect[3])), (0, 255, 0), 3) cv2.imwrite(out_img_dir + im_name, im)
if not exists(path_ok): makedirs(path_ok) index = 0 for item in listdir(parent_path)[1:]: if not item.endswith(".png"): continue path_in = join(parent_path, item) index += 1 name = "1{}{:03}.jpg".format(name_list[hoa_index], index) path_out = join(path_ok, name) # if not os.path.exists(path_out): # os.makedirs(path_out) print path_out try: image = cv2.imread(path_in) image = resize(image) image = kmeans(image, 2) image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # ret,image = cv2.threshold(image,127,255,cv2.THRESH_BINARY) # image = cv2.adaptiveThreshold(image,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2) image = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5, 7) cv2.imwrite(path_out, image) # images = [image, th1, th2, th3] # print images # cv2.imshow("OK", th1) except Exception, err: print "Err", err, input # image = cv2.imread("/Users/marsch/Projects/IPAI/FlowerDetect/tools/Hoa/Hoa Canh Buom/Screen Shot 2017-01-10 at 20.21.32.png")
def test_resize(): img1 = os.path.join('sample_data', 'merged.png') img2 = os.path.join('sample_data', 'resized.png') #helper.resize(img1,0.5,img2) helper.resize(img1, 2, img2) helper.show(img2)
img = cv2.imread(path_sub) # rotate img = imutils.rotate( img, rotate) if rotate is not 0 else img # scale img = zoomin(img, scale) if scale > 1 else img # grayscale with 3 channels img = cv2.cvtColor( cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), cv2.COLOR_GRAY2RGB) if grayscale else img # normalize and filter img = normalize(img, EXPECTED_MAX, MEDIAN_VALUE) # gather stat stat[img.shape] = stat[ img.shape] + 1 if img.shape in stat else 1 r = resize(img, EXPECTED_SIZE) append_data_and_label(r, i, data, labels) except Exception as err: print(err) print(path_sub) sys.exit(0) i += 1 count += 1 bar.update(count) bar.finish() print('{} records saved'.format(data.nrows)) # write label index as json file with open(index_file, 'w') as f: json.dump(label_indexes, f)