Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import win32api
- import pygame
- import time
- import datetime
- import numpy
- import math
- import os,errno
- from PIL import ImageGrab
- import xlwt
- from tkinter import *
- from operator import itemgetter
- import shutil
- class settitlename(Frame):
- def __init__(self, master=None):
- Frame.__init__(self, master)
- self.pack()
- def scale():
- root = Tk()
- root.configure(background='#BFAEFF')
- root.resizable(width=False, height=False)
- app = settitlename(master=root)
- app.master.title("Level of cancer")
- v = IntVar()
- v.set(0)
- Label(root,
- text="""Please specify confidence:""",
- bg="#BFAEFF",
- font=("Tahoma", 14),
- fg="white",
- justify = LEFT,
- padx = 20, pady=10).pack()
- Radiobutton(root,
- text="Uncertain (50%)",
- bg="#BFAEFF",
- padx = 20,
- pady=10,
- variable=v,
- font=("Tahoma", 14),
- value=1).pack()
- Radiobutton(root,
- text="Expected Yes (60%)",
- bg="#BFAEFF",
- padx = 20,
- pady=10,
- variable=v,
- font=("Tahoma", 14),
- value=2).pack()
- Radiobutton(root,
- text="Likely (70%)",
- bg="#BFAEFF",
- padx = 20,
- pady=10,
- variable=v,
- font=("Tahoma", 14),
- value=3).pack()
- Radiobutton(root,
- text="Pretty sure (80%)",
- bg="#BFAEFF",
- padx = 20,
- pady=10,
- variable=v,
- font=("Tahoma", 14),
- value=4).pack()
- Radiobutton(root,
- text="Absolute (99%)",
- bg="#BFAEFF",
- padx = 20,
- pady=10,
- variable=v,
- font=("Tahoma", 14),
- value=5).pack()
- root.mainloop()
- return v.get()
- screen = pygame.display.set_mode((1,1),pygame.NOFRAME)
- print("Bone scintigraphy label Tool")
- print("1) Press \"X\" to capture the screen")
- print("2) Drag mouse on cancer area for localization")
- print("3) Segment the cancer point by dragging mouse")
- print("4) Press \"S\" to save the segmentation point in .csv file")
- print(" Press \"E\" to erase drawing in the image")
- print(" Press \"R\" to resize the image")
- print(" Press \"F\" when you finished label")
- print(" Press \"Q\" to quit the program")
- print("Log transaction\n")
- start =(-1,-1)
- end =(-1,-1)
- color = (255,0,0)
- capture = False
- screenres = (win32api.GetSystemMetrics(0),win32api.GetSystemMetrics(1))
- dest = (0,0)
- filename =""
- draw = False
- resize = False
- topleft =(-1,-1)
- topleftget =False
- bottomright =(-1,-1)
- bottomrightget =False
- resizeready =False
- ccount=0
- data = []
- drawpair = set([])
- line = set([])
- line_xls = set([])
- allbbox=[]
- wb = xlwt.Workbook()
- ws = wb.add_sheet("Sheet1")
- crop_topleft = (-1,-1)
- get_crop_topleft = False
- current_point = (-1,-1)
- zoom_scale = 1
- def createbbox(line):
- max_x = max(line, key=itemgetter(0))[0]
- min_x = min(line, key=itemgetter(0))[0]
- max_y = max(line, key=itemgetter(1))[1]
- min_y = min(line, key=itemgetter(1))[1]
- topleftbox = (min_x,min_y)
- bottomrightbox = (max_x,max_y)
- return topleftbox,bottomrightbox
- def saveDataToNewFile(st,level,line_xls,tlp,brp):
- r = 4
- c = 0
- wb = xlwt.Workbook()
- ws = wb.add_sheet("Sheet1")
- ws.write(0, c, level)
- ws.write(1, 0, "Bounding Box Position")
- ws.write(2, 0, tlp[0])
- ws.write(2, 1, tlp[1])
- ws.write(2, 2, brp[0])
- ws.write(2, 3, brp[1])
- print("BoundingBox is "+"("+str(tlp[0])+","+str(tlp[1])+")"+"("+str(brp[0])+","+str(brp[1])+")")
- allbbox.append((tlp[0],tlp[1],brp[0],brp[1]))
- ws.write(3, 0, "X")
- ws.write(3, 1, "Y")
- for p in line_xls:
- ws.write(r, c, p[0])
- ws.write(r, c+1, p[1])
- r += 1
- wb.save("data/"+st+"/image_"+str(ccount)+"/"+"image_"+str(ccount)+".csv")
- while True:
- if (capture):
- ccount=0
- zoom_scale=1
- st = datetime.datetime.fromtimestamp(time.time()).strftime('%Y%m%d_%H%M%S')
- im=ImageGrab.grab().convert('L')
- try:
- os.makedirs("data/"+st)
- except OSError as e:
- if e.errno != errno.EEXIST:
- raise
- filename=st+".bmp"
- im.save("data/" + st + "/OriginalImage.bmp", format='BMP' , subsampling=0 , quality=100)
- im.save("data/"+st+"/"+"FullImage.bmp", format='BMP' , subsampling=0 , quality=100)
- screen = pygame.display.set_mode(screenres, pygame.FULLSCREEN,32)
- image = pygame.image.load("data/"+st+"/"+"FullImage.bmp")
- screen.blit(image,image.get_rect())
- line.clear()
- line_xls.clear()
- capture = False
- a = win32api.GetKeyState(0x01)
- if (draw):
- if a < 0:
- end=win32api.GetCursorPos()
- if (drawrect.collidepoint(end)):
- if start == (-1,-1):
- start = end
- pygame.draw.line(screen,color,start,end,2)
- drawpair.add((start,end))
- start=end
- #if zoom_scale != 1:
- #inipos = (int((screenres[0] - image_scale.get_rect().size[0]) / 2),int((screenres[1] - image_scale.get_rect().size[1]) / 2))
- #line.add( (end[0] - (inipos[0] - (topleft[0]-(end[0]-inipos[0]))) , end[1] - (inipos[1] - (topleft[1]-(end[1]-inipos[1])))) )
- #line_xls.add((end[0] - (inipos[0] - topleft[0]), end[1] - (inipos[1] - topleft[1])))
- #else:
- size = numpy.subtract(bottomright,topleft)
- x = size[0]
- y = size[1]
- inipos = (int((screenres[0] - x) / 2), int((screenres[1] - y) / 2))
- line.add((end[0] - inipos[0], end[1] - inipos[1]))
- line_xls.add((end[0]-(inipos[0]-topleft[0]),end[1]-(inipos[1]-topleft[1])))
- else:
- start =(-1,-1)
- elif (topleftget):
- topleft =win32api.GetCursorPos()
- topleftget=False
- elif(bottomrightget):
- bottomright =win32api.GetCursorPos()
- bottomrightget =False
- resizeready =True
- elif (resizeready):
- size =numpy.subtract(bottomright,topleft)
- x = size[0]
- y = size[1]
- if x!=y and x>=0 and y>=0:
- rect = pygame.Rect(0,0,x, y)
- rect.topleft = topleft
- rect.bottomright =bottomright
- sub = screen.subsurface(rect)
- try:
- os.makedirs("data/"+st+"/image_"+str(ccount))
- except OSError as e:
- if e.errno != errno.EEXIST:
- raise
- pygame.image.save(sub, "data/"+st+"/image_"+str(ccount)+"/"+"r_"+str(ccount)+".bmp")
- screen = pygame.display.set_mode(screenres, pygame.FULLSCREEN,32)
- screen.fill((0,0,0))
- image = pygame.image.load("data/"+st+"/image_"+str(ccount)+"/"+"r_"+str(ccount)+".bmp")
- inipos = (int((screenres[0]-x)/2),int((screenres[1]-y)/2))
- drawrect =screen.blit(image,inipos)
- subscreen = screen.subsurface(drawrect)
- resizeready =False
- resize =False
- draw =True
- else:
- print("Please resize the image again (Drag from top-left to bottom-right)")
- resizeready=False
- #x = screen capture -> resize the screen
- #r = resizing again
- #e = erase all draw point
- #s = save without closing the program
- events = pygame.event.get()
- for event in events:
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_x:
- if (not draw):
- capture = True
- resize =True
- elif event.key == pygame.K_s:
- if len(line)!=0:
- tlp,brp = createbbox(line)
- pygame.image.save(subscreen, "data/"+st+"/image_"+str(ccount)+"/"+"s_"+str(ccount)+".bmp")
- #pygame.draw.rect(subscreen, (0, 0, 255),(tlp[0], tlp[1], (brp[0] - tlp[0]) + 2, (brp[1] - tlp[1]) + 2), 1)
- #pygame.image.save(subscreen, "data/"+st+"/image_"+str(ccount)+"/"+"b_"+str(ccount)+".bmp")
- draw=False
- screen = pygame.display.set_mode((1,1),pygame.NOFRAME)
- pygame.display.flip()
- level = scale()
- #tlp, brp = createbbox(line_xls)
- # saveDataToNewFile(st, level, line_xls,tlp,brp)
- print("Level of image" + filename + " is " + str(level))
- screen = pygame.display.set_mode(screenres, pygame.FULLSCREEN, 32)
- image = pygame.image.load("data/"+st+"/"+"FullImage.bmp")
- screen.blit(image, image.get_rect())
- line.clear()
- line_xls.clear()
- for point in drawpair:
- pygame.draw.line(screen, color, (point[0][0]-(inipos[0]-topleft[0]),point[0][1]-(inipos[1]-topleft[1])),(point[1][0]-(inipos[0]-topleft[0]),point[1][1]-(inipos[1]-topleft[1])), 2)
- pygame.image.save(screen, "data/"+st+"/"+"FullImage.bmp")
- drawpair.clear()
- ccount = ccount + 1
- resize = True
- else:
- print("Please draw in the image")
- elif event.key == pygame.K_r:
- drawpair.clear()
- line.clear()
- line_xls.clear()
- os.remove("data/"+st+"/image_"+str(ccount)+"/"+"r_"+str(ccount)+".bmp")
- shutil.rmtree("data/"+st+"/image_"+str(ccount))
- screen = pygame.display.set_mode(screenres, pygame.FULLSCREEN, 32)
- image = pygame.image.load("data/" + st + "/" + "FullImage.bmp")
- screen.blit(image, image.get_rect())
- resize =True
- draw =False
- capture = False
- elif event.key == pygame.K_e:
- if resize==False and resizeready==False:
- drawpair.clear()
- line.clear()
- line_xls.clear()
- screen = pygame.display.set_mode(screenres, pygame.FULLSCREEN, 32)
- screen.fill((0, 0, 0))
- screen.fill((0, 0, 0))
- image = pygame.image.load("data/"+st+"/image_"+str(ccount)+"/"+"r_"+str(ccount)+".bmp")
- inipos = (int((screenres[0] - x) / 2), int((screenres[1] - y) / 2))
- drawrect = screen.blit(image, inipos)
- subscreen = screen.subsurface(drawrect)
- resizeready = False
- resize = False
- draw = True
- elif event.key == pygame.K_f:
- '''
- if(draw==False and resize==True):
- for b in range(0,ccount):
- tlx = allbbox[b][0]
- tly = allbbox[b][1]
- brx = allbbox[b][2]
- bry = allbbox[b][3]
- pygame.draw.rect(screen, (0, 0, 255),(tlx, tly, (brx - tlx) + 2, (bry - tly) + 2), 1)
- pygame.image.save(screen,"data/"+st+"/"+"Allbbox.bmp")
- '''
- screen = pygame.display.set_mode((1, 1), pygame.NOFRAME)
- elif event.key == pygame.K_q:
- pygame.quit()
- sys.exit()
- elif event.type == pygame.MOUSEBUTTONUP:
- if event.button == 1:
- if (resize and not bottomrightget):
- bottomrightget =True
- get_crop_topleft=False
- image = pygame.image.load("data/" + st + "/" + "FullImage.bmp")
- screen.blit(image, image.get_rect())
- elif event.type == pygame.MOUSEBUTTONDOWN:
- if event.button==1:
- if (resize and not topleftget):
- topleftget =True
- crop_topleft=win32api.GetCursorPos()
- current_point=crop_topleft
- get_crop_topleft=True
- elif event.button==4 and draw==True :
- drawpair.clear()
- line.clear()
- line_xls.clear()
- width_default = image.get_rect().size[0]
- height_default = image.get_rect().size[1]
- if zoom_scale==1:
- width_default = math.floor(width_default*1.5)
- height_default = math.floor(height_default*1.5)
- zoom_scale=1.5
- elif zoom_scale==1.5:
- width_default = math.floor(width_default * 2)
- height_default = math.floor(height_default * 2)
- zoom_scale=2
- elif zoom_scale==2:
- width_default = math.floor(width_default * 2.5)
- height_default = math.floor(height_default * 2.5)
- zoom_scale = 2.5
- elif zoom_scale==2.5:
- width_default = math.floor(width_default * 3)
- height_default = math.floor(height_default * 3)
- zoom_scale = 3
- elif zoom_scale==3:
- width_default = math.floor(width_default * 3.5)
- height_default = math.floor(height_default * 3.5)
- zoom_scale = 3.5
- if zoom_scale!=3.5:
- screen = pygame.display.set_mode(screenres, pygame.FULLSCREEN, 32)
- screen.fill((0, 0, 0))
- image_scale = pygame.transform.scale(image, (width_default, height_default))
- inipos = (int((screenres[0] - width_default) / 2), int((screenres[1] - height_default) / 2))
- drawrect = screen.blit(image_scale, inipos)
- subscreen = screen.subsurface(drawrect)
- elif event.button==5 and draw==True :
- drawpair.clear()
- line.clear()
- line_xls.clear()
- width_default = image.get_rect().size[0]
- height_default = image.get_rect().size[1]
- if zoom_scale == 1.5:
- width_default = math.floor(width_default)
- height_default = math.floor(height_default)
- zoom_scale = 1
- elif zoom_scale == 2:
- width_default = math.floor(width_default * 1.5)
- height_default = math.floor(height_default * 1.5)
- zoom_scale = 1.5
- elif zoom_scale == 2.5:
- width_default = math.floor(width_default * 2)
- height_default = math.floor(height_default * 2)
- zoom_scale = 2
- elif zoom_scale == 3:
- width_default = math.floor(width_default * 2.5)
- height_default = math.floor(height_default * 2.5)
- zoom_scale = 2.5
- elif zoom_scale == 3.5:
- width_default = math.floor(width_default * 3)
- height_default = math.floor(height_default * 3)
- zoom_scale = 3
- screen = pygame.display.set_mode(screenres, pygame.FULLSCREEN, 32)
- screen.fill((0, 0, 0))
- image_scale = pygame.transform.scale(image, (width_default, height_default))
- inipos = (int((screenres[0] - width_default) / 2), int((screenres[1] - height_default) / 2))
- drawrect = screen.blit(image_scale, inipos)
- subscreen = screen.subsurface(drawrect)
- if get_crop_topleft==True:
- if win32api.GetCursorPos()!= current_point:
- image = pygame.image.load("data/" + st + "/" + "FullImage.bmp")
- screen.blit(image, image.get_rect())
- current_point = win32api.GetCursorPos()
- pygame.draw.rect(screen, (0, 0, 255), (crop_topleft[0], crop_topleft[1], (current_point[0] - crop_topleft[0]) + 2,(current_point[1] - crop_topleft[1]) + 2), 2)
- pygame.display.update()
- wb.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement