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 prepare_pneumonia_data(self): print('== Preparing kaggle data') normal_fn = os.path.join(self.pneumonia_path, 'stage_2_detailed_class_info.csv') pneu_fn = os.path.join(self.pneumonia_path, 'stage_2_train_labels.csv') csv_normal = pd.read_csv(normal_fn, nrows=None) csv_pneu = pd.read_csv(pneu_fn, nrows=None) labels = {'normal': [], 'pneumonia': []} train = [] test = [] for index, row in csv_normal.iterrows(): if row['class'] == 'Normal': labels['normal'].append(row['patientId']) for index, row in csv_pneu.iterrows(): if int(row['Target']) == 1: labels['pneumonia'].append(row['patientId']) for key in labels.keys(): list_fns = labels[key] if len(list_fns) == 0: continue # fixed evaluation test_patients = np.load( os.path.join(self.pneumonia_path, 'rsna_test_patients_{}.npy'.format(key))) # Counts would be: # test # count: {'normal': 885, 'pneumonia': 1058, 'COVID-19': 10} # train # count: {'normal': 7966, 'pneumonia': 8526, 'COVID-19': 105} for patient in list_fns: fn = os.path.join(self.pneumonia_path, 'stage_2_train_images', patient + '.dcm') ds = dicom.dcmread(fn) img = cv2.cvtColor(ds.pixel_array, cv2.COLOR_GRAY2RGB) img = utils.standardize_img( cv2.resize(img, (self.resize, self.resize)).astype('float32')) meta = { 'dataset': 'neumonia_kaggle_Dataset', 'id': patient, 'filename': fn, 'image': img, 'label': key, 'train': 0 if patient in test_patients else 1 } if patient in test_patients: test.append(meta) self.test_count[key] += 1 else: train.append(meta) self.train_count[key] += 1 print('label {} read and packed!'.format(key)) training_patients = [ex['id'] for ex in train] test_patients = [ex['id'] for ex in test] assert len(set(training_patients).intersection( set(test_patients))) == 0 for lb in labels.keys(): train_label = [ex for ex in train if ex['label'] == lb] test_label = [ex for ex in test if ex['label'] == lb] print('saving records for label: {}'.format(lb)) if len(train_label) != 0: self.save_data(train_label, lb, 'kaggle') if len(test_label) != 0: self.save_data(test_label, lb, 'kaggle', training=False) print('test count: ', self.test_count) print('train count: ', self.train_count)
def prepare_covid_xray(self): print('== Preparing covid19 data') csv = pd.read_csv(os.path.join(args.covid_path, 'metadata.csv'), nrows=None) idx_pa = csv["view"] == "PA" csv = csv[idx_pa] train = [] test = [] labels = {'normal': [], 'pneumonia': [], 'COVID-19': []} for index, row in csv.iterrows(): f = row['finding'] if f in self.mapping: self.data_counts[self.mapping[f]] += 1 entry = [ int(row['patientid']), row['filename'], self.mapping[f] ] labels[self.mapping[f]].append(entry) print('Data distribution from covid-chestxray-dataset:') print(self.data_counts) for key in labels.keys(): data_list = labels[key] if len(data_list) == 0: print('No data for {} in this dataset'.format(key)) continue print('Key: ', key) print('Test patients: ', self.test_dict_persons[key]) # go through all the patients for patient in data_list: fn = os.path.join(args.covid_path, 'images', patient[1]) img = cv2.resize(utils.imread(fn), (self.resize, self.resize)) # print(img.shape) meta = { 'dataset': 'covid-chestxray-dataset', 'patient_id': patient, 'filename': fn, 'image': utils.standardize_img(img.astype('float32')), 'label': key, 'train': 0 if str(patient[0]) in self.test_dict_persons[key] else 1 } # print(0 if patient[0] in self.test_dict_persons[key] else 1) # print(patient[0], self.test_dict_persons[key]) if meta['train'] == 0: test.append(meta) self.test_count[patient[2]] += 1 else: train.append(meta) self.train_count[patient[2]] += 1 for lb in labels.keys(): train_label = [ex for ex in train if ex['label'] == lb] test_label = [ex for ex in test if ex['label'] == lb] print('saving records for label: {}'.format(lb)) if len(train_label) != 0: self.save_data(train_label, lb, 'chestxray') if len(test_label) != 0: self.save_data(test_label, lb, 'chestxray', False) print('test count: ', self.test_count) print('train count: ', self.train_count)
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]
except: print("calibration failed") 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