def to_handmodel(hand_crop, direction='up'): handcontour = utils.skindetect(hand_crop) hand = utils.cut(hand_crop, handcontour) handskeleton = utils.skeletonize(hand) fingerlines = utils.linedetect(handskeleton) if direction == 'dn': handmodel = map(lambda l: [l[2], l[3]], fingerlines) else: handmodel = map(lambda l: [l[0], l[1]], fingerlines) if sum([1 for _ in handmodel]) > 4: handmodel = utils.cluster(handmodel, \ value=lambda p: (p[0])**2 + (p[1])**2, \ K=4) combine = lambda p1, p2: [(p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2] handmodel = map(lambda l: reduce(combine, l), handmodel) return handmodel
def to_handmodel(hand_crop, direction='up'): handcontour = utils.skindetect(hand_crop) hand = utils.cut(hand_crop, handcontour) handskeleton = utils.skeletonize(hand) fingerlines = utils.linedetect(handskeleton) if direction == 'dn': handmodel = map(lambda l: [l[2], l[3]], fingerlines) else: handmodel = map(lambda l: [l[0], l[1]], fingerlines) if sum([1 for _ in handmodel]) > 4: handmodel = utils.cluster(handmodel, \ value=lambda p: (p[0])**2 + (p[1])**2, \ K=4) combine=lambda p1, p2: [(p1[0]+p2[0])/2, (p1[1]+p2[1])/2] handmodel = map(lambda l: reduce(combine, l), handmodel) return handmodel
def get_guitar(frame): guitar["available"] = False # Get all lines in current frame that are long enough to be strings lines = utils.linedetect(frame, minLineLength=180) # Return if no lines if len(lines) < 1: return guitar # Convert to line model lines = list(map(lambda l: utils.tolinemodel(l), lines)) # Get average angle of all lines string_angle = reduce(lambda avg, lm: avg + lm["angle"], [0] + lines) / len(lines) # Filter down to all lines within delta angle of the average delta_angle = 2 #degs lines_near_avg = list( filter(lambda lm: abs(lm["angle"] - string_angle) < delta_angle, lines)) # If the lines that fit the average are not the majority, exit if len(lines_near_avg) < len(lines) / 2: return guitar # Search for strings and append/update string locations string_lines = utils.cluster(lines_near_avg, \ value=lambda lm: lm["origin"], K=NUM_STRINGS) string_lines = map(lambda sl: reduce(utils.combine_linemodel, sl), string_lines) if string_lines is None: return guitar guitar["available"] = True # Update string locations if len(guitar["locations"]["strings"]) < 4 or \ len(string_lines) == NUM_STRINGS: # Update all strings guitar["locations"]["strings"] = list( map(lambda lm: lm["line"], string_lines)) else: # TODO: Collectively move strings by average displacement guitar["locations"]["strings"] = list( map(lambda lm: lm["line"], string_lines)) # Bounding box is x, y coordinates of both ends for first and last strings bounding_box = [] bounding_box.append(guitar["locations"]["strings"][0][:2]) bounding_box.append(guitar["locations"]["strings"][0][2:]) bounding_box.append(guitar["locations"]["strings"][-1][:2]) bounding_box.append(guitar["locations"]["strings"][-1][2:]) print("Bounding box: {}".format(bounding_box)) # Search for frets and append/update string locations lines = utils.linedetect(frame, minLineLength=20, maxLineGap=20) # Return if no lines if len(lines) < 1: return guitar # Convert to line model lines = map(lambda l: utils.tolinemodel(l), lines) # Frets are 90 degrees to the strings fret_angle = string_angle + 90 # Filter down to all lines within twice the delta angle of the fret angle lines_near_avg = filter( lambda lm: abs(lm["angle"] - fret_angle) < 2 * delta_angle, lines) # Filter down to lines that lie partially inside space made by strings print("Frets Found:\n{}".format(map(lambda lm: lm["line"], lines_near_avg))) # TODO: Figure this out def box_check(lm): return True lines_near_avg = list(filter(box_check, lines_near_avg)) # If there aren't enough lines to support this operation, return if len(lines_near_avg) < 1: #NUM_FRETS/2: return guitar # Search for strings and append/update string locations fret_lines = utils.cluster(lines_near_avg, \ value=lambda lm: lm["origin"], K=NUM_STRINGS) fret_lines = map(lambda sl: reduce(utils.combine_linemodel, sl), fret_lines) if fret_lines is None: return guitar guitar["locations"]["frets"] = map(lambda lm: lm["line"], fret_lines) guitar["available"] = True return guitar
def get_guitar(frame): guitar["available"] = False # Get all lines in current frame that are long enough to be strings lines = utils.linedetect(frame, minLineLength=180) # Return if no lines if len(lines) < 1: return guitar # Convert to line model lines = list(map(lambda l: utils.tolinemodel(l), lines)) # Get average angle of all lines string_angle = reduce(lambda avg, lm: avg + lm["angle"], [0] + lines)/len(lines) # Filter down to all lines within delta angle of the average delta_angle = 2 #degs lines_near_avg = list(filter(lambda lm: abs(lm["angle"] - string_angle) < delta_angle, lines)) # If the lines that fit the average are not the majority, exit if len(lines_near_avg) < len(lines)/2: return guitar # Search for strings and append/update string locations string_lines = utils.cluster(lines_near_avg, \ value=lambda lm: lm["origin"], K=NUM_STRINGS) string_lines = map(lambda sl: reduce(utils.combine_linemodel, sl), string_lines) if string_lines is None: return guitar guitar["available"] = True # Update string locations if len(guitar["locations"]["strings"]) < 4 or \ len(string_lines) == NUM_STRINGS: # Update all strings guitar["locations"]["strings"] = list(map(lambda lm: lm["line"], string_lines)) else: # TODO: Collectively move strings by average displacement guitar["locations"]["strings"] = list(map(lambda lm: lm["line"], string_lines)) # Bounding box is x, y coordinates of both ends for first and last strings bounding_box = [] bounding_box.append(guitar["locations"]["strings"][0][:2]) bounding_box.append(guitar["locations"]["strings"][0][2:]) bounding_box.append(guitar["locations"]["strings"][-1][:2]) bounding_box.append(guitar["locations"]["strings"][-1][2:]) print("Bounding box: {}".format(bounding_box)) # Search for frets and append/update string locations lines = utils.linedetect(frame, minLineLength=20, maxLineGap=20) # Return if no lines if len(lines) < 1: return guitar # Convert to line model lines = map(lambda l: utils.tolinemodel(l), lines) # Frets are 90 degrees to the strings fret_angle = string_angle + 90 # Filter down to all lines within twice the delta angle of the fret angle lines_near_avg = filter(lambda lm: abs(lm["angle"] - fret_angle) < 2*delta_angle, lines) # Filter down to lines that lie partially inside space made by strings print("Frets Found:\n{}".format(map(lambda lm: lm["line"], lines_near_avg))) # TODO: Figure this out def box_check(lm): return True lines_near_avg = list(filter(box_check, lines_near_avg)) # If there aren't enough lines to support this operation, return if len(lines_near_avg) < 1:#NUM_FRETS/2: return guitar # Search for strings and append/update string locations fret_lines = utils.cluster(lines_near_avg, \ value=lambda lm: lm["origin"], K=NUM_STRINGS) fret_lines = map(lambda sl: reduce(utils.combine_linemodel, sl), fret_lines) if fret_lines is None: return guitar guitar["locations"]["frets"] = map(lambda lm: lm["line"], fret_lines) guitar["available"] = True return guitar