Created
August 14, 2017 19:26
-
-
Save hoyeunglee/df7e6cb9b76c576b26fd2bb2b26bfe2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
from functools import partial | |
import sys | |
import numpy as np | |
import cv2 | |
from pylab import plot,show | |
from numpy import vstack,array | |
from numpy.random import rand | |
from scipy.cluster.vq import kmeans,vq | |
from pywinauto.application import Application | |
import win32api, win32con | |
import sys | |
import time | |
import os | |
#import pyscreenshot as ImageGrab | |
PY3 = sys.version_info[0] == 3 | |
if PY3: | |
xrange = range | |
def mse(imageA, imageB): | |
err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2) | |
err /= float(imageA.shape[0] * imageA.shape[1]) | |
return err | |
def click(x,y): | |
win32api.SetCursorPos((x,y)) | |
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0) | |
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0) | |
im = cv2.imread(r'C:\Users\martlee2\Documents\roster2.png') | |
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) | |
gray = np.float32(gray) | |
#dst = cv2.cornerHarris(gray,2,3,0.04) | |
dst = cv2.cornerHarris(gray,14,5,0.04) | |
dst = cv2.dilate(dst,None) | |
#im[dst>0.01*dst.max()]=[0,0,255] | |
dd = dst>0.01*dst.max() | |
#out = np.zeros(im.shape,np.uint8) | |
#gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) | |
#thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2) | |
m5 = cv2.imread(r'C:\Users\martlee2\Documents\roster2.png') | |
im = cv2.imread(r'C:\Users\martlee2\Documents\roster2.png') | |
#im3 = im.copy() | |
ma = Image.open("roster2.png") | |
color1 = ma.load() | |
print ma.size | |
print color1[1,1] | |
color1 = ma.load() | |
print ma.size | |
print color1[1,1] | |
colortolocation = {} | |
def addtogroupkey(keyandmemory, key1, memorycontent): | |
k = key1 | |
if k in keyandmemory: | |
keyandmemory[k].append(memorycontent) | |
else: | |
keyandmemory[k] = [memorycontent] | |
return keyandmemory | |
#for ii in range(0, ma.size[0]): | |
# for jj in range(0, ma.size[1]): | |
#if color1[ii,jj][0] <= 102 and color1[ii,jj][2] <= 57 and color1[ii,jj][2] > 0 and color1[ii,jj][0] > 0: | |
#colortolocation = addtogroupkey(colortolocation, (0,0,0), (ii,jj)) | |
def isneighborlocation(lo1, lo2): | |
if abs(lo1[0] - lo2[0]) <= 70 and abs(lo1[1] - lo2[1]) <= 12: | |
return 1 | |
elif abs(lo1[0] - lo2[0]) == 1 and lo1[1] == lo2[1]: | |
return 1 | |
elif abs(lo1[1] - lo2[1]) == 1 and lo1[0] == lo2[0]: | |
return 1 | |
else: | |
return 0 | |
eachcolor = (0,0,0) | |
#max_key = sorted(colortolocation, key=lambda k: len(colortolocation[k]), reverse=True)[:12] | |
#del max_key[0] | |
#for eachcolor in max_key: | |
# print eachcolor | |
#for eachcolor in colortolocation: | |
#if 1 == 1: | |
#for eachcolor in max_key: | |
eachcolor = (0,0,0) | |
if 1 == 1: | |
#testing1 = list(colortolocation[eachcolor]) | |
#testing1.sort(key=lambda x: (x[0], x[1])) | |
#locations = testing1 | |
#testing1 = [list((float(elem[0]),float(elem[1]))) for elem in testing1] | |
testing1 = zip(*np.where(dd == 1)) | |
locations = testing1 | |
testing1 = [list((float(elem[0]),float(elem[1]))) for elem in testing1] | |
centroids,_ = kmeans(testing1,10) | |
row,col = vq(testing1,centroids) | |
locationsgroup = {} | |
keyone = len(locationsgroup)+1 | |
for jj in range(0, len(row)): | |
for ii in range(0, len(np.nonzero(row == jj)[0])): | |
locationsgroup = addtogroupkey(locationsgroup, keyone, locations[np.nonzero(row == jj)[0][ii]]) | |
keyone = len(locationsgroup)+1 | |
#for ii in range(0, len(np.nonzero(row == jj)[0])): | |
#locationsgroup = addtogroupkey(locationsgroup, keyone, locations[np.nonzero(row == jj)[0][ii]]) | |
#keyone = len(locationsgroup)+1 | |
colortolocation[eachcolor] = locationsgroup | |
count = 0 | |
#for eachcolor in max_key: | |
#app = Application().start("notepad.exe") | |
#dlg_spec = app.window(title='Untitled - Notepad') | |
#dlg_spec.maximize() | |
eachcolor = (0,0,0) | |
if 1 == 1: | |
for kk in colortolocation[eachcolor]: | |
if len(colortolocation[eachcolor][kk]) > 0: | |
x = min(colortolocation[eachcolor][kk])[0]-5 | |
y = min(colortolocation[eachcolor][kk])[1]-5 | |
xw = max(colortolocation[eachcolor][kk])[0]+10 | |
yh = max(colortolocation[eachcolor][kk])[1]+7 | |
cv2.rectangle(im,(x,y),(xw,yh),(0,0,255),2) | |
#if x < 300 and y < 100: | |
#click((x+xw)/2,(y+yh)/2) | |
#time.sleep(5) | |
cv2.imwrite("C:\\Users\\martlee2\\Documents\\words\\"+str(count)+".png",im[y:yh,x:xw]) | |
count = count + 1 | |
im[dst>0.01*dst.max()]=[0,0,255] | |
cv2.imshow('norm',im) | |
cv2.waitKey(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment