def test(): filepaths = [ "/home/mataevs/captures/metadata/dump_05_05_01_50", "/home/mataevs/captures/metadata/dump_05_06_13_10", # "/home/mataevs/captures/metadata/dump_07_05_11_07", "/home/mataevs/captures/metadata/dump_10_06_11_47", "/home/mataevs/captures/metadata/dump_05_05_01_51", "/home/mataevs/captures/metadata/dump_05_06_13_15", "/home/mataevs/captures/metadata/dump_07_05_11_40", "/home/mataevs/captures/metadata/dump_10_06_11_48", "/home/mataevs/captures/metadata/dump_05_05_11_54", "/home/mataevs/captures/metadata/dump_05_06_13_20", "/home/mataevs/captures/metadata/dump_07_05_11_46", "/home/mataevs/captures/metadata/dump_10_06_12_16", "/home/mataevs/captures/metadata/dump_05_06_12_57", "/home/mataevs/captures/metadata/dump_05_06_13_21", "/home/mataevs/captures/metadata/dump_05_06_13_24", # "/home/mataevs/captures/metadata/dump_07_05_12_02", "/home/mataevs/captures/metadata/dump_16_06_14_57", "/home/mataevs/captures/metadata/dump_05_06_13_25", "/home/mataevs/captures/metadata/dump_07_05_12_03", "/home/mataevs/captures/metadata/dump_16_06_15_26", "/home/mataevs/captures/metadata/dump_05_06_13_28", "/home/mataevs/captures/metadata/dump_07_05_12_05" ] testImages = utils.getFullImages(*filepaths) with open("annotations.txt", "a") as annotationFile: while True: imgPath = random.choice(testImages) testImages.remove(imgPath) annotateImage(imgPath, annotationFile)
def test(input_classifier_name, scale=0.6): c = load_classifier(input_classifier_name) # testImages = utils.getFullImages( # "/home/mataevs/ptz/dumpNew1", # "/home/mataevs/ptz/dumpNew2", # "/home/mataevs/ptz/dumpNew3", # "/home/mataevs/ptz/ns1", # "/home/mataevs/ptz/ns2", # "/home/mataevs/ptz/ns3", # "/home/mataevs/ptz/ns4") testImages = utils.getFullImages( "/home/mataevs/ptz/INRIAPerson/Test/pos" ) while True: imgPath = random.choice(testImages) bestWindow = test_img(c, imgPath, scale) img = cv2.imread(imgPath) img = cv2.resize(img, (0, 0), fx=scale, fy=scale) utils.draw_detections(img, [bestWindow]) cv2.imshow("detection", img) key = cv2.waitKey(0) if key == 27: exit(1)
def test(input_classifier_name, scale=0.6): c = load_classifier(input_classifier_name) # testImages = utils.getFullImages( # "/home/mataevs/ptz/dumpNew1", # "/home/mataevs/ptz/dumpNew2", # "/home/mataevs/ptz/dumpNew3", # "/home/mataevs/ptz/ns1", # "/home/mataevs/ptz/ns2", # "/home/mataevs/ptz/ns3", # "/home/mataevs/ptz/ns4") testImages = utils.getFullImages("/home/mataevs/ptz/INRIAPerson/Test/pos") while True: imgPath = random.choice(testImages) bestWindow = test_img(c, imgPath, scale) img = cv2.imread(imgPath) img = cv2.resize(img, (0, 0), fx=scale, fy=scale) utils.draw_detections(img, [bestWindow]) cv2.imshow("detection", img) key = cv2.waitKey(0) if key == 27: exit(1)
def train(classifier_out_name, noInitialFeatures, noWantedFeatures, noEstimators): posImages = utils.getFullImages( "/home/mataevs/ptz/INRIAPerson/train/pos", "/home/mataevs/ptz/positive") posImages = utils.randomize(posImages) negImages = utils.getFullImages( "/home/mataevs/ptz/INRIAPerson/train/neg", "/home/mataevs/ptz/negative") negImages = utils.randomize(negImages) print len(posImages) print len(negImages) c = Classifier() print "Starting training" c.train(posImages, negImages, initialFeatures=noInitialFeatures, wantedFeatures=noWantedFeatures, noEstimators=noEstimators) print "Finished training" c.saveClassifier(classifier_out_name)
def train(classifier_out_name, noInitialFeatures, noWantedFeatures, noEstimators): posImages = utils.getFullImages("/home/mataevs/ptz/INRIAPerson/train/pos", "/home/mataevs/ptz/positive") posImages = utils.randomize(posImages) negImages = utils.getFullImages("/home/mataevs/ptz/INRIAPerson/train/neg", "/home/mataevs/ptz/negative") negImages = utils.randomize(negImages) print len(posImages) print len(negImages) c = Classifier() print "Starting training" c.train(posImages, negImages, initialFeatures=noInitialFeatures, wantedFeatures=noWantedFeatures, noEstimators=noEstimators) print "Finished training" c.saveClassifier(classifier_out_name)
from classifier import * filepaths = [ "/home/mataevs/captures/simple/set1", "/home/mataevs/captures/simple/set2", "/home/mataevs/captures/simple/set3", "/home/mataevs/captures/simple/set4", "/home/mataevs/captures/simple/ns1", "/home/mataevs/captures/simple/ns2", "/home/mataevs/captures/simple/ns3", "/home/mataevs/captures/simple/ns4", "/home/mataevs/captures/simple/dumpNew1", "/home/mataevs/captures/simple/dumpNew2", "/home/mataevs/captures/simple/dumpNew3", ] images = utils.getFullImages(*filepaths) random.shuffle(images) c = loadCascadeClassifier("cascade_classifier_100_500_2k.dump") resultsFile = open("hog_corpus_neg.txt", "w") negatives = 0 imgIndex = 0 while negatives < 10000: imgPath = images[imgIndex] for scale in [0.3, 0.4, 0.5]: windows = c.getWindowsAndDescriptors(imgPath, scale)
def test_multiscale(hog_classifier_file, icf_classifier_file, hog_result_dir, icf_result_dir, no_samples): hog_classifier = tester_hog.load_classifier(hog_classifier_file) icf_classifier = tester_icf.load_classifier(icf_classifier_file) filepaths = [ "/home/mataevs/captures/metadata/dump_05_05_01_50", "/home/mataevs/captures/metadata/dump_05_06_13_10", "/home/mataevs/captures/metadata/dump_10_06_11_47", "/home/mataevs/captures/metadata/dump_05_05_01_51", "/home/mataevs/captures/metadata/dump_05_06_13_15", "/home/mataevs/captures/metadata/dump_07_05_11_40", "/home/mataevs/captures/metadata/dump_10_06_11_48", "/home/mataevs/captures/metadata/dump_05_05_11_54", "/home/mataevs/captures/metadata/dump_05_06_13_20", "/home/mataevs/captures/metadata/dump_07_05_11_46", "/home/mataevs/captures/metadata/dump_10_06_12_16", "/home/mataevs/captures/metadata/dump_05_06_12_57", "/home/mataevs/captures/metadata/dump_05_06_13_21", "/home/mataevs/captures/metadata/dump_05_06_13_24", "/home/mataevs/captures/metadata/dump_16_06_14_57", "/home/mataevs/captures/metadata/dump_05_06_13_25", "/home/mataevs/captures/metadata/dump_07_05_12_03", "/home/mataevs/captures/metadata/dump_16_06_15_26", "/home/mataevs/captures/metadata/dump_05_06_13_28", "/home/mataevs/captures/metadata/dump_07_05_12_05" ] if not os.path.exists(hog_result_dir): os.makedirs(hog_result_dir) if not os.path.exists(icf_result_dir): os.makedirs(icf_result_dir) testImages = utils.getFullImages(*filepaths) metadata = utils.parseMetadata(*filepaths) scales = [[0.45, 0.5, 0.55], [0.4, 0.45, 0.5], [0.3, 0.35], [0.3]] scaleSteps = [35, 45, 65, 90] for sample in range(0, no_samples): print "### Sample " + str(sample) + " ###" imgPath = random.choice(testImages) img = cv2.imread(imgPath) tilt = int(metadata[imgPath]['tilt']) if tilt > 90: tilt = 90 - (tilt - 90) imgScales = [] for i in range(0, len(scaleSteps)): if tilt < scaleSteps[i]: imgScales = scales[i] break print imgScales prev_img_path = utils.get_prev_img(imgPath) prev_img = cv2.imread(prev_img_path) flow_rgb, boundingRect = optical_flow.optical_flow(img, prev_img) height, width, _ = img.shape bestWindowsHog = tester_hog.test_img(hog_classifier, imgPath, imgScales, allPositive=True, flow_rgb=flow_rgb, subwindow=boundingRect) if bestWindowsHog != None and bestWindowsHog != []: scale = bestWindowsHog[0][4] img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale) if boundingRect != None: x, y, w, h = utils.getDetectionWindow(boundingRect, img_hog.shape[1], img_hog.shape[0], scale) cv2.rectangle(img_hog, (x, y), (x + w, y + h), (0, 0, 255), thickness=2, lineType=8) utils.draw_detections(img_hog, bestWindowsHog) else: scale = 0.5 img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale) if boundingRect != None: x, y, w, h = utils.getDetectionWindow(boundingRect, img_hog.shape[1], img_hog.shape[0], scale) cv2.rectangle(img_hog, (x, y), (x + w, y + h), (0, 0, 255), thickness=2, lineType=8) cv2.imwrite(hog_result_dir + "/sample_2_" + str(sample) + ".jpg", img_hog) bestWindowsIcf = tester_icf.test_img(icf_classifier, imgPath, imgScales, allPositive=True, subwindow=boundingRect) if bestWindowsIcf != None and bestWindowsIcf != []: scale = bestWindowsIcf[0][4] img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale) if boundingRect != None: x, y, w, h = utils.getDetectionWindow(boundingRect, img_icf.shape[1], img_icf.shape[0], scale) cv2.rectangle(img_icf, (x, y), (x + w, y + h), (0, 0, 255), thickness=2, lineType=8) utils.draw_detections(img_icf, bestWindowsIcf) else: scale = 0.5 img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale) if boundingRect != None: x, y, w, h = utils.getDetectionWindow(boundingRect, img_icf.shape[1], img_icf.shape[0], scale) cv2.rectangle(img_icf, (x, y), (x + w, y + h), (0, 0, 255), thickness=2, lineType=8) cv2.imwrite(icf_result_dir + "/sample_2_" + str(sample) + ".jpg", img_icf)
def test_multiscale( hog_classifier_file, icf_classifier_file, hog_result_dir, icf_result_dir, no_samples): hog_classifier = tester_hog.load_classifier(hog_classifier_file) icf_classifier = tester_icf.load_classifier(icf_classifier_file) filepaths = [ "/home/mataevs/captures/metadata/dump_05_05_01_50", "/home/mataevs/captures/metadata/dump_05_06_13_10", "/home/mataevs/captures/metadata/dump_10_06_11_47", "/home/mataevs/captures/metadata/dump_05_05_01_51", "/home/mataevs/captures/metadata/dump_05_06_13_15", "/home/mataevs/captures/metadata/dump_07_05_11_40", "/home/mataevs/captures/metadata/dump_10_06_11_48", "/home/mataevs/captures/metadata/dump_05_05_11_54", "/home/mataevs/captures/metadata/dump_05_06_13_20", "/home/mataevs/captures/metadata/dump_07_05_11_46", "/home/mataevs/captures/metadata/dump_10_06_12_16", "/home/mataevs/captures/metadata/dump_05_06_12_57", "/home/mataevs/captures/metadata/dump_05_06_13_21", "/home/mataevs/captures/metadata/dump_05_06_13_24", "/home/mataevs/captures/metadata/dump_16_06_14_57", "/home/mataevs/captures/metadata/dump_05_06_13_25", "/home/mataevs/captures/metadata/dump_07_05_12_03", "/home/mataevs/captures/metadata/dump_16_06_15_26", "/home/mataevs/captures/metadata/dump_05_06_13_28", "/home/mataevs/captures/metadata/dump_07_05_12_05" ] if not os.path.exists(hog_result_dir): os.makedirs(hog_result_dir) if not os.path.exists(icf_result_dir): os.makedirs(icf_result_dir) testImages = utils.getFullImages(*filepaths) metadata = utils.parseMetadata(*filepaths) scales = [ [0.45, 0.5, 0.55], [0.4, 0.45, 0.5], [0.3, 0.35], [0.3] ] scaleSteps = [35, 45, 65, 90] for sample in range(0, no_samples): print "### Sample " + str(sample) + " ###" imgPath = random.choice(testImages) img = cv2.imread(imgPath) tilt = int(metadata[imgPath]['tilt']) if tilt > 90: tilt = 90 - (tilt - 90) imgScales = [] for i in range(0, len(scaleSteps)): if tilt < scaleSteps[i]: imgScales = scales[i] break print imgScales prev_img_path = utils.get_prev_img(imgPath) prev_img = cv2.imread(prev_img_path) flow_rgb, boundingRect = optical_flow.optical_flow(img, prev_img) height, width, _ = img.shape bestWindowsHog = tester_hog.test_img(hog_classifier, imgPath, imgScales, allPositive=True, flow_rgb=flow_rgb, subwindow=boundingRect) if bestWindowsHog != None and bestWindowsHog != []: scale = bestWindowsHog[0][4] img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale) if boundingRect != None: x, y, w, h = utils.getDetectionWindow(boundingRect, img_hog.shape[1], img_hog.shape[0], scale) cv2.rectangle(img_hog, (x, y), (x+w, y+h), (0, 0, 255), thickness=2, lineType=8) utils.draw_detections(img_hog, bestWindowsHog) else: scale = 0.5 img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale) if boundingRect != None: x, y, w, h = utils.getDetectionWindow(boundingRect, img_hog.shape[1], img_hog.shape[0], scale) cv2.rectangle(img_hog, (x, y), (x+w, y+h), (0, 0, 255), thickness=2, lineType=8) cv2.imwrite(hog_result_dir + "/sample_2_" + str(sample) + ".jpg", img_hog) bestWindowsIcf = tester_icf.test_img(icf_classifier, imgPath, imgScales, allPositive=True, subwindow=boundingRect) if bestWindowsIcf != None and bestWindowsIcf != []: scale = bestWindowsIcf[0][4] img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale) if boundingRect != None: x, y, w, h = utils.getDetectionWindow(boundingRect, img_icf.shape[1], img_icf.shape[0], scale) cv2.rectangle(img_icf, (x, y), (x+w, y+h), (0, 0, 255), thickness=2, lineType=8) utils.draw_detections(img_icf, bestWindowsIcf) else: scale = 0.5 img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale) if boundingRect != None: x, y, w, h = utils.getDetectionWindow(boundingRect, img_icf.shape[1], img_icf.shape[0], scale) cv2.rectangle(img_icf, (x, y), (x+w, y+h), (0, 0, 255), thickness=2, lineType=8) cv2.imwrite(icf_result_dir + "/sample_2_" + str(sample) + ".jpg", img_icf)
def test_multiscale(hog_classifier_file, icf_classifier_file, imgpaths, kinect_detections, hog_result_dir, icf_result_dir): hog_classifier = tester_hog.load_classifier(hog_classifier_file) icf_classifier = tester_icf.load_classifier(icf_classifier_file) if not os.path.exists(hog_result_dir): os.makedirs(hog_result_dir) if not os.path.exists(icf_result_dir): os.makedirs(icf_result_dir) testImages = utils.getFullImages(*imgpaths) metadata = utils.parseMetadata(*imgpaths) kinect_ts = read_kinect_metadata(kinect_detections) print metadata print kinect_ts scales = [[0.45, 0.5, 0.55], [0.4, 0.45, 0.5], [0.3, 0.35], [0.3]] scaleSteps = [35, 45, 65, 90] for kinectts in kinect_ts: sel = min(metadata.items(), key=lambda mt: abs(kinectts - mt[1]['time'])) print str(kinectts) + " " + str(sel[1]['time']) imgPath = sel[0] imgTs = sel[1]['time'] print "### Sample " + str(imgPath) + " ###" img = cv2.imread(imgPath) tilt = int(metadata[imgPath]['tilt']) print tilt imgScales = [] for i in range(0, len(scaleSteps)): if tilt < scaleSteps[i]: imgScales = scales[i] break print imgScales bestWindowHog = tester_hog.test_img(hog_classifier, imgPath, imgScales) if bestWindowHog != None: scale = bestWindowHog[4] print "best scale hog = " + str(scale) img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale) utils.draw_detections(img_hog, [bestWindowHog[0:4]]) else: scale = 0.5 img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale) cv2.imwrite(hog_result_dir + "/" + str(imgTs) + ".jpg", img_hog) bestWindowIcf = tester_icf.test_img(icf_classifier, imgPath, imgScales) if bestWindowIcf != None: scale = bestWindowIcf[4] print "best scale icf = " + str(scale) img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale) utils.draw_detections(img_icf, [bestWindowIcf[0:4]]) cv2.imshow("icf", img_icf) else: scale = 0.5 img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale) cv2.imshow("icf", img_icf) cv2.imwrite(icf_result_dir + "/" + str(imgTs) + ".jpg", img_icf)
def test_multiscale( hog_classifier_file, icf_classifier_file, imgpaths, kinect_detections, hog_result_dir, icf_result_dir): hog_classifier = tester_hog.load_classifier(hog_classifier_file) icf_classifier = tester_icf.load_classifier(icf_classifier_file) if not os.path.exists(hog_result_dir): os.makedirs(hog_result_dir) if not os.path.exists(icf_result_dir): os.makedirs(icf_result_dir) testImages = utils.getFullImages(*imgpaths) metadata = utils.parseMetadata(*imgpaths) kinect_ts = read_kinect_metadata(kinect_detections) print metadata print kinect_ts scales = [ [0.45, 0.5, 0.55], [0.4, 0.45, 0.5], [0.3, 0.35], [0.3] ] scaleSteps = [35, 45, 65, 90] for kinectts in kinect_ts: sel = min(metadata.items(), key=lambda mt: abs(kinectts - mt[1]['time'])) print str(kinectts) + " " + str(sel[1]['time']) imgPath = sel[0] imgTs = sel[1]['time'] print "### Sample " + str(imgPath) + " ###" img = cv2.imread(imgPath) tilt = int(metadata[imgPath]['tilt']) print tilt imgScales = [] for i in range(0, len(scaleSteps)): if tilt < scaleSteps[i]: imgScales = scales[i] break print imgScales bestWindowHog = tester_hog.test_img(hog_classifier, imgPath, imgScales) if bestWindowHog != None: scale = bestWindowHog[4] print "best scale hog = " + str(scale) img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale) utils.draw_detections(img_hog, [bestWindowHog[0:4]]) else: scale = 0.5 img_hog = cv2.resize(img, (0, 0), fx=scale, fy=scale) cv2.imwrite(hog_result_dir + "/" + str(imgTs) + ".jpg", img_hog) bestWindowIcf = tester_icf.test_img(icf_classifier, imgPath, imgScales) if bestWindowIcf != None: scale = bestWindowIcf[4] print "best scale icf = " + str(scale) img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale) utils.draw_detections(img_icf, [bestWindowIcf[0:4]]) cv2.imshow("icf", img_icf) else: scale = 0.5 img_icf = cv2.resize(img, (0, 0), fx=scale, fy=scale) cv2.imshow("icf", img_icf) cv2.imwrite(icf_result_dir + "/" + str(imgTs) + ".jpg", img_icf)