Commit 7b201427 by tilman

### restructure repo

parents

531 KB

1.71 MB

193 KB

157 KB

lib/bisection.py 0 → 100644
 import numpy as np import numpy.linalg as la def getAngle(a,b,c): ba = a - b bc = c - b cosine_angle = np.dot(ba, bc) / (np.linalg.norm(ba) * np.linalg.norm(bc)) angle = np.arccos(cosine_angle) return angle def getBisecPoint(a,b,c): angle = getAngle(a,b,c) if(a[0]-b[0]<0): #check wich direction (left/right) the vector should point # if(angle < 180): angle = 360 - angle else: angle = 180 - angle angle = angle / 2 dist = la.norm(a-b) # dist = 100 d = (int(dist * np.cos(angle)), int(dist * np.sin(angle))) #with origin zero return (d[0]+b[0],d[1]+b[1]) #with origin b def poseToBisectVector(pose): points = pose[[0,1,8]] if(0.0 in points[:,2:]): #if one point has confidence zero, we can not generate the vector return None a,b,c = points[:,:2] # cut of confidence score so we have normal coordinate points bisecPoint = getBisecPoint(a,b,c) return np.array([bisecPoint,b])
lib/misc.py 0 → 100644
 def trp(point): #trim array to Point return (int(point[0]),int(point[1])) \ No newline at end of file
lib/triangles.py 0 → 100644
 import numpy as np triangleTopSortedIndices = [1,0,2,5,15,16,17,18] triangleLeftSortedIndices = [11,24,13,22,10,9] triangleRightSortedIndices = [14,21,19,20,13,12] def reduceToTrianglePoint(indices,pose): points = pose[indices] #filter out poses wich are not part of the points points indices if len(points) <= 0: return None #no relevant indices exist pointsFiltered = points[np.where(points[:,2] > 0.1)[0]] #filter out points bellow openpose confidence value of 0.5 if len(pointsFiltered) <= 0 or pointsFiltered is None: bestPoint = points[np.argsort(points[:,2])[::-1]][0] #points with best openpose confidence if bestPoint[2]>0.0: return bestPoint else: return None return None #else return point with best openpose confidence else: return pointsFiltered[0] #take first point if filterd has values def poseToTriangle(pose): top = reduceToTrianglePoint(triangleTopSortedIndices,pose) left = reduceToTrianglePoint(triangleLeftSortedIndices,pose) right = reduceToTrianglePoint(triangleRightSortedIndices,pose) if top is not None and left is not None and right is not None: retval = np.array([(top[0],top[1]),(left[0],left[1]),(right[0],right[1])]) return np.array(retval).astype(int) else: return None \ No newline at end of file
 # From Python # It requires OpenCV installed for Python import sys import cv2 import os from sys import platform import argparse sys.path.append('/Users/Tilman/Documents/Programme/Python/forschungspraktikum/openpose/python'); from openpose import pyopenpose as op try: # Custom Params (refer to include/openpose/flags.hpp for more parameters) params = dict() params["model_folder"] = "/Users/Tilman/Documents/Programme/Python/forschungspraktikum/openpose/models/" # Starting OpenPose opWrapper = op.WrapperPython() opWrapper.configure(params) opWrapper.start() # Process Image datum = op.Datum() imageToProcess = cv2.imread("/Users/Tilman/Documents/Programme/Python/forschungspraktikum/openpose/examples/media/art/page_19_item_5_08religi.jpg") datum.cvInputData = imageToProcess opWrapper.emplaceAndPop([datum]) # Display Image print("Body keypoints: \n" + str(datum.poseKeypoints)) cv2.imshow("OpenPose 1.5.1 - Tutorial Python API", datum.cvOutputData) cv2.waitKey(0) except Exception as e: print(e) sys.exit(-1)
 # import the necessary packages import numpy as np import argparse import glob import cv2 def auto_canny(image, sigma=0.33): # compute the median of the single channel pixel intensities v = np.median(image) # apply automatic Canny edge detection using the computed median lower = int(max(0, (1.0 - sigma) * v)) upper = int(min(255, (1.0 + sigma) * v)) edged = cv2.Canny(image, lower, upper) # return the edged image return edged # construct the argument parse and parse the arguments # load the image, convert it to grayscale, and blur it slightly image = cv2.imread('test2.jpg') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (3, 3), 0) # apply Canny edge detection using a wide threshold, tight # threshold, and automatically determined threshold wide = cv2.Canny(blurred, 10, 200) tight = cv2.Canny(blurred, 225, 250) auto = auto_canny(blurred) lines = cv2.HoughLinesP(image=auto, rho=10, theta=np.pi/360, threshold=100, minLineLength=100, maxLineGap=s) # if(lines): for line in lines: x1, y1, x2, y2 = line[0] cv2.line(image, (x1, y1), (x2, y2), (255, 0, 0), 3) # show the images cv2.namedWindow("Original", cv2.WINDOW_NORMAL) cv2.namedWindow("Edges", cv2.WINDOW_NORMAL) cv2.imshow("Original", image) cv2.imshow("Edges", np.hstack([wide, tight, auto])) cv2.waitKey(0) cv.destroyAllWindows() \ No newline at end of file
 # From Python import cv2 import numpy as np import numpy.linalg as la from scipy import stats from lib.bisection import * def trp(point): #trimPoint return (int(point[0]),int(point[1])) # poseToBisectVector [[187.13042 214.5613 0.9383101 ] # [204.06824 236.76866 0.94541883] # [202.77951 307.24927 0.85203063]] # poseToBisectVector [[138.87166 201.4717 0.93929714] # [112.72041 225.00642 0.82738197] # [ 94.437416 290.2507 0.67826796]] # poseToBisectVector [[3.0586206e+02 2.0805431e+02 8.8720602e-01] # [3.1895749e+02 2.3153630e+02 6.6084003e-01] # [3.3592047e+02 2.9677136e+02 1.7637391e-01]] # poseToBisectVector [[ 0. 0. 0. ] # [315.0623 232.79909 0.6628662] # [ 0. 0. 0. ]] points = [[187.13042,214.5613,0.9383101 ],[204.06824,236.76866,0.94541883],[202.77951,307.24927,0.85203063]] print("poseToBisectVector",points) pa = trp(points[0]) pb = trp(points[1]) pc = trp(points[2]) a = np.array(pa) b = np.array(pb) c = np.array(pc) dist1 = la.norm(a-b) dist2 = la.norm(b-c) print("distance",dist1,dist2) angle = getAngle(a,b,c) print(np.degrees(angle)) #degress print(angle) #radians pd = getBisecPoint(a,b,c) print(pd) img = cv2.imread("/Users/Tilman/Documents/Programme/Python/forschungspraktikum/openpose/examples/media/art/28165_gw.jpg") cv2.namedWindow("test", cv2.WINDOW_NORMAL) cv2.line(img, pa, pb, (0,255,0)); cv2.line(img, pb, pc, (0,255,0)); cv2.line(img, pb, pd, (0,0,255)); cv2.imshow("test", img) cv2.waitKey(0) cv2.destroyAllWindows() \ No newline at end of file
 import numpy as np import cv2 import os #images = ["28165_gw.jpg", "38172_gw.jpg", "92981_gw.jpg", "page_5_item_17_annunci1.jpg", "page_19_item_5_08religi.jpg"] images = ["658px-Giotto_di_Bondone_-_No._3_Scenes_from_the_Life_of_Joachim_-_3._Annunciation_to_St_Anne_-_WGA09171.jpg", "Ankunft_bei_den_Hirten.jpg", "Beweinung.jpg", "Darbringung_im_Tempel.jpg", "Franziskus-Giotto1.jpg", "Franziskus_Lossagung_vom_Vater.jpg", "Fusswaschung.jpg", "Giotto+-+Ambrogio+Bondone+-+Scrovegni+-+[10]+-+Prayer+of+the+Suitors+.jpeg", "Giotto-Di-Bondone-Flight-into-Egypt-2-.jpeg", "Giotto_-_Scrovegni_-_-06-_-_Meeting_at_the_Golden_Gate.jpg", "Giotto_di_Bondone_-_Joachims_Dream_-_Capella_degli_Scrovegni.jpg", "Giotto_di_Bondone_-_No._11_Scenes_from_the_Life_of_the_Virgin_-_5._Marriage_of_the_Virgin_-_WGA09183.jpg", "Giotto_di_Bondone_-_No._4_Scenes_from_the_Life_of_Joachim_-_4._Joachim's_Sacrificial_Offering_-_WGA09173.jpg", "Giotto_di_Bondone_-_No._9_Scenes_from_the_Life_of_the_Virgin_-_3._The_Bringing_of_the_Rods_to_the_Temple_-_WGA09181.jpg", "HochzeitzuKanan.jpg", "Judas_und_Christus.jpg", "Kindermord.jpg", "Verstoßung_Joachims_aus_Tempel.jpg", "flucht-nach-aegypten.jpg"] os.chdir('/Users/Tilman/Documents/Programme/Python/forschungspraktikum/images/out/images_imdahl/kmeans_k10/') #save images in this dir for img_name in images: img = cv2.imread("/Users/Tilman/Documents/Programme/Python/forschungspraktikum/images/images_imdahl/"+img_name) # img = cv2.imread(img_name) Z = img.reshape((-1,3)) # convert to np.float32 Z = np.float32(Z) # define criteria, number of clusters(K) and apply kmeans() criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) K = 10 ret,label,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS) # Now convert back into uint8, and make original image center = np.uint8(center) res = center[label.flatten()] res2 = res.reshape((img.shape)) cv2.imwrite(img_name,res2) # cv2.imshow('res2'+img_name,res2) # cv2.waitKey(0) #cv2.imwrite('/Users/Tilman/Documents/Programme/Python/forschungspraktikum/images/out/images_imdahl/kmean/'+img_name,res2) # cv2.waitKey(0) # cv2.destroyAllWindows() \ No newline at end of file
 import cv2 as cv import numpy as np img = cv.imread('test2.jpg', cv.IMREAD_COLOR) # road.png is the filename gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) #cv.imshow("Result gray", gray) edges = cv.Canny(gray, 10, 200) cv.imshow("Result edges", edges) #lines = cv.HoughLinesP(edges, 1, np.pi/180, 150, minLineLength=300, maxLineGap=50) lines = cv.HoughLinesP(image=edges, rho=4, theta=np.pi/360, threshold=100, minLineLength=100, maxLineGap=5) for line in lines: x1, y1, x2, y2 = line[0] cv.line(img, (x1, y1), (x2, y2), (255, 0, 0), 3) cv.imshow("Result Image", img) cv.waitKey(0) cv.destroyAllWindows() \ No newline at end of file
 ''' IMAGE SEGMENTATION USING K-MEANS (UNSUPERVISED LEARNING) AUTHOR Paul Asselin command line arguments: python imageSegmentation.py K inputImageFilename outputImageFilename where K is greater than 2 ''' import numpy as np import sys from PIL import Image from sklearn import preprocessing from sklearn.metrics.pairwise import euclidean_distances iterations = 5 # Parse command-line arguments # sets K, inputName & outputName if len(sys.argv) < 4: print("Error: Insufficient arguments, imageSegmentation takes three arguments") sys.exit() else: K = int(sys.argv[1]) if K < 3: print("Error: K has to be greater than 2") sys.exit() inputName = sys.argv[2] outputName = sys.argv[3] # Open input image image = Image.open(inputName) imageW = image.size[0] imageH = image.size[1] # Initialise data vector with attribute r,g,b,x,y for each pixel dataVector = np.ndarray(shape=(imageW * imageH, 5), dtype=float) # Initialise vector that holds which cluster a pixel is currently in pixelClusterAppartenance = np.ndarray(shape=(imageW * imageH), dtype=int) # Populate data vector with data from input image # dataVector has 5 fields: red, green, blue, x coord, y coord for y in range(0, imageH): for x in range(0, imageW): xy = (x, y) rgb = image.getpixel(xy) dataVector[x + y * imageW, 0] = rgb[0] dataVector[x + y * imageW, 1] = rgb[1] dataVector[x + y * imageW, 2] = rgb[2] dataVector[x + y * imageW, 3] = x dataVector[x + y * imageW, 4] = y # Standarize the values of our features dataVector_scaled = preprocessing.normalize(dataVector) # Set centers minValue = np.amin(dataVector_scaled) maxValue = np.amax(dataVector_scaled) centers = np.ndarray(shape=(K,5)) for index, center in enumerate(centers): centers[index] = np.random.uniform(minValue, maxValue, 5) for iteration in range(iterations): # Set pixels to their cluster for idx, data in enumerate(dataVector_scaled): distanceToCenters = np.ndarray(shape=(K)) for index, center in enumerate(centers): distanceToCenters[index] = euclidean_distances(data.reshape(1, -1), center.reshape(1, -1)) pixelClusterAppartenance[idx] = np.argmin(distanceToCenters) ################################################################################################## # Check if a cluster is ever empty, if so append a random datapoint to it clusterToCheck = np.arange(K) #contains an array with all clusters #e.g for K=10, array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) clustersEmpty = np.in1d(clusterToCheck, pixelClusterAppartenance) #^ [True True False True * n of clusters] False means empty for index, item in enumerate(clustersEmpty): if item == False: pixelClusterAppartenance[np.random.randint(len(pixelClusterAppartenance))] = index # ^ sets a random pixel to that cluster as mentioned in the homework writeup ################################################################################################## # Move centers to the centroid of their cluster for i in range(K): dataInCenter = [] for index, item in enumerate(pixelClusterAppartenance): if item == i: dataInCenter.append(dataVector_scaled[index]) dataInCenter = np.array(dataInCenter) centers[i] = np.mean(dataInCenter, axis=0) #TODO check for convergence print("Centers Iteration num", iteration, ": \n", centers) # set the pixels on original image to be that of the pixel's cluster's centroid for index, item in enumerate(pixelClusterAppartenance): dataVector[index][0] = int(round(centers[item][0] * 255)) dataVector[index][1] = int(round(centers[item][1] * 255)) dataVector[index][2] = int(round(centers[item][2] * 255)) # Save image image = Image.new("RGB", (imageW, imageH)) for y in range(imageH): for x in range(imageW): image.putpixel((x, y), (int(dataVector[y * imageW + x][0]), int(dataVector[y * imageW + x][1]), int(dataVector[y * imageW + x][2]))) image.save(outputName) \ No newline at end of file
 import numpy as np import cv2 as cv from matplotlib import pyplot as plt img = cv.imread('test2.jpg',0) plt.imshow(img, cmap = 'gray', interpolation = 'bicubic') plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis plt.plot(np.array([100, 200]),np.array([100,200]),'r-') plt.show() \ No newline at end of file
 import numpy as np import cv2 as cv # Load a color image in grayscale img = cv.imread('test2.jpg') cv.namedWindow('image', cv.WINDOW_NORMAL) cv.imshow('image',img) cv.waitKey(0) cv.destroyAllWindows() \ No newline at end of file
 # From Python import sys import cv2 import numpy as np import os from scipy import stats from sys import platform #sys.path.append('/Users/Tilman/Documents/Programme/Python/forschungspraktikum/openpose/python'); from openpose import pyopenpose as op params = dict() params["model_folder"] = "/Users/Tilman/Documents/Programme/Python/forschungspraktikum/openpose/models/" # Starting OpenPose opWrapper = op.WrapperPython() opWrapper.configure(params) opWrapper.start() images = ["28165_gw.jpg", "38172_gw.jpg", "92981_gw.jpg", "page_5_item_17_annunci1.jpg", "page_19_item_5_08religi.jpg"] #images = ["28165_gw.jpg"] for img_name in images: # Process Image datum = op.Datum() #img = cv2.imread("/Users/Tilman/Documents/Programme/Python/forschungspraktikum/openpose/examples/media/art/page_19_item_5_08religi.jpg") img = cv2.imread("/Users/Tilman/Documents/Programme/Python/forschungspraktikum/openpose/examples/media/art/"+img_name) datum.cvInputData = img opWrapper.emplaceAndPop([datum]) img = datum.cvOutputData # Display Image bodys = [((pose[1][0],pose[1][1]),(pose[8][0],pose[8][1])) for pose in datum.poseKeypoints if pose[1][2]>0 and pose[8][2]>0] #filter out low results def pose_line_plot(a,b): if (a[0]==0 and a[1]==0) or (b[0]==0 and b[1]==0): return cv2.line(img, (a[0],a[1]), (b[0],b[1]), (0,0,255), 10) def pose_plot(pose): pose_line_plot(pose[0],pose[1]) pose_line_plot(pose[1],pose[8]) pose_line_plot(pose[1],pose[8]) pose_line_plot(pose[9],pose[10]) pose_line_plot(pose[10],pose[11]) pose_line_plot(pose[12],pose[13]) pose_line_plot(pose[12],pose[14]) def pose_line(pose): pose_plot(pose) # print("posetest") # print(pose) # print("posetest2") relpose = pose[[0,1,8,9,10,11,12,13,14]] #only cut out relevant points, see indices from img: https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/doc/output.md#pose-output-format-body_25 filtered = np.array(list(filter(lambda x: x[0]!=0.0 and x[1]!=0.0,relpose))) # filter out bad results x,y = filtered.transpose()[:2] print(x) print(y) slope, intercept, r_value, p_value, std_err = stats.linregress(x,y) return ((int(x[0]),int(intercept + slope*x[0])),(int(x[-1]),int(intercept + slope*x[-1]))) pose_regs = [pose_line(pose) for pose in datum.poseKeypoints if pose[1][2]>0 and pose[8][2]>0] #filter out low results print("new points: \n"+str(bodys)) print("new points: \n"+str(pose_regs)) name = "linreg pose "+img_name cv2.namedWindow(name, cv2.WINDOW_NORMAL) for regression in pose_regs: cv2.line(img, regression[0], regression[1], (0,255,0), 5) for body in bodys: cv2.line(img, body[0], body[1], (0,0,255), 3) cv2.imshow(name, img) cv2.waitKey(0) cv2.destroyAllWindows() \ No newline at end of file
 # example script for using PyGaze # # # # # # importing the relevant libraries import random import constants from pygaze import libscreen from pygaze import libtime from pygaze import liblog from pygaze import libinput from pygaze import eyetracker # # # # # # experiment setup # create display object disp = libscreen.Display() # create eyetracker object tracker = eyetracker.EyeTracker(disp) # create keyboard object keyboard = libinput.Keyboard(keylist=['space'], timeout=None) # create logfile object log = liblog.Logfile() log.write(["trialnr", "trialtype", "endpos", "latency", "correct"]) # create screens inscreen = libscreen.Screen() inscreen.draw_text(text="When you see a cross, look at it and press space. Then make an eye movement to the black circle when it appears.\n\n(press space to start)", fontsize=24) fixscreen = libscreen.Screen() fixscreen.draw_fixation(fixtype='cross',pw=3) targetscreens = {} targetscreens['left'] = libscreen.Screen() targetscreens['left'].draw_circle(pos=(int(constants.DISPSIZE[0]*0.25),constants.DISPSIZE[1]/2), fill=True) targetscreens['right'] = libscreen.Screen() targetscreens['right'].draw_circle(pos=(int(constants.DISPSIZE[0]*0.75),constants.DISPSIZE[1]/2), fill=True) feedbackscreens = {} feedbackscreens[1] = libscreen.Screen() feedbackscreens[1].draw_text(text='correct', colour=(0,255,0), fontsize=24) feedbackscreens[0] = libscreen.Screen() feedbackscreens[0].draw_text(text='incorrect', colour=(255,0,0), fontsize=24) # # # # # # run the experiment # calibrate eye tracker tracker.calibrate() # show instructions disp.fill(inscreen) disp.show() keyboard.get_key() # run 20 trials for trialnr in range(1,21): # prepare trial trialtype = random.choice(['left','right']) # drift correction checked = False while not checked: disp.fill(fixscreen) disp.show() checked = tracker.drift_correction() # start eye tracking tracker.start_recording() tracker.status_msg("trial %d" % trialnr) tracker.log("start_trial %d trialtype %s" % (trialnr, trialtype)) # present fixation disp.fill(screen=fixscreen) disp.show() tracker.log("fixation") libtime.pause(random.randint(750, 1250)) # present target disp.fill(targetscreens[trialtype]) t0 = disp.show() tracker.log("target %s" % trialtype) # wait for eye movement t1, startpos = tracker.wait_for_saccade_start() endtime, startpos, endpos = tracker.wait_for_saccade_end() # stop eye tracking tracker.stop_recording() # process input: