def handle_detect_bounding(req): #if req.data: stime = time.time() frame = Image.open( '/home/casch/ws_moveit/src/yolo_detector/imageTest/tmpImage.png') img = frame.resize((width, height), Image.NEAREST) img.save('/home/casch/ws_moveit/src/yolo_detector/imageTest/imgYolo.png') frame = cv2.imread( '/home/casch/ws_moveit/src/yolo_detector/imageTest/imgYolo.png') results = tfnet.return_predict(frame) yoloInstance.detectionDone.data = True #--------------------------------------------------------------- yoloInstance.do_encapsulationBoundingBox(results) yoloInstance.do_bounding_boxes(colors[0], results, frame) yoloInstance.counter += 1 cv2.imwrite( '/home/casch/ws_moveit/src/yolo_detector/imageResult/imageResult.png', yoloInstance.frameBoundingBox) print('FPS: {:.1f} \t Counter: {}'.format((1 / (time.time() - stime)), yoloInstance.counter)) # plt.imshow(yoloInstance.frameBoundingBox) # plt.draw() # plt.pause(3) # plt.close() return CheckForObjectsResponse(yoloInstance.detectionDone.data, yoloInstance.tmpBoundingBox)
def publishCharacterImage(self): from PIL import Image, ImageDraw, ImageFilter # Publish Image with label img = self.img for x, y, label in zip(self.person_x, self.person_y, self.labels): # Draw character image ---> os.chdir(self.PICTURE_URL) char = ch.CHARACTER_PICTURE[int(label)] # Load and resize src = pil_image.cv2pil(img) forward = Image.open( char["filename"]) h, w = forward.size forward = forward.resize((int(h*char["size"]), int(w*char["size"]))) # For convert src = src.convert('RGBA') # Paste c = Image.new('RGBA', src.size, (255, 255, 255, 0)) #src.paste(forward, ( char["x"], char["y"])) #c.paste(forward, ( char["x"] + x, char["y"] + y), forward) c.paste(forward, ( char["x"] + x - int(char["size"]*h/2), char["y"] + y - int(char["size"]*w/2)), forward) result = Image.alpha_composite(src, c) result.save('tmp.jpg', quality=95) img = cv.imread('tmp.jpg') # Write label name to img cv.putText(img, ch.CHARACTER_NAME[int(label)], (x-50,y), cv.FONT_HERSHEY_SIMPLEX, 0.9, (0,0,150), 2, cv.LINE_AA) msg = self.bridge.cv2_to_imgmsg(img, encoding="rgb8") self.image_pub.publish(msg)
def __init__(self): self.model = load_model( os.path.join( rospkg.RosPack().get_path("ras_object_classification"), 'scripts', 'vgg_dropout_v3.h5') ) # objects_nasnet_data_gen_dir_VX5.h5 # objects_vgg16_data_gen_dir_V4.h5 # vgg16_127-5_dataset2_v1.h5 self.model._make_predict_function() # super important !! self.test_image = Image.open( os.path.join( rospkg.RosPack().get_path("ras_object_classification"), 'scripts', 'blue.jpg')) self.image_size = (224, 224) self.dict = { 0: 1, 1: 10, 2: 11, 3: 12, 4: 13, 5: 14, 6: 2, 7: 3, 8: 4, 9: 5, 10: 6, 11: 7, 12: 8, 13: 9 }
def determineLicensePlate(self, cameraImg): # Calls on image cropper which crops the robot's raw camera # image and saves the 5 images to a local folder: competitionImages/ imageCrop(cameraImg) conModel = load_model('ConvolutionModels/LPModel.h5') LP_msg = "" RELATIVE_PATH = "competitionImgs/" # files = os.listdir(RELATIVE_PATH) files = [ "img_0.jpg", "img_1.jpg", "img_2.jpg", "img_3.jpg", "img_4.jpg" ] # for fileName in files[:]: for i in range(5): letterNumImg = np.array(Image.open(RELATIVE_PATH + files[i])) resizedImg = np.reshape(letterNumImg, [1, 39, 36, 3]) predictions = LPModel.predict() index = np.where(predictions == np.amax(predictions)) index = int(index[1]) character = self.answerKey[index] LP_msg = LP_msg + character return LP_msg
def load_depth(): depth_dataset_path = os.path.expanduser('~/Data/depth_data') depth_key = '.pkl' rgb_key = '.png' for dir_name, sub_dirs, files in os.walk(depth_dataset_path): for f in files: load_file = os.path.join(dir_name, f) if depth_key == f[-len(depth_key):]: depth_data = pickle.load(open(load_file, 'rb')) print(depth_data) if rgb_key == f[-len(rgb_key):]: print(f) rgb_data = Image.open(load_file) print(rgb_data) WIDTH = 50 HEIGHT = 25 bridge = CvBridge() try: #color_image = bridge.imgmsg_to_cv2(rgb_data, 'passthrough') depth_image = bridge.imgmsg_to_cv2(depth_data, 'passthrough') except CvBridgeError, e: rospy.logerr(e)
def get_current_observation(self, request): if self.current_obs_index > self.size_of_dataset: self.current_obs_index = 1 print('REPLAY........') current_image_path = self.record_path + str(self.current_obs_index) + '.jpg' image = Image.open(current_image_path) image = cv_to_ros(pil_to_cv(image), self.bridge) self.current_obs_index +=1 print(current_image_path) return CurrentObservationResponse(image)
def saveImg(): bitmap_object = autopy.bitmap.capture_screen() bitmap_object.save('img/path.png') im = Image.open(r"img/path.png") left = 0 top = 720 right = 400 bottom = 1000 im1 = im.crop((left, top, right, bottom)) im1.save("pathreal.png") try: retval, buffer = cv2.imencode('.jpg', cv2.imread("pathreal.png")) jpg_as_text = base64.b64encode(buffer) global fun2 fun2.Call("data:image/jpeg;base64,/" + str(jpg_as_text)[3:-1]) except: pass
def openImage(self): np.set_printoptions(threshold=np.inf) #see all matrix img = Image.open("map.pgm") area = (950, 950, 1600, 1130) #left, top, right, bottom cropped_img = img.crop(area) img_matrix = np.array(cropped_img) #unknown positions of map BW_img_des = img_matrix == 205 BW_img_des = BW_img_des * -1 #occupied positions of the map BW_img_oc = img_matrix == 254 BW_img_oc = BW_img_oc * 1 #0 and 1 instead of False and True return BW_img_des + BW_img_oc
def callback_udp_response(self, msg): #process reponse data if msg.data.find("coarse_target:0:done") is not -1: self.cb6.setChecked(True) self.step1_complete = True elif msg.data.find("fine_target:0:done") is not -1: self.cb8.setChecked(True) elif msg.data.find("target_image") is not -1: ## decode target image data base64ImgData = msg.data.split(":")[ 2] # target_image:0:image_data base 64s # base64 display in GUI rospy.loginfo(rospy.get_caller_id() + "img_str %s", msg.data[0:10]) if msg.data is "" or msg.data is "h": print('img str base64 encode invalid!') return current_image = Image.open(BytesIO(base64.b64decode(msg.data))) p = self.convert_img_pil(current_image) self.paint_pixmap(p) self._view.setPixmap( p) #display the image, self._view is the image viewer
def showResults(self, path): from PIL import Image, ImageFont, ImageDraw import os # print self.object_positions for filename in sorted(os.listdir(path)): if filename.endswith(".png"): print filename[:-4] t = int(filename[:-4]) print t idx = utils.binary_search(t, self.camera_times) if idx+1 == len(self.camera_times): nxtidx = idx idx = idx - 1 else: nxtidx = idx + 1 print idx pos = [utils.interpolate_aprox(t, self.camera_times[idx], \ self.object_positions[idx][i], \ self.camera_times[nxtidx], \ self.object_positions[nxtidx][i]) for i in range(3)] IMAGE_HEIGHT = 701 IMAGE_WIDTH = 801 BIN = 0.1 row = int(round(math.floor(((((IMAGE_HEIGHT*BIN)/2.0) - pos[0])/(IMAGE_HEIGHT*BIN)) * IMAGE_HEIGHT))) column = int(round(math.floor(((((IMAGE_WIDTH*BIN)/2.0) - pos[1])/(IMAGE_WIDTH*BIN)) * IMAGE_WIDTH))) print pos, row, column source_img = Image.open(path + filename) draw = ImageDraw.Draw(source_img) draw.rectangle(((column-10, row-10), (column+10, row+10))) newimgpath = path + "res/" + filename source_img.save(newimgpath, "PNG")
# all outputs are float32 numpy arrays, so convert types as appropriate output_dict['num_detections'] = int(output_dict['num_detections'][0]) output_dict['detection_classes'] = output_dict[ 'detection_classes'][0].astype(np.uint8) output_dict['detection_boxes'] = output_dict['detection_boxes'][0] output_dict['detection_scores'] = output_dict['detection_scores'][0] if 'detection_masks' in output_dict: output_dict['detection_masks'] = output_dict['detection_masks'][0] return output_dict # %% for image_path in TEST_IMAGE_PATHS: image = Image.open(image_path) ##################################tell them to give root address, i.e whole path # the array based representation of the image will be used later in order to prepare the # result image with boxes and labels on it. image_np = load_image_into_numpy_array(image) # Expand dimensions since the model expects images to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims(image_np, axis=0) # Actual detection. output_dict = run_inference_for_single_image(image_np, detection_graph) # Visualization of the results of a detection. vis_util.visualize_boxes_and_labels_on_image_array( image_np, output_dict['detection_boxes'], output_dict['detection_classes'], output_dict['detection_scores'],
def test_diffeo(argv): # Find diffeomorphisms file try: dfile = argv[argv.index('-dl')+1] except ValueError: dfile = '/media/data/learned_diffeo/camera_ptz.dynamics.pickle' print 'Using diffeomorphism from file: ',dfile # Find diffeomorphisms file try: outpath = argv[argv.index('-o')+1] except ValueError: outpath = '/media/data/tempimages/' print 'Saving images to path: ',outpath # Find output prefix file try: prefix = argv[argv.index('-p')+1] except ValueError: prefix = 'logitech_cam_ptz' print 'Using prefix for output files: ',prefix # Find Input image try: infile = argv[argv.index('-im')+1] except ValueError: infile = '/home/adam/git/surf12adam/diffeo_experiments/lighttower640.jpg' print 'Using image file: ',infile # Number of levels to apply try: levels = int(argv[argv.index('-l')+1]) except ValueError: levels = 5 print 'Applying diffeomorphism ',levels,' times' # Image size try: size = eval(argv[argv.index('-size')+1]) except ValueError: size = [160,120] print 'Image size: ',size logfile = open(outpath+prefix+'log.html','w') logfile.write('<html><body><h1>Diffeomorphism test log: '+dfile+'</h1>') logfile.write('Diffeomorphism file: '+dfile+'<br/>') logfile.write('Test image: <br/><img src="'+infile+'.png"/><br/><br/>') logfile.write('<table>\n') logfile.write('<tr align="center">\n <td>Command</td>\n') logfile.write('<td>Diffeomorphism<br/>Angle</td>') logfile.write('<td>Diffeomorphism<br/>Norm</td>') logfile.write('<td>Diffeomorphism<br/>Variance</td>') for i in range(levels+1): logfile.write('<td>'+str(i)+'</td>') logfile.write('</tr>') im = np.array(Image.open(infile).resize(size).getdata(),np.uint8).reshape((size[1],size[0],3)) #Y0,Y1 = get_Y_pair((30,30),(0,0),(160,120),im) # pdb.set_trace() diffeo_list = pickle.load(open(dfile,'rb')) for D in diffeo_list: logfile.write('<tr>') cmdstr = str(D.command).replace(' ','') # outpath+prefix+'diffeo'+cmdstr+'angle'+'.png' logfile.write('<td>') logfile.write(str(D.command)) logfile.write('</td>') Image.fromarray(diffeo_to_rgb_angle(D.d)).save(outpath+prefix+'diffeo'+cmdstr+'angle'+'.png') logfile.write('<td>') logfile.write('<img src="'+outpath+prefix+'diffeo'+cmdstr+'angle'+'.png'+'"/>') logfile.write('</td>') Image.fromarray(diffeo_to_rgb_norm(D.d)).save(outpath+prefix+'diffeo'+cmdstr+'norm'+'.png') logfile.write('<td>') logfile.write('<img src="'+outpath+prefix+'diffeo'+cmdstr+'norm'+'.png'+'"/>') logfile.write('</td>') Image.fromarray((D.variance*255).astype(np.uint8)).save(outpath+prefix+'diffeo'+cmdstr+'variance'+'.png') logfile.write('<td>') logfile.write('<img src="'+outpath+prefix+'diffeo'+cmdstr+'variance'+'.png'+'"/>') logfile.write('</td>') Y = im Image.fromarray(Y).save(outpath+prefix+cmdstr+str(0)+'.png') logfile.write('<td>') logfile.write('<img src="'+outpath+prefix+cmdstr+str(0)+'.png'+'"/>') logfile.write('</td>') for i in range(levels): Y, var = D.apply(Y) Image.fromarray(Y).save(outpath+prefix+cmdstr+str(i+1)+'.png') logfile.write('<td>') logfile.write('<img src="'+outpath+prefix+cmdstr+str(i+1)+'.png'+'"/>') logfile.write('</td>') logfile.write('</tr>') logfile.write('</table></body></html>') logfile.flush() logfile.close()
class predictor: """ """ def apply(self, y0, index): """ Apply diffeomorphism number <index> to image <y0> """ def compare(self, y0, yn, size): """ Compare reduced size of images. Returns norm of difference in certain areas, which is defined by the variance is less than the average variance value. """ def find_neighbor_indices(self, y0): #pdb.set_trace() self.estimator = learner((y0.shape[1], y0.shape[0]), [4, 4]).new_estimator() self.estimator.init_structures(y0[:, :, 0]) #self.neighbor_indices = estimator.neighbor_indices def compare_neighbor(self, y0, yn, var=NONE): """ Compare fullsize of images in neighbour area """ estr = learner((y0.shape[1], y0.shape[0]), [4, 4]).new_estimator() estr.update(y0[:, :, 0], yn[:, :, 0]) estr.update(y0[:, :, 1], yn[:, :, 1]) estr.update(y0[:, :, 2], yn[:, :, 2]) n = len(estr.neighbor_similarity_flat) best_sim = np.array([[0]] * n) for k in range(n): best_sim[k] = max(estr.neighbor_similarity_flat[k]) if var != NONE: best_sim = best_sim * var.flat return np.mean(best_sim) def apply_random_sequence(self, y0, level=5): """ Apply diffeomorphisms on image <y0> in a randomized sequence of depth <level>. Returns the command sequence as a <level>-tuple and the final image. """ def search_for_plan(self, y0, yn, nitr=10, level=5): """ Search for a sequence of diffeomorphisms that transforms <y0> to <yn>. Max <nitr> random sequences is tried, max length of sequences is <level> """ size = [16, 12] for i in range(nitr): # get a estimated final image and the sequence to the final image y_est, seq = self.apply_random_sequence(yo, level) # compare the estimated image against the goal. sim = self.compare(y0, yn, size) def test_compare_neighbor(self, argv): def get_Y_pair((x0, y0), (dx, dy), (w, h), im): imc = im.crop((x0, y0, x0 + w, y0 + h)) Y0 = np.array(imc.getdata(), np.uint8).reshape((h, w, 3)) imc2 = im.crop((x0 + dx, y0 + dy, x0 + dx + w, y0 + dy + h)) Y1 = np.array(imc2.getdata(), np.uint8).reshape((h, w, 3)) return (Y0, Y1) # Find Input image try: infile = argv[argv.index('-im') + 1] except ValueError: infile = '/home/adam/git/surf12adam/diffeo_experiments/lighttower640.jpg' print 'Using image file: ', infile im = Image.open(infile) print 'Compare two images close to each other' y0, y1 = get_Y_pair((260, 300), (2, 0), (160, 120), im) self.find_neighbor_indices(y0) print self.compare_neighbor(y0, y1) print 'Compare two images far from each other' y0, y1 = get_Y_pair((260, 300), (5, 0), (160, 120), im) print self.compare_neighbor(y0, y1) print 'Compare two images really far from each other' y0, y1 = get_Y_pair((260, 300), (200, 0), (160, 120), im) print self.compare_neighbor(y0, y1) print 'Compare two images close to each other with nois' y0, y1 = get_Y_pair((260, 300), (0, 0), (160, 120), im) #pdb.set_trace() y1 = y1 + np.random.randint(0, 10, y0.shape) y1 = (y1 * 255.0 / np.max(y1)).astype(np.uint8) # pdb.set_trace() print self.compare_neighbor(y0, y1)
from transforms3d import quaternions import roslib import sys import rospy from sensor_msgs.msg import Image, Imu import time import math from PIL import Image import matplotlib.pyplot as plt from matplotlib import colors resolution = 0.05 #meters/pixel np.set_printoptions(threshold=4) #see all matrix img = Image.open("map.pgm") area = (950, 950, 1600, 1130) #left, top, right, bottom cropped_img = img.crop(area) img_matrix = np.array(cropped_img) #unknown positions of map BW_img_des = img_matrix == 205 BW_img_des = BW_img_des * - 1 #occupied positions of the map BW_img_oc = img_matrix == 0 BW_img_oc = BW_img_oc* 1 #0 and 1 instead of False and True map = BW_img_des+BW_img_oc length_map = map.shape[1]#no of columns width_map = map.shape[0]#no of rows
#command_list = [[200,0,0],[-200,0,0],[0,150,0],[0,-150,0],[0,0,5],[0,0,-5],[0,0,0]] #command_list = [[0,0,5],[0,0,-5]] # Keep track of the actual zoom for the last command and the current zoom zoom_last = 1.0 zoom = 1.0 out_bag = rosbag.Bag(outfile, 'w') # Load image to crop subimages from if image_str == 'random': M = np.random.randint(0,255,(748,1024,3)).astype(np.uint8) im = Image.fromarray(M) else: im = Image.open(image_str+'.png') cam_sim = logitech_cam_simulation(size,im,scale_factor) # Y0,Y1,cmd = cam_sim.simulate_Y_pair(size,[0,0,50]) print('simulated') def show(Y0,Y1): Image.fromarray(Y0).show() Image.fromarray(Y1).show() #pdb.set_trace() for i in range(duration): print(i) command = command_list[np.random.randint(0,len(command_list))] Y0,Y1,cmd = cam_sim.simulate_Y_pair(size,command) # Image.fromarray(Y0).save('test'+str(i)+'y0cmd'+str(cmd)+'.png') # Image.fromarray(Y1).save('test'+str(i)+'y1cmd'+str(cmd)+'.png')