|
| 1 | +%% script to test dataPreprocessing |
| 2 | +%% created by Fu-Jen Chu on 09/15/2016 |
| 3 | + |
| 4 | +close all |
| 5 | +clear |
| 6 | + |
| 7 | +%parpool(4) |
| 8 | +addpath('/media/fujenchu/home3/data/grasps/') |
| 9 | + |
| 10 | + |
| 11 | +% generate list for splits |
| 12 | +list = [100:949 1000:1034]; |
| 13 | +list_idx = randperm(length(list)); |
| 14 | +train_list_idx = list_idx(length(list)/5+1:end); |
| 15 | +test_list_idx = list_idx(1:length(list)/5); |
| 16 | +train_list = list(train_list_idx); |
| 17 | +test_list = list(test_list_idx); |
| 18 | + |
| 19 | + |
| 20 | +for folder = 1:10 |
| 21 | +display(['processing folder ' int2str(folder)]) |
| 22 | + |
| 23 | +imgDataDir = ['/home/fujenchu/projects/deepLearning/tensorflow-finetune-flickr-style-master/data/grasps/' sprintf('%02d',folder) '_rgd']; |
| 24 | +txtDataDir = ['/home/fujenchu/projects/deepLearning/tensorflow-finetune-flickr-style-master/data/grasps/' sprintf('%02d',folder)]; |
| 25 | + |
| 26 | +%imgDataOutDir = ['/home/fujenchu/projects/deepLearning/tensorflow-finetune-flickr-style-master/data/grasps/' sprintf('%02d',folder) '_Cropped320_rgd']; |
| 27 | +imgDataOutDir = '/media/fujenchu/home3/fasterrcnn_grasp/rgd_multibbs_5_5_5_tf/data/Images'; |
| 28 | +annotationDataOutDir = '/media/fujenchu/home3/fasterrcnn_grasp/rgd_multibbs_5_5_5_tf/data/Annotations'; |
| 29 | +imgSetTrain = '/media/fujenchu/home3/fasterrcnn_grasp/rgd_multibbs_5_5_5_tf/data/ImageSets/train.txt'; |
| 30 | +imgSetTest = '/media/fujenchu/home3/fasterrcnn_grasp/rgd_multibbs_5_5_5_tf/data/ImageSets/test.txt'; |
| 31 | + |
| 32 | +imgFiles = dir([imgDataDir '/*.png']); |
| 33 | +txtFiles = dir([txtDataDir '/*pos.txt']); |
| 34 | + |
| 35 | +logfileID = fopen('log.txt','a'); |
| 36 | +mainfileID = fopen(['/home/fujenchu/projects/deepLearning/deepGraspExtensiveOffline/data/grasps/scripts/trainttt' sprintf('%02d',folder) '.txt'],'a'); |
| 37 | +for idx = 1:length(imgFiles) |
| 38 | + %% display progress |
| 39 | + tic |
| 40 | + display(['processing folder: ' sprintf('%02d',folder) ', imgFiles: ' int2str(idx)]) |
| 41 | + |
| 42 | + %% reading data |
| 43 | + imgName = imgFiles(idx).name; |
| 44 | + [pathstr,imgname] = fileparts(imgName); |
| 45 | + |
| 46 | + filenum = str2num(imgname(4:7)); |
| 47 | + if(any(test_list == filenum)) |
| 48 | + file_writeID = fopen(imgSetTest,'a'); |
| 49 | + fprintf(file_writeID, '%s\n', [imgDataDir(1:end-3) 'Cropped320_rgd/' imgname '_preprocessed_1.png' ] ); |
| 50 | + fclose(file_writeID); |
| 51 | + continue; |
| 52 | + end |
| 53 | + |
| 54 | + txtName = txtFiles(idx).name; |
| 55 | + [pathstr,txtname] = fileparts(txtName); |
| 56 | + |
| 57 | + img = imread([imgDataDir '/' imgname '.png']); |
| 58 | + fileID = fopen([txtDataDir '/' txtname '.txt'],'r'); |
| 59 | + sizeA = [2 100]; |
| 60 | + bbsIn_all = fscanf(fileID, '%f %f', sizeA); |
| 61 | + fclose(fileID); |
| 62 | + |
| 63 | + %% data pre-processing |
| 64 | + [imagesOut bbsOut] = dataPreprocessing_fasterrcnn(img, bbsIn_all, 227, 5, 5); |
| 65 | + |
| 66 | + % for each augmented image |
| 67 | + for i = 1:1:size(imagesOut,2) |
| 68 | + |
| 69 | + % for each bbs |
| 70 | + file_writeID = fopen([annotationDataOutDir '/' imgname '_preprocessed_' int2str(i) '.txt'],'w'); |
| 71 | + printCount = 0; |
| 72 | + for ibbs = 1:1:size(bbsOut{i},2) |
| 73 | + A = bbsOut{i}{ibbs}; |
| 74 | + xy_ctr = sum(A,2)/4; x_ctr = xy_ctr(1); y_ctr = xy_ctr(2); |
| 75 | + width = sqrt(sum((A(:,1) - A(:,2)).^2)); height = sqrt(sum((A(:,2) - A(:,3)).^2)); |
| 76 | + if(A(1,1) > A(1,2)) |
| 77 | + theta = atan((A(2,2)-A(2,1))/(A(1,1)-A(1,2))); |
| 78 | + else |
| 79 | + theta = atan((A(2,1)-A(2,2))/(A(1,2)-A(1,1))); % note y is facing down |
| 80 | + end |
| 81 | + |
| 82 | + % process to fasterrcnn |
| 83 | + x_min = x_ctr - width/2; x_max = x_ctr + width/2; |
| 84 | + y_min = y_ctr - height/2; y_max = y_ctr + height/2; |
| 85 | + %if(x_min < 0 || y_min < 0 || x_max > 227 || y_max > 227) display('yoooooooo'); end |
| 86 | + if((x_min < 0 && x_max < 0) || (y_min > 227 && y_max > 227) || (x_min > 227 && x_max > 227) || (y_min < 0 && y_max < 0)) display('xxxxxxxxx'); break; end |
| 87 | + cls = round((theta/pi*180+90)/10) + 1; |
| 88 | + |
| 89 | + % write as lefttop rightdown, Xmin Ymin Xmax Ymax, ex: 261 109 511 705 (x水平 y垂直) |
| 90 | + fprintf(file_writeID, '%d %f %f %f %f\n', cls, x_min, y_min, x_max, y_max ); |
| 91 | + printCount = printCount+1; |
| 92 | + end |
| 93 | + if(printCount == 0) fprintf(logfileID, '%s\n', [imgname '_preprocessed_' int2str(i) ]);end |
| 94 | + |
| 95 | + fclose(file_writeID); |
| 96 | + imwrite(imagesOut{i}, [imgDataOutDir '/' imgname '_preprocessed_' int2str(i) '.png']); |
| 97 | + |
| 98 | + % write filename to imageSet |
| 99 | + file_writeID = fopen(imgSetTrain,'a'); |
| 100 | + fprintf(file_writeID, '%s\n', [imgname '_preprocessed_' int2str(i) ] ); |
| 101 | + fclose(file_writeID); |
| 102 | + |
| 103 | + end |
| 104 | + |
| 105 | + |
| 106 | + toc |
| 107 | +end |
| 108 | +fclose(mainfileID); |
| 109 | + |
| 110 | +end |
0 commit comments