def __getitem__(self, index): img_path = self.img_files[index] # label_path = self.label_files[index] model_stride = 2**4 labfile = splitext(img_path)[0] + '.txt' L = readShapes(labfile) img = cv2.imread(img_path) # BGR assert img is not None, 'File Not Found ' + img_path data = [img, L[0]] img, labels = process_data_item(data, self.img_size, model_stride) # labels = [] # if os.path.isfile(label_path): # with open(label_path, 'r') as file: # for line in file: # data = line.strip().split(',') # labels = data[1:9] # text = data[9] # Normalize img = img[:, :, ::-1] - np.zeros_like(img, dtype=np.float32) img = img.transpose(2, 0, 1) # BGR to RGB, to 3x416x416 # img = np.ascontiguousarray(img, dtype=np.float32) # uint8 to float32 # img /= 255.0 # 0 - 255 to 0.0 - 1.0 # labels = np.array(labels).reshape(2,4) labels = np.ascontiguousarray(labels, dtype=np.float32) labels = torch.from_numpy(labels) # labels = labels.requires_grad_() img = torch.from_numpy(img) # img = img.requires_grad_() return img, labels #, (h, w)
def genOutput(img_path, output_dir, bname): I = cv2.imread(img_path) detected_cars_labels = '%s/%s_cars.txt' % (output_dir, bname) Lcar = lread(detected_cars_labels) sys.stdout.write('%s' % bname) if Lcar: for i, lcar in enumerate(Lcar): draw_label(I, lcar, color=YELLOW, thickness=3) lp_label = '%s/%s_%dcar_lp.txt' % (output_dir, bname, i) lp_label_str = '%s/%s_%dcar_lp_str.txt' % (output_dir, bname, i) if isfile(lp_label): Llp_shapes = readShapes(lp_label) pts = Llp_shapes[0].pts * lcar.wh().reshape( 2, 1) + lcar.tl().reshape(2, 1) ptspx = pts * np.array(I.shape[1::-1], dtype=float).reshape( 2, 1) draw_losangle(I, ptspx, RED, 3) if isfile(lp_label_str): with open(lp_label_str, 'r') as f: lp_str = f.read().strip() llp = Label(0, tl=pts.min(1), br=pts.max(1)) write2img(I, llp, lp_str) sys.stdout.write(',%s' % lp_str) cv2.imwrite('%s/%s_output.png' % (output_dir, bname), I, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
def sample_validate(image_abs_path, gt_file): L = readShapes(gt_file)[0] pts_transformed = L.pts model_stride = 16.0 image_ndarray = cv2.imread(image_abs_path) tl, br = pts_transformed.min(1), pts_transformed.max(1) llp = Label(0, tl, br) YY, is_there_plate = labels2output_map(llp, pts_transformed, 304, 304, model_stride, L.cls, image_abs_path) print('There is place: %s | Class %s ' % (str(is_there_plate), L.cls)) print('Car ') print(np.matrix(YY[..., 0])) print('Non-object') print(np.matrix(YY[..., 10])) print('Moto') print(np.matrix(YY[..., 9]))
def generate_outputs(input_dir, output_dir): img_files = image_files_from_folder(input_dir) for img_file in img_files: bname = splitext(basename(img_file))[0] I = cv2.imread(img_file) detected_cars_labels = '%s/%s_cars.txt' % (output_dir, bname) Lcar = lread(detected_cars_labels) sys.stdout.write('%s' % bname) if Lcar: for i, lcar in enumerate(Lcar): draw_label(I, lcar, color=YELLOW, thickness=3) lp_label = '%s/%s_%dcar_lp.txt' % (output_dir, bname, i) lp_label_str = '%s/%s_%dcar_lp_str.txt' % (output_dir, bname, i) if isfile(lp_label): Llp_shapes = readShapes(lp_label) pts = Llp_shapes[0].pts * lcar.wh().reshape(2, 1) + lcar.tl().reshape(2, 1) ptspx = pts * np.array(I.shape[1::-1], dtype=float).reshape(2, 1) draw_losangle(I, ptspx, RED, 3) if isfile(lp_label_str): with open(lp_label_str, 'r') as f: lp_str = f.read().strip() llp = Label(0, tl=pts.min(1), br=pts.max(1)) write2img(I, llp, lp_str) sys.stdout.write(',%s' % lp_str) cv2.imwrite('%s/%s_output.png' % (output_dir, bname), I)
def labelImgShow(): YELLOW = ( 0,255,255) RED = ( 0, 0,255) img_file = '/home/yaokun/code/alpr-unconstrained/samples/train-detector/00011.jpg' lp_label = '/home/yaokun/code/alpr-unconstrained/samples/train-detector/00011.txt' I = cv2.imread(img_file) if isfile(lp_label): #pt1 = tuple(pts[:,i].astype(int).tolist()) #pt2 = tuple(pts[:,(i+1)%4].astype(int).tolist()) #cv2.line(I,pt1,pt2,color,thickness) Llp_shapes = readShapes(lp_label) print(np.array(I.shape[1::-1]).reshape(2,1)) print(Llp_shapes[0].pts) pts = Llp_shapes[0].pts*np.array(I.shape[1::-1],dtype=float).reshape(2,1) print(pts) #ptspx = pts*np.array(I.shape[1::-1],dtype=float).reshape(2,1) draw_losangle(I,pts,RED,3) show(I)
key_roll_decrease = LESS_THAN key_perspective_reset = HOME zoom_factor = 1. action_keys = [key_exit, key_previous] + key_next curr_image = 0 while curr_image < len(img_files): img_file = img_files[curr_image] lab_file = splitext(img_file)[0] + '.txt' if isfile(lab_file): shapes = readShapes(lab_file, obj_type=ShapeDisplay) selected = len(shapes) - 1 else: shapes, selected = [ShapeDisplay()], 0 zoom_factor = 1. disp = Display(cv2.imread(img_file), maxW, maxH) disp.show() key = disp.waitKey() typing_mode = False while not key in action_keys: disp.resetDisplay() displayAllShapes(disp, shapes, selected, typing_mode) disp.show()
if not isdir(outdir): makedirs(outdir) model, model_stride, xshape, yshape = load_network(args.model, dim) opt = getattr(keras.optimizers, args.optimizer)(lr=args.learning_rate) model.compile(loss=loss, optimizer=opt) print('Checking input directory...') Files = image_files_from_folder(train_dir) Data = [] for file in Files: labfile = splitext(file)[0] + '.txt' if isfile(labfile): L = readShapes(labfile) I = cv2.imread(file) Data.append([I, L[0]]) print('%d images with labels found' % len(Data)) dg = DataGenerator( data=Data, \ process_data_item_func=lambda x: process_data_item(x,dim,model_stride),\ xshape=xshape, \ yshape=(yshape[0],yshape[1],yshape[2]+1), \ nthreads=2, \ pool_size=1000, \ min_nsamples=100 ) dg.start() Xtrain = np.empty((batch_size, dim, dim, 3), dtype='single')
Lcar = lread(detected_cars_labels) sys.stdout.write('%s' % bname) if Lcar: for i, lcar in enumerate(Lcar): draw_label(I, lcar, color=YELLOW, thickness=3) lp_label = '%s/%s_%dcar_lp.txt' % (output_dir, bname, i) lp_label_str = '%s/%s_%dcar_lp_str.txt' % (output_dir, bname, i) if isfile(lp_label): Llp_shapes = readShapes(lp_label) pts = Llp_shapes[0].pts * lcar.wh().reshape( 2, 1) + lcar.tl().reshape(2, 1) ptspx = pts * np.array(I.shape[1::-1], dtype=float).reshape( 2, 1) draw_losangle(I, ptspx, RED, 3) if isfile(lp_label_str): with open(lp_label_str, 'r', encoding='utf-8') as f: lp_str = f.read().strip() llp = Label(0, tl=pts.min(1), br=pts.max(1)) I = write2img(I, llp, lp_str) sys.stdout.write(',%s' % lp_str) cv2.imwrite('%s/%s_output.png' % (output_dir, bname), I)
file_gr_transformed.close() salvar_imagem(img_ndarray, caminho_completo_saida) # if not possui_bbox: # nome_arquivo = 'BAD_SAMPLE_'+nome_arquivo # caminho_completo_saida_bbox = os.path.abspath(os.path.join(diretorio_saida, nome_arquivo)) def salvar_imagem(imagem_array, caminho_completo_arquivo): cv2.imwrite(caminho_completo_arquivo, imagem_array) if __name__ == '__teste3__': data_item = [] # L = readShapes('/media/jones/dataset/alpr/lotes_rotulacao/preprocessados/train/lote1_1703_2000000544.txt') # file = '/media/jones/dataset/alpr/lotes_rotulacao/preprocessados/train/lote1_1703_2000000544.jpg' L = readShapes('samples/034_1304_1354_1705_0000_0130m000000041.txt') file = 'samples/034_1304_1354_1705_0000_0130m000000041.jpg' # I = cv2.imread(file) dim_w = 304 dim_h = 304 # data_item.append([file, L[0]]) data_item.append(file) data_item.append(L[0]) model_stride = 16.0 nome_arquivo = '034_1304_1354_1705_0000_0130m000000041' nome_arquivo_imagem = 'samples/' + nome_arquivo + '_transf.jpg' nome_arquivo_gt = 'samples/' + nome_arquivo + '_transf.txt' XX, YY_output_map, pts_transformed = process_data_item( data_item, dim_w, dim_h, model_stride) sample_height, sample_width = XX.shape[:2] # x_points = (pts_transformed[0:]*sample_width).tolist()
batch_size = args.batch_size dim = 304 model,model_stride,xshape,yshape = load_network(args.model,dim) opt = getattr(keras.optimizers,args.optimizer)(lr=args.learning_rate) model.compile(loss=loss_mc, optimizer=opt) # model.compile(loss=loss, optimizer=opt) print('yshape: %s ' % str(yshape)) for layer in model.layers: print(layer.name) print(layer.output_shape) print('Checking input directory...') Files = image_files_from_folder(train_dir) dataset_samples = [] for file in Files: # file = Files[0] labfile = splitext(file)[0] + '.txt' gt_list = readShapes(labfile) for gt_sample in gt_list: dataset_samples.append([file, gt_sample]) XX, YY, pts_transformed = process_data_item(dataset_samples[0], dim, dim, model_stride) Xtrain = np.empty((batch_size, dim, dim, 3), dtype='single') Ytrain = np.empty((batch_size, int(dim / model_stride), int(dim / model_stride), 2 * 4 + 3)) Xtrain[0] = XX Ytrain[0] = YY XX, YY, pts_transformed = process_data_item(dataset_samples[1], dim, dim, model_stride) Xtrain[1] = XX Ytrain[1] = YY print('starting train') train_loss = model.train_on_batch(Xtrain, Ytrain) print('loss %f ' % train_loss) # print(flags)