def process(im, haarc=None, silent=False): if haarc is None: haarc = haar.haarInit(im + '/../haar/cascade.xml') img_ref = im img, rect = findROI(img_ref, haarc) imb = extractBinary(img) imb_contours = imb.copy() vect = None img_tr = np.copy(img_ref) if not silent: debugThresh(img) if rect is None: img_ref = cv2.cvtColor(img_ref, cv2.COLOR_GRAY2BGR) return img_ref, img_ref, None contours, _ = cv2.findContours(imb_contours, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) if contours: contour = bestContourAsInt(contours) hull = cv2.convexHull(contour, returnPoints=False).astype('int') defects = cv2.convexityDefects(contour, hull) hull_points = [ tuple(p[0]) for p in cv2.convexHull(contour, returnPoints=True) ] contour_points = [tuple(p[0]) for p in contour] hull_refined, defects_points = refineHullDefects( hull_points, defects, contour, 2500) features = packFeatures(contour, hull_points, defects_points, hull_refined, rect) img = drawResult(img, features) img_ref = cv2.cvtColor(img_ref, cv2.COLOR_GRAY2BGR) (x, y, w, h) = rect img_ref[y:y + h, x:x + w] = img cv2.rectangle(img_ref, (x, y), (x + w, y + h), (255, 0, 0)) img_tr[y:y + h, x:x + w] = imb else: img_ref = cv2.cvtColor(img_ref, cv2.COLOR_GRAY2BGR) img_tr = imb densityVect = zoning(imb) img_tr = cv2.cvtColor(img_tr, cv2.COLOR_GRAY2BGR) return img_ref, img_tr, densityVect
def process(im, haarc=None,silent=False): if haarc is None: haarc = haar.haarInit(im + '/../haar/cascade.xml') img_ref = im img, rect = findROI(img_ref, haarc) imb = extractBinary(img) imb_contours = imb.copy() vect = None img_tr = np.copy(img_ref) if not silent: debugThresh(img) if rect is None: img_ref = cv2.cvtColor(img_ref,cv2.COLOR_GRAY2BGR) return img_ref, img_ref, None contours, _ = cv2.findContours(imb_contours, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) if contours: contour = bestContourAsInt(contours) hull = cv2.convexHull(contour, returnPoints=False).astype('int') defects = cv2.convexityDefects(contour, hull) hull_points = [tuple(p[0]) for p in cv2.convexHull(contour, returnPoints=True)] contour_points = [tuple(p[0]) for p in contour] hull_refined, defects_points = refineHullDefects(hull_points, defects, contour, 2500) features = packFeatures(contour, hull_points, defects_points, hull_refined, rect) img = drawResult(img, features) img_ref = cv2.cvtColor(img_ref,cv2.COLOR_GRAY2BGR) (x,y,w,h) = rect img_ref[y:y+h, x:x+w] = img cv2.rectangle(img_ref, (x,y), (x+w,y+h), (255,0,0)) img_tr[y:y+h, x:x+w] = imb else: img_ref = cv2.cvtColor(img_ref,cv2.COLOR_GRAY2BGR) img_tr = imb densityVect = zoning(imb) img_tr = cv2.cvtColor(img_tr,cv2.COLOR_GRAY2BGR) return img_ref, img_tr,densityVect
fichier.sort(key=lambda x: os.path.basename(x)) length = len(fichier) return fichier if __name__ == '__main__' : parser = OptionParser() parser.add_option("-d", "--directory", dest="dirname", help="Directory to parse", metavar="DIR") (options, args) = parser.parse_args() filesNames = listDirectory(options.dirname) class_dict = eval(open("../bayes/dictionnary.txt").read()) haarc = haar.haarInit(os.path.dirname(os.path.realpath(__file__)) + '/../haar/cascade.xml') trainData = [] responses = [] total = len(filesNames) i = 1 for fileName in filesNames: print "{0} / {1}".format(i,total) i = i+1 _,_,densityVect = detect.process(detect.loadSample(fileName),haarc,True) if densityVect is None: continue trainData.append(densityVect)