def get_all_objs(): a, img_work = utils.take_workspace_img(client) img_work = utils.standardize_img(img_work) if not a: a, img_work = debug_markers(img_work) return img_work, None mask = utils.objs_mask(img_work) objs = utils.extract_objs(img_work, mask) if len(objs) == 0: return img_work, [] imgs = [] if model is None: return img_work, objs for x in range(len(objs)): imgs.append(cv2.resize(objs[x].img, (64, 64))) imgs = np.array(imgs) predictions = model.predict(imgs) for x in range(len(predictions)): obj = objs[x] obj.type = predictions[x].argmax() # graphical debug cv2.drawContours(img_work, [obj.box], 0, (0, 0, 255), 2) pos = [obj.x + 20, obj.y] # Text position img_work = cv2.putText(img_work, objects_names[obj.type], tuple(pos), font, 0.5, (255, 0, 0), 1, cv2.LINE_AA) pos[0] += img_work.shape[0] img_work = cv2.putText(img_work, objects_names[obj.type], tuple(pos), font, 0.5, (0, 255, 0), 1, cv2.LINE_AA) return img_work, objs
def labelling(client, name): try: os.mkdir("./data/" + name) except: pass print("label ", name) a, img_work = utils.take_workspace_img(client, 1.5) mask = utils.objs_mask(img_work) debug = concat_imgs([img_work, mask], 1) if __name__ == '__main__': show_img("robot view", debug, wait_ms=1) objs = utils.extract_objs(img_work, mask) if len(objs) != 0: print(str(len(objs)) + " object detected") objs[0].img = resize_img(objs[0].img, width=64, height=64) if __name__ == '__main__': show_img("robot view2", img_work, wait_ms=50) print("saved", name) cv2.imwrite("./data/" + name + "/" + str(time.time()) + ".jpg", img_work) else: print(str(len(objs)) + " object detected") return img_work
def load_dataset(data_path): #list all directories in data_path objects_names = os.listdir(data_path) print(objects_names) objects_list = [] #list containing all objects images labels_list = [] #list containing all objects Labels files_names = [] #list containing all objects names obj_id = 0 #make the data_mask directorie try: os.mkdir("./data_mask1") except: pass for name in objects_names: # list all image for each objects list_dir = os.listdir(data_path + name) print(name + " " + str(len(list_dir))) # make a "data_mask/<obj_name>" subdirectory for each object try: os.mkdir("./data_mask1/" + name) except: pass #for each file in in "data/<obj_name>"" for file_name in list_dir: # read the file img = cv2.imread(data_path + name + "/" + file_name) # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = utils.standardize_img(img) # extract all objects from the image mask = utils.objs_mask(img) objs = utils.extract_objs(img, mask) #for each object in the image for x in range(0, len(objs)): img = objs[x].img #write the image in data_mask/<file_name>" cv2.imwrite( "data_mask1/" + name + "/" + str(x) + "_" + file_name, img) #resize the image to match our model input img = cv2.resize(img, (64, 64)) #create a numpy array of float with a size of (64, 64, 3) img_float = np.zeros((64, 64, 3), np.float32) #scale the image color between 0.0 and 1.0 and copy it in the numpy array img_float[:][:][:] = img[:][:][:] / 255.0 #create a numpy array full of 0 with a shape of (len(objects_names)) label = np.zeros((len(objects_names)), np.float32) #set is corresponding id to 1 label[obj_id] = 1 #insert all our numpy array in the data set objects_list.append(img_float) labels_list.append(label) files_names.append(str(x) + "_" + file_name) print(len(objs), end='') print("|", end="", flush=True) print("") obj_id += 1 return [objects_list, labels_list, files_names, objects_names]
client.change_tool(RobotTool.GRIPPER_2) while 1: #move to observation_pose client.move_pose(*observation_pose.to_list()) a = False #take a picture of the workspace (infinite loop if the robot can't see the workspace) while not a: a, img_work = utils.take_workspace_img(client) img_work = utils.standardize_img(img_work) #calculate the mask for img_work (black and white image) mask = utils.objs_mask(img_work) #aply the mask to the image img_masked = cv2.bitwise_and(img_work, img_work, mask=mask) img_db = concat_imgs([img_work, mask, img_masked], 1) #get all opbject from the image objs = utils.extract_objs(img_work, mask) if len(objs) == 0: continue imgs = [] #resize all objects img to 64*64 pixels for x in range(len(objs)): imgs.append(resize_img(objs[x].img, width=64, height=64))