def test(): os.system('rm -rf tmp/pos/*') os.system('rm -rf tmp/neg/*') os.system('rm -rf tmp/part/*') logger.info('Load WIDER') train_data, val_data = load_wider() print("TRAIN DATA: ", len(train_data)) img_path, bboxes = train_data[np.random.choice(len(train_data))] bboxes = np.asarray(bboxes) #pdb.set_trace() img = cv2.imread(img_path, cv2.IMREAD_COLOR) detector = JfdaDetector(cfg.PROPOSAL_NETS['r']) #print("HERE??>> 529") negatives, positives, part = proposal(img, bboxes, detector) logger.info('%d gt_bboxes', len(bboxes)) logger.info('%d negatives, %d positives, %d part', len(negatives), len(positives), len(part)) for i, (data[1], bbox_target) in enumerate(positives): cv2.imwrite(longPath + 'tmp/pos/%03d.png' % i, data) for i, (data) in enumerate(negatives): cv2.imwrite(longPath + 'tmp/neg/%03d.png' % i, data[0]) #pdb.set_trace() for i, (data[1], bbox_target) in enumerate(part): cv2.imwrite('tmp/part/%03d.png' % i, data[0]) cv2.imwrite('tmp/test.png', img)
def get_detector(): nets = cfg.PROPOSAL_NETS[cfg.NET_TYPE] if nets is None or not cfg.USE_DETECT: detector = None else: if cfg.GPU_ID >= 0: caffe.set_mode_gpu() caffe.set_device(cfg.GPU_ID) else: caffe.set_mode_cpu() detector = JfdaDetector(nets) return detector
def main(args): net = [ 'proto/p.prototxt', 'model/p.caffemodel', 'proto/r.prototxt', 'model/r.caffemodel', 'proto/o.prototxt', 'model/o.caffemodel', 'proto/l.prototxt', 'model/l.caffemodel', ] if args.minicaffe: from jfda.mdetector import MiniCaffeDetector detector = MiniCaffeDetector(net) else: detector = JfdaDetector(net) if args.pnet_single: detector.set_pnet_single_forward(True) param = { 'ths': [0.6, 0.7, 0.8], 'factor': 0.709, 'min_size': 24, } timer = Timer() def gen(img, bboxes, outname): for i in range(len(bboxes)): x1, y1, x2, y2, score = bboxes[i, :5] x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 2) cv2.putText(img, '%.03f' % score, (x1, y1), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0)) # landmark landmark = bboxes[i, 9:].reshape((5, 2)) for j in range(5): x, y = landmark[j] x, y = int(x), int(y) cv2.circle(img, (x, y), 2, (0, 255, 0), -1) cv2.imwrite(outname, img) with open('demo.txt', 'r') as fin: for line in fin.readlines(): fp = line.strip() dn = os.path.dirname(fp) fn = os.path.basename(fp).split('.')[0] img = cv2.imread(fp, cv2.IMREAD_COLOR) timer.tic() bb, ts = detector.detect(img, debug=True, **param) timer.toc() print 'detect %s costs %.04lfs' % (fp, timer.elapsed()) print 'image size = (%d x %d), s1: %.04lfs, s2: %.04lfs, s3: %.04lfs, s4: %.04lf' % ( img.shape[0], img.shape[1], ts[0], ts[1], ts[2], ts[3]) print 'bboxes, s1: %d, s2: %d, s3: %d, s4: %d' % (len( bb[0]), len(bb[1]), len(bb[2]), len(bb[3])) out1 = '%s/%s_stage1.jpg' % (dn, fn) out2 = '%s/%s_stage2.jpg' % (dn, fn) out3 = '%s/%s_stage3.jpg' % (dn, fn) out4 = '%s/%s_stage4.jpg' % (dn, fn) gen(img.copy(), bb[0], out1) gen(img.copy(), bb[1], out2) gen(img.copy(), bb[2], out3) gen(img.copy(), bb[3], out4)
def main(args): if args.net == 'p': net = ['proto/p.prototxt', 'model/p.caffemodel'] elif args.net == 'r': net = [ 'proto/p.prototxt', 'model/p.caffemodel', 'proto/r.prototxt', 'model/r.caffemodel' ] else: net = [ 'proto/p.prototxt', 'model/p.caffemodel', 'proto/r.prototxt', 'model/r.caffemodel', 'proto/o.prototxt', 'model/o.caffemodel', ] detector = JfdaDetector(net) if args.pnet_single: detector.set_pnet_single_forward(True) logger = get_logger() counter = 0 timer = Timer() param = { 'ths': [0.5, 0.5, 0.4], 'factor': 0.709, 'min_size': 24, } total_time = 0. total_width = 0 total_height = 0 for i in range(10): logger.info('Process FOLD-%02d', i) txt_in = cfg.FDDB_DIR + '/FDDB-folds/FDDB-fold-%02d.txt' % (i + 1) txt_out = cfg.FDDB_DIR + '/result/fold-%02d-out.txt' % (i + 1) answer_in = cfg.FDDB_DIR + '/FDDB-folds/FDDB-fold-%02d-ellipseList.txt' % ( i + 1) fin = open(txt_in, 'r') fout = open(txt_out, 'w') ain = open(answer_in, 'r') for line in fin.readlines(): line = line.strip() in_file = cfg.FDDB_DIR + '/images/' + line + '.jpg' out_file = cfg.FDDB_DIR + '/result/images/' + line.replace( '/', '-') + '.jpg' counter += 1 img = cv2.imread(in_file, cv2.IMREAD_COLOR) timer.tic() bboxes = detector.detect(img, **param) timer.toc() total_time += timer.elapsed() h, w = img.shape[:-1] total_height += h total_width += w logger.info( 'Detect %04d th image costs %.04lfs, average %.04lfs, %d x %d', counter, timer.elapsed(), total_time / counter, total_height / counter, total_width / counter) fout.write('%s\n' % line) fout.write('%d\n' % len(bboxes)) for bbox in bboxes: x1, y1, x2, y2, score = bbox[:5] fout.write('%lf %lf %lf %lf %lf\n' % (x1, y1, x2 - x1, y2 - y1, score)) # draw ground truth ain.readline() # remove fname n = int(ain.readline().strip()) for i in range(n): line = ain.readline().strip() components = [float(_) for _ in line.split(' ')[:5]] major_axis_radius, minor_axis_radius, angle, center_x, center_y = components angle = angle / 3.1415926 * 180. center_x, center_y = int(center_x), int(center_y) major_axis_radius, minor_axis_radius = int( major_axis_radius), int(minor_axis_radius) cv2.ellipse(img, (center_x, center_y), (major_axis_radius, minor_axis_radius), angle, 0, 360, (255, 0, 0), 2) # draw and save for bbox in bboxes: x1, y1, x2, y2, score = bbox[:5] x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 2) cv2.putText(img, '%.03f' % score, (x1, y1), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0)) landmark = bbox[9:].reshape((5, 2)) for x, y in landmark: x, y = int(x), int(y) cv2.circle(img, (x, y), 2, (0, 255, 0), -1) cv2.imwrite(out_file, img) fin.close() fout.close() ain.close()