Commit 06043825 authored by mt181068's avatar mt181068
Browse files

Initial commit

parents
import cv2
import numpy as np
import itertools as IT
from operator import itemgetter
import keyboard
import time
from osc4py3.as_eventloop import *
from osc4py3 import oscbuildparse
notes = []
noteLength = []
noteVelocity = []
def getBlobAverage(keypoints):
avg = 0
nummer = 0
for i in keypoints:
if avg == 0:
avg = avg + i.size
nummer = nummer + 1
else:
if not(i.size > 2 * avg or i.size < 0.5*avg):
avg = avg + i.size
nummer = nummer +1
if nummer > 0:
avg = avg / nummer
print("AVG: " + str(avg))
return avg
def isAtPosition(coordinate1,coordinate2, avgSize):
if abs(coordinate1 - coordinate2) < avgSize:
return True
return False
def findLine(coordinate, existingLines,avgSize):
count = 0
found = False
while count < len(existingLines) and not found:
if isAtPosition(coordinate, existingLines[count],avgSize):
found = True
return existingLines[count]
count += 1
if(not found):
existingLines.append(coordinate)
return coordinate
def noteBoolToString(note):
noteSt = ""
for i in range(len(note)):
for j in range(len(note[i])):
if note[i][j]:
noteSt += str((j+1)+(i*3))
return noteSt
def translate(letters):
dur = 500
note = ''
for f in letters:
if f != '':
if("3" in f and "6" in f):
dur = 500
elif("3" in f):
dur = 250
elif("6" in f):
dur = 125
else:
dur = 62
note = f
noteLength.append(dur)
noteVelocity.append("80")
note = note.replace("3","")
note = note.replace("6","")
if(note == "145"):
note = "60" #C
elif(note == "15"):
note = "62" #D
elif(note == "124"):
note = "64" #E
elif(note == "1245"):
note = "65" #F
elif(note == "125"):
note = "67" #G
elif(note == "24"):
note = "69" #A
elif(note == "245"):
note = "71" #H
else:
note = "60"
print(f + " " + note + " " + str(dur))
notes.append(note)
# read image
#img = cv2.imread("braille3.jpeg")
#img = cv2.imread("braille.jpg")
img = cv2.imread("braille4.jpg")
debug = True
cap = cv2.VideoCapture('http://10.0.0.4:4747/video')
ret = False
while not keyboard.is_pressed('w'):
cv2.waitKey(30)
if not debug:
try:
ret, frame = cap.read()
if ret:
im = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
else:
im = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
except:
im = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
else:
im = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
im = cv2.medianBlur(im,5)
thresh = cv2.adaptiveThreshold(im,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv2.THRESH_BINARY,11,2)
#im = thresh
cv2.imshow("1", thresh)
# do adaptive threshold on gray image
#thresh = cv2.adaptiveThreshold(im, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 101, 3)
#cv2.imshow("2", thresh)
# apply morphology open then close
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2,2))
blob = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,9))
bla = cv2.morphologyEx(blob, cv2.MORPH_CLOSE, kernel)
cv2.imshow("2", thresh)
height, width = im.shape
# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()
# # Change thresholds
# # Filter by Area.
#params.filterByArea = True
#params.minArea = 10
# # Filter by Circularity
#params.filterByCircularity = True
#params.minCircularity = 0.5
detector = cv2.SimpleBlobDetector_create(params)
# Detect blobs.
keypoints = detector.detect(im)
im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
blobPositions = [[None for i in range(2)] for j in range(len(keypoints))]
avgBlobsize = getBlobAverage(keypoints)
xLines = []
yLines = []
for i in range(len(keypoints)):
x = keypoints[i].pt[0]
y = keypoints[i].pt[1]
if(len(xLines) == 0):
xLines.append(x)
blobPositions[i][0] = x
else:
coord = findLine(x,xLines,avgBlobsize)
blobPositions[i][0] = coord
if(len(yLines)==0):
yLines.append(y)
blobPositions[i][1] = y
else:
coord = findLine(y,yLines,avgBlobsize)
blobPositions[i][1] = coord
for pos in blobPositions:
im_with_keypoints = cv2.line(im_with_keypoints, (int(pos[0]), 0), (int(pos[0]), int(height)), (0, 255, 0), thickness=2)
im_with_keypoints = cv2.line(im_with_keypoints, (0, int(pos[1])), (int(width), int(pos[1])), (0, 255, 0), thickness=2)
blobPositions = sorted(blobPositions , key=lambda k: [k[0], k[1]])
xLines.sort()
yLines.sort()
cv2.imshow("Keypoints", im_with_keypoints)
blobRaster = [[False for i in range(len(yLines))] for j in range(len(xLines))]
xCount = 0
while xCount < len(xLines):
yCount = 0
while yCount < len(yLines):
posCount = 0
found = False
while posCount < len(blobPositions) and not found:
if blobPositions[posCount][0] == xLines[xCount]:
if blobPositions[posCount][1] == yLines[yCount]:
blobRaster[xCount][yCount] = True
found = True
posCount += 1
yCount += 1
xCount += 1
letters = []
for i in range(int(len(blobRaster)/2)):
blob = blobRaster[i*2:i*2+2]
letters.append(noteBoolToString(blob))
translate(letters)
line_thickness = 2
# Show blobs
osc_startup()
# Make client channels to send packets.
osc_udp_client("127.0.0.1", 2781, "aclientname")
# Build a message with autodetection of data types, and send it.
for i in range(0,len(notes)):
msg = oscbuildparse.OSCMessage("/BrailleLength", None, [noteLength[i]])
osc_send(msg, "aclientname")
osc_process()
msg = oscbuildparse.OSCMessage("/BrailleVelocity", None, [noteVelocity[i]])
osc_send(msg, "aclientname")
osc_process()
msg = oscbuildparse.OSCMessage("/BrailleNote", None, [notes[i]])
osc_send(msg, "aclientname")
osc_process()
msg = oscbuildparse.OSCMessage("/BrailleNote", None, ["7"])
osc_send(msg, "aclientname")
osc_process()
osc_terminate()
cv2.imshow("Keypoints", im_with_keypoints)
cv2.waitKey(0)
File added
{
"patcher" : {
"fileversion" : 1,
"appversion" : {
"major" : 8,
"minor" : 1,
"revision" : 3,
"architecture" : "x64",
"modernui" : 1
}
,
"classnamespace" : "box",
"rect" : [ 34.0, 77.0, 1472.0, 969.0 ],
"bglocked" : 0,
"openinpresentation" : 0,
"default_fontsize" : 12.0,
"default_fontface" : 0,
"default_fontname" : "Arial",
"gridonopen" : 1,
"gridsize" : [ 15.0, 15.0 ],
"gridsnaponopen" : 1,
"objectsnaponopen" : 1,
"statusbarvisible" : 2,
"toolbarvisible" : 1,
"lefttoolbarpinned" : 0,
"toptoolbarpinned" : 0,
"righttoolbarpinned" : 0,
"bottomtoolbarpinned" : 0,
"toolbars_unpinned_last_save" : 0,
"tallnewobj" : 0,
"boxanimatetime" : 200,
"enablehscroll" : 1,
"enablevscroll" : 1,
"devicewidth" : 0.0,
"description" : "",
"digest" : "",
"tags" : "",
"style" : "",
"subpatcher_template" : "",
"boxes" : [ {
"box" : {
"fontname" : "Arial",
"fontsize" : 12.0,
"format" : 6,
"id" : "obj-14",
"maxclass" : "flonum",
"numinlets" : 1,
"numoutlets" : 2,
"outlettype" : [ "", "bang" ],
"parameter_enable" : 0,
"patching_rect" : [ 869.134300708770752, 569.059689402580261, 80.0, 22.0 ]
}
}
, {
"box" : {
"fontname" : "Arial",
"fontsize" : 12.0,
"id" : "obj-11",
"maxclass" : "number",
"numinlets" : 1,
"numoutlets" : 2,
"outlettype" : [ "", "bang" ],
"parameter_enable" : 0,
"patching_rect" : [ 970.635186553001404, 151.746263265609741, 50.0, 22.0 ]
}
}
, {
"box" : {
"fontname" : "Arial",
"fontsize" : 12.0,
"id" : "obj-9",
"maxclass" : "message",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 986.10444974899292, 209.08954930305481, 61.0, 22.0 ],
"text" : "tempo $1"
}
}
, {
"box" : {
"fontname" : "Arial",
"fontsize" : 12.0,
"id" : "obj-6",
"maxclass" : "newobj",
"numinlets" : 2,
"numoutlets" : 9,
"outlettype" : [ "int", "int", "float", "float", "float", "", "int", "float", "" ],
"patching_rect" : [ 986.10444974899292, 233.08954930305481, 156.0, 22.0 ],
"text" : "transport @name mytempo"
}
}
, {
"box" : {
"fontname" : "Arial",
"fontsize" : 12.0,
"id" : "obj-79",
"maxclass" : "newobj",
"numinlets" : 1,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 869.134300708770752, 545.059689402580261, 364.0, 22.0 ],
"text" : "translate @transport mytempo @in notevalues @out ms @listen 1"
}
}
, {
"box" : {
"id" : "obj-76",
"maxclass" : "button",
"numinlets" : 1,
"numoutlets" : 1,
"outlettype" : [ "bang" ],
"parameter_enable" : 0,
"patching_rect" : [ 840.298477411270142, 226.119394898414612, 24.0, 24.0 ]
}
}
, {
"box" : {
"id" : "obj-74",
"maxclass" : "newobj",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "bang" ],
"patching_rect" : [ 840.298477411270142, 195.522381067276001, 56.0, 22.0 ],
"text" : "metro 1n"
}
}
, {
"box" : {
"id" : "obj-71",
"maxclass" : "toggle",
"numinlets" : 1,
"numoutlets" : 1,
"outlettype" : [ "int" ],
"parameter_enable" : 0,
"patching_rect" : [ 840.298477411270142, 150.746263265609741, 24.0, 24.0 ]
}
}
, {
"box" : {
"id" : "obj-60",
"maxclass" : "message",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 881.343252062797546, 475.555539965629578, 29.5, 22.0 ],
"text" : "8n"
}
}
, {
"box" : {
"id" : "obj-53",
"maxclass" : "message",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 846.26862645149231, 475.373117327690125, 29.5, 22.0 ],
"text" : "4n"
}
}
, {
"box" : {
"id" : "obj-48",
"maxclass" : "message",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 810.313404679298401, 475.555539965629578, 29.5, 22.0 ],
"text" : "2n"
}
}
, {
"box" : {
"id" : "obj-45",
"maxclass" : "message",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 773.134300708770752, 475.373117327690125, 29.5, 22.0 ],
"text" : "1n"
}
}
, {
"box" : {
"id" : "obj-43",
"maxclass" : "newobj",
"numinlets" : 5,
"numoutlets" : 5,
"outlettype" : [ "bang", "bang", "bang", "bang", "" ],
"patching_rect" : [ 787.313404679298401, 403.731328845024109, 111.0, 22.0 ],
"text" : "sel 500 250 125 62"
}
}
, {
"box" : {
"id" : "obj-41",
"maxclass" : "newobj",
"numinlets" : 1,
"numoutlets" : 2,
"outlettype" : [ "bang", "float" ],
"patching_rect" : [ 605.970127582550049, 371.641777753829956, 29.5, 22.0 ],
"text" : "t b f"
}
}
, {
"box" : {
"id" : "obj-40",
"maxclass" : "newobj",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "bang" ],
"patching_rect" : [ 605.970127582550049, 468.226648569107056, 37.0, 22.0 ],
"text" : "delay"
}
}
, {
"box" : {
"id" : "obj-86",
"maxclass" : "newobj",
"numinlets" : 1,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 250.0, 213.254909455776215, 71.0, 22.0 ],
"text" : "fromsymbol"
}
}
, {
"box" : {
"id" : "obj-82",
"maxclass" : "button",
"numinlets" : 1,
"numoutlets" : 1,
"outlettype" : [ "bang" ],
"parameter_enable" : 0,
"patching_rect" : [ 71.851849496364594, 255.555547177791595, 24.0, 24.0 ]
}
}
, {
"box" : {
"id" : "obj-70",
"maxclass" : "newobj",
"numinlets" : 1,
"numoutlets" : 2,
"outlettype" : [ "bang", "bang" ],
"patching_rect" : [ 183.703697681427002, 357.742932498455048, 32.0, 22.0 ],
"text" : "t b b"
}
}
, {
"box" : {
"id" : "obj-65",
"maxclass" : "newobj",
"numinlets" : 2,
"numoutlets" : 2,
"outlettype" : [ "", "" ],
"patching_rect" : [ 497.037020742893219, 422.928115546703339, 54.0, 22.0 ],
"text" : "zl.queue"
}
}
, {
"box" : {
"id" : "obj-64",
"maxclass" : "newobj",
"numinlets" : 2,
"numoutlets" : 2,
"outlettype" : [ "", "" ],
"patching_rect" : [ 391.111098289489746, 422.928115546703339, 54.0, 22.0 ],
"text" : "zl.queue"
}
}
, {
"box" : {
"id" : "obj-63",
"maxclass" : "newobj",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 645.185164034366608, 611.111091077327728, 68.0, 22.0 ],
"text" : "print length"
}
}
, {
"box" : {
"id" : "obj-62",
"maxclass" : "newobj",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 542.222204446792603, 611.111091077327728, 75.0, 22.0 ],
"text" : "print velocity"
}
}
, {
"box" : {
"id" : "obj-61",
"maxclass" : "newobj",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 455.55554062128067, 611.111091077327728, 59.0, 22.0 ],
"text" : "print note"
}
}
, {
"box" : {
"id" : "obj-52",
"maxclass" : "newobj",
"numinlets" : 1,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 431.666652679443359, 237.254909455776215, 71.0, 22.0 ],
"text" : "fromsymbol"
}
}
, {
"box" : {
"id" : "obj-51",
"maxclass" : "newobj",
"numinlets" : 1,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 330.0, 237.254909455776215, 71.0, 22.0 ],
"text" : "fromsymbol"
}
}
, {
"box" : {
"id" : "obj-49",
"maxclass" : "message",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 431.666652679443359, 517.86493319272995, 29.5, 22.0 ],
"text" : "$1"
}
}
, {
"box" : {
"id" : "obj-33",
"maxclass" : "newobj",
"numinlets" : 3,
"numoutlets" : 2,
"outlettype" : [ "float", "float" ],
"patching_rect" : [ 337.777766704559326, 573.333314538002014, 61.0, 22.0 ],
"text" : "makenote"
}
}
, {
"box" : {
"id" : "obj-29",
"maxclass" : "newobj",
"numinlets" : 2,
"numoutlets" : 2,
"outlettype" : [ "", "" ],
"patching_rect" : [ 236.185187637805939, 475.555539965629578, 46.0, 22.0 ],
"text" : "route 0"
}
}
, {
"box" : {
"id" : "obj-21",
"maxclass" : "newobj",
"numinlets" : 1,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 337.777766704559326, 465.18516993522644, 71.0, 22.0 ],
"text" : "fromsymbol"
}
}
, {
"box" : {
"id" : "obj-59",
"maxclass" : "message",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"patching_rect" : [ 337.777766704559326, 517.86493319272995, 29.5, 22.0 ],
"text" : "$1"
}
}
, {
"box" : {
"id" : "obj-57",