Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 16.27 KB | None | 0 0
  1. import win32api
  2. import pygame
  3. import time
  4. import datetime
  5. import numpy
  6. import math
  7. import os,errno
  8. from PIL import ImageGrab
  9. import xlwt
  10. from tkinter import *
  11. from operator import itemgetter
  12. import shutil
  13.  
  14. class settitlename(Frame):
  15.     def __init__(self, master=None):
  16.         Frame.__init__(self, master)
  17.         self.pack()
  18.  
  19. def scale():
  20.     root = Tk()
  21.     root.configure(background='#BFAEFF')
  22.     root.resizable(width=False, height=False)
  23.     app = settitlename(master=root)
  24.     app.master.title("Level of cancer")
  25.     v = IntVar()
  26.     v.set(0)
  27.     Label(root,
  28.           text="""Please specify confidence:""",
  29.           bg="#BFAEFF",
  30.           font=("Tahoma", 14),
  31.           fg="white",
  32.           justify = LEFT,
  33.           padx = 20, pady=10).pack()
  34.     Radiobutton(root,
  35.                 text="Uncertain (50%)",
  36.                 bg="#BFAEFF",
  37.                 padx = 20,
  38.                 pady=10,
  39.                 variable=v,
  40.                 font=("Tahoma", 14),
  41.                 value=1).pack()
  42.     Radiobutton(root,
  43.                 text="Expected Yes (60%)",
  44.                 bg="#BFAEFF",
  45.                 padx = 20,
  46.                 pady=10,
  47.                 variable=v,
  48.                 font=("Tahoma", 14),
  49.                 value=2).pack()
  50.     Radiobutton(root,
  51.                 text="Likely (70%)",
  52.                 bg="#BFAEFF",
  53.                 padx = 20,
  54.                 pady=10,
  55.                 variable=v,
  56.                 font=("Tahoma", 14),
  57.                 value=3).pack()
  58.     Radiobutton(root,
  59.                 text="Pretty sure (80%)",
  60.                 bg="#BFAEFF",
  61.                 padx = 20,
  62.                 pady=10,
  63.                 variable=v,
  64.                 font=("Tahoma", 14),
  65.                 value=4).pack()
  66.     Radiobutton(root,
  67.                 text="Absolute (99%)",
  68.                 bg="#BFAEFF",
  69.                 padx = 20,
  70.                 pady=10,
  71.                 variable=v,
  72.                 font=("Tahoma", 14),
  73.                 value=5).pack()
  74.     root.mainloop()
  75.     return v.get()
  76.  
  77. screen = pygame.display.set_mode((1,1),pygame.NOFRAME)
  78. print("Bone scintigraphy label Tool")
  79. print("1) Press \"X\" to capture the screen")
  80. print("2) Drag mouse on cancer area for localization")
  81. print("3) Segment the cancer point by dragging mouse")
  82. print("4) Press \"S\" to save the segmentation point in .csv file")
  83. print("   Press \"E\" to erase drawing in the image")
  84. print("   Press \"R\" to resize the image")
  85. print("   Press \"F\" when you finished label")
  86. print("   Press \"Q\" to quit the program")
  87. print("Log transaction\n")
  88. start =(-1,-1)
  89. end =(-1,-1)
  90. color = (255,0,0)
  91. capture = False
  92.  
  93. screenres = (win32api.GetSystemMetrics(0),win32api.GetSystemMetrics(1))
  94. dest = (0,0)
  95. filename =""
  96. draw = False
  97. resize = False
  98. topleft =(-1,-1)
  99. topleftget =False
  100. bottomright =(-1,-1)
  101. bottomrightget =False
  102. resizeready =False
  103. ccount=0
  104. data = []
  105. drawpair = set([])
  106. line = set([])
  107. line_xls = set([])
  108. allbbox=[]
  109. wb = xlwt.Workbook()
  110. ws = wb.add_sheet("Sheet1")
  111. crop_topleft = (-1,-1)
  112. get_crop_topleft = False
  113. current_point = (-1,-1)
  114. zoom_scale = 1
  115.  
  116. def createbbox(line):
  117.     max_x = max(line, key=itemgetter(0))[0]
  118.     min_x = min(line, key=itemgetter(0))[0]
  119.     max_y = max(line, key=itemgetter(1))[1]
  120.     min_y = min(line, key=itemgetter(1))[1]
  121.     topleftbox = (min_x,min_y)
  122.     bottomrightbox = (max_x,max_y)
  123.     return topleftbox,bottomrightbox
  124.  
  125. def saveDataToNewFile(st,level,line_xls,tlp,brp):
  126.     r = 4
  127.     c = 0
  128.     wb = xlwt.Workbook()
  129.     ws = wb.add_sheet("Sheet1")
  130.     ws.write(0, c, level)
  131.     ws.write(1, 0, "Bounding Box Position")
  132.     ws.write(2, 0, tlp[0])
  133.     ws.write(2, 1, tlp[1])
  134.     ws.write(2, 2, brp[0])
  135.     ws.write(2, 3, brp[1])
  136.     print("BoundingBox is "+"("+str(tlp[0])+","+str(tlp[1])+")"+"("+str(brp[0])+","+str(brp[1])+")")
  137.     allbbox.append((tlp[0],tlp[1],brp[0],brp[1]))
  138.     ws.write(3, 0, "X")
  139.     ws.write(3, 1, "Y")
  140.     for p in line_xls:
  141.         ws.write(r, c, p[0])
  142.         ws.write(r, c+1, p[1])
  143.         r += 1
  144.     wb.save("data/"+st+"/image_"+str(ccount)+"/"+"image_"+str(ccount)+".csv")
  145.  
  146. while True:
  147.     if (capture):
  148.         ccount=0
  149.         zoom_scale=1
  150.         st = datetime.datetime.fromtimestamp(time.time()).strftime('%Y%m%d_%H%M%S')
  151.         im=ImageGrab.grab().convert('L')
  152.         try:
  153.             os.makedirs("data/"+st)
  154.         except OSError as e:
  155.             if e.errno != errno.EEXIST:
  156.                 raise
  157.         filename=st+".bmp"
  158.         im.save("data/" + st + "/OriginalImage.bmp", format='BMP' , subsampling=0 , quality=100)
  159.         im.save("data/"+st+"/"+"FullImage.bmp", format='BMP' , subsampling=0 , quality=100)
  160.         screen = pygame.display.set_mode(screenres, pygame.FULLSCREEN,32)
  161.         image = pygame.image.load("data/"+st+"/"+"FullImage.bmp")
  162.         screen.blit(image,image.get_rect())
  163.         line.clear()
  164.         line_xls.clear()
  165.         capture = False
  166.     a = win32api.GetKeyState(0x01)
  167.     if (draw):
  168.         if a < 0:
  169.             end=win32api.GetCursorPos()
  170.             if (drawrect.collidepoint(end)):
  171.                 if start == (-1,-1):
  172.                     start = end
  173.                 pygame.draw.line(screen,color,start,end,2)
  174.                 drawpair.add((start,end))
  175.                 start=end
  176.                 #if zoom_scale != 1:
  177.                     #inipos = (int((screenres[0] - image_scale.get_rect().size[0]) / 2),int((screenres[1] - image_scale.get_rect().size[1]) / 2))
  178.                     #line.add( (end[0] - (inipos[0] - (topleft[0]-(end[0]-inipos[0]))) , end[1] - (inipos[1] - (topleft[1]-(end[1]-inipos[1])))) )
  179.                     #line_xls.add((end[0] - (inipos[0] - topleft[0]), end[1] - (inipos[1] - topleft[1])))
  180.                 #else:
  181.                 size = numpy.subtract(bottomright,topleft)
  182.                 x = size[0]
  183.                 y = size[1]
  184.                 inipos = (int((screenres[0] - x) / 2), int((screenres[1] - y) / 2))
  185.                 line.add((end[0] - inipos[0], end[1] - inipos[1]))
  186.                 line_xls.add((end[0]-(inipos[0]-topleft[0]),end[1]-(inipos[1]-topleft[1])))
  187.         else:
  188.             start =(-1,-1)
  189.     elif (topleftget):
  190.         topleft =win32api.GetCursorPos()
  191.         topleftget=False
  192.     elif(bottomrightget):
  193.         bottomright =win32api.GetCursorPos()
  194.         bottomrightget =False
  195.         resizeready =True
  196.     elif (resizeready):
  197.         size =numpy.subtract(bottomright,topleft)
  198.         x = size[0]
  199.         y = size[1]
  200.         if x!=y and x>=0 and y>=0:
  201.             rect = pygame.Rect(0,0,x, y)
  202.             rect.topleft = topleft
  203.             rect.bottomright =bottomright
  204.             sub = screen.subsurface(rect)
  205.             try:
  206.                 os.makedirs("data/"+st+"/image_"+str(ccount))
  207.             except OSError as e:
  208.                 if e.errno != errno.EEXIST:
  209.                     raise
  210.             pygame.image.save(sub, "data/"+st+"/image_"+str(ccount)+"/"+"r_"+str(ccount)+".bmp")
  211.             screen = pygame.display.set_mode(screenres, pygame.FULLSCREEN,32)
  212.             screen.fill((0,0,0))
  213.             image = pygame.image.load("data/"+st+"/image_"+str(ccount)+"/"+"r_"+str(ccount)+".bmp")
  214.             inipos = (int((screenres[0]-x)/2),int((screenres[1]-y)/2))
  215.             drawrect =screen.blit(image,inipos)
  216.             subscreen = screen.subsurface(drawrect)
  217.             resizeready =False
  218.             resize =False
  219.             draw =True
  220.         else:
  221.             print("Please resize the image again (Drag from top-left to bottom-right)")
  222.             resizeready=False
  223. #x = screen capture -> resize the screen
  224. #r = resizing again
  225. #e = erase all draw point
  226. #s = save without closing the program
  227.     events = pygame.event.get()
  228.     for event in events:
  229.         if event.type == pygame.KEYDOWN:
  230.             if event.key == pygame.K_x:
  231.                 if (not draw):
  232.                     capture = True
  233.                     resize =True
  234.             elif event.key == pygame.K_s:
  235.                 if len(line)!=0:
  236.                     tlp,brp = createbbox(line)
  237.                     pygame.image.save(subscreen, "data/"+st+"/image_"+str(ccount)+"/"+"s_"+str(ccount)+".bmp")
  238.                     #pygame.draw.rect(subscreen, (0, 0, 255),(tlp[0], tlp[1], (brp[0] - tlp[0]) + 2, (brp[1] - tlp[1]) + 2), 1)
  239.                     #pygame.image.save(subscreen, "data/"+st+"/image_"+str(ccount)+"/"+"b_"+str(ccount)+".bmp")
  240.                     draw=False
  241.                     screen = pygame.display.set_mode((1,1),pygame.NOFRAME)
  242.                     pygame.display.flip()
  243.                     level = scale()
  244.                     #tlp, brp = createbbox(line_xls)
  245.                    # saveDataToNewFile(st, level, line_xls,tlp,brp)
  246.                     print("Level of image" + filename + " is " + str(level))
  247.                     screen = pygame.display.set_mode(screenres, pygame.FULLSCREEN, 32)
  248.                     image = pygame.image.load("data/"+st+"/"+"FullImage.bmp")
  249.                     screen.blit(image, image.get_rect())
  250.                     line.clear()
  251.                     line_xls.clear()
  252.                     for point in drawpair:
  253.                         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)
  254.                     pygame.image.save(screen, "data/"+st+"/"+"FullImage.bmp")
  255.                     drawpair.clear()
  256.                     ccount = ccount + 1
  257.                     resize = True
  258.                 else:
  259.                     print("Please draw in the image")
  260.             elif event.key == pygame.K_r:
  261.                 drawpair.clear()
  262.                 line.clear()
  263.                 line_xls.clear()
  264.                 os.remove("data/"+st+"/image_"+str(ccount)+"/"+"r_"+str(ccount)+".bmp")
  265.                 shutil.rmtree("data/"+st+"/image_"+str(ccount))
  266.                 screen = pygame.display.set_mode(screenres, pygame.FULLSCREEN, 32)
  267.                 image = pygame.image.load("data/" + st + "/" + "FullImage.bmp")
  268.                 screen.blit(image, image.get_rect())
  269.                 resize =True
  270.                 draw =False
  271.                 capture = False
  272.             elif event.key == pygame.K_e:
  273.                 if resize==False and resizeready==False:
  274.                     drawpair.clear()
  275.                     line.clear()
  276.                     line_xls.clear()
  277.                     screen = pygame.display.set_mode(screenres, pygame.FULLSCREEN, 32)
  278.                     screen.fill((0, 0, 0))
  279.                     screen.fill((0, 0, 0))
  280.                     image = pygame.image.load("data/"+st+"/image_"+str(ccount)+"/"+"r_"+str(ccount)+".bmp")
  281.                     inipos = (int((screenres[0] - x) / 2), int((screenres[1] - y) / 2))
  282.                     drawrect = screen.blit(image, inipos)
  283.                     subscreen = screen.subsurface(drawrect)
  284.                     resizeready = False
  285.                     resize = False
  286.                     draw = True
  287.             elif event.key == pygame.K_f:
  288.                 '''
  289.                if(draw==False and resize==True):
  290.                    for b in range(0,ccount):
  291.                        tlx = allbbox[b][0]
  292.                        tly = allbbox[b][1]
  293.                        brx = allbbox[b][2]
  294.                        bry = allbbox[b][3]
  295.                        pygame.draw.rect(screen, (0, 0, 255),(tlx, tly, (brx - tlx) + 2, (bry - tly) + 2), 1)
  296.                    pygame.image.save(screen,"data/"+st+"/"+"Allbbox.bmp")
  297.                '''
  298.                 screen = pygame.display.set_mode((1, 1), pygame.NOFRAME)
  299.             elif event.key == pygame.K_q:
  300.                 pygame.quit()
  301.                 sys.exit()
  302.         elif event.type == pygame.MOUSEBUTTONUP:
  303.             if event.button == 1:
  304.                 if (resize and not bottomrightget):
  305.                     bottomrightget =True
  306.                     get_crop_topleft=False
  307.                     image = pygame.image.load("data/" + st + "/" + "FullImage.bmp")
  308.                     screen.blit(image, image.get_rect())
  309.         elif event.type == pygame.MOUSEBUTTONDOWN:
  310.             if event.button==1:
  311.                 if (resize and not topleftget):
  312.                     topleftget =True
  313.                     crop_topleft=win32api.GetCursorPos()
  314.                     current_point=crop_topleft
  315.                     get_crop_topleft=True
  316.             elif event.button==4 and draw==True :
  317.                 drawpair.clear()
  318.                 line.clear()
  319.                 line_xls.clear()
  320.                 width_default = image.get_rect().size[0]
  321.                 height_default = image.get_rect().size[1]
  322.                 if zoom_scale==1:
  323.                     width_default = math.floor(width_default*1.5)
  324.                     height_default = math.floor(height_default*1.5)
  325.                     zoom_scale=1.5
  326.                 elif zoom_scale==1.5:
  327.                     width_default = math.floor(width_default * 2)
  328.                     height_default = math.floor(height_default * 2)
  329.                     zoom_scale=2
  330.                 elif zoom_scale==2:
  331.                     width_default = math.floor(width_default * 2.5)
  332.                     height_default = math.floor(height_default * 2.5)
  333.                     zoom_scale = 2.5
  334.                 elif zoom_scale==2.5:
  335.                     width_default = math.floor(width_default * 3)
  336.                     height_default = math.floor(height_default * 3)
  337.                     zoom_scale = 3
  338.                 elif zoom_scale==3:
  339.                     width_default = math.floor(width_default * 3.5)
  340.                     height_default = math.floor(height_default * 3.5)
  341.                     zoom_scale = 3.5
  342.                 if  zoom_scale!=3.5:
  343.                     screen = pygame.display.set_mode(screenres, pygame.FULLSCREEN, 32)
  344.                     screen.fill((0, 0, 0))
  345.                     image_scale = pygame.transform.scale(image, (width_default, height_default))
  346.                     inipos = (int((screenres[0] - width_default) / 2), int((screenres[1] - height_default) / 2))
  347.                     drawrect = screen.blit(image_scale, inipos)
  348.                     subscreen = screen.subsurface(drawrect)
  349.             elif event.button==5 and draw==True :
  350.                 drawpair.clear()
  351.                 line.clear()
  352.                 line_xls.clear()
  353.                 width_default = image.get_rect().size[0]
  354.                 height_default = image.get_rect().size[1]
  355.                 if zoom_scale == 1.5:
  356.                     width_default = math.floor(width_default)
  357.                     height_default = math.floor(height_default)
  358.                     zoom_scale = 1
  359.                 elif zoom_scale == 2:
  360.                     width_default = math.floor(width_default * 1.5)
  361.                     height_default = math.floor(height_default * 1.5)
  362.                     zoom_scale = 1.5
  363.                 elif zoom_scale == 2.5:
  364.                     width_default = math.floor(width_default * 2)
  365.                     height_default = math.floor(height_default * 2)
  366.                     zoom_scale = 2
  367.                 elif zoom_scale == 3:
  368.                     width_default = math.floor(width_default * 2.5)
  369.                     height_default = math.floor(height_default * 2.5)
  370.                     zoom_scale = 2.5
  371.                 elif zoom_scale == 3.5:
  372.                     width_default = math.floor(width_default *  3)
  373.                     height_default = math.floor(height_default * 3)
  374.                     zoom_scale = 3
  375.                 screen = pygame.display.set_mode(screenres, pygame.FULLSCREEN, 32)
  376.                 screen.fill((0, 0, 0))
  377.                 image_scale = pygame.transform.scale(image, (width_default, height_default))
  378.                 inipos = (int((screenres[0] - width_default) / 2), int((screenres[1] - height_default) / 2))
  379.                 drawrect = screen.blit(image_scale, inipos)
  380.                 subscreen = screen.subsurface(drawrect)
  381.         if get_crop_topleft==True:
  382.             if win32api.GetCursorPos()!= current_point:
  383.                 image = pygame.image.load("data/" + st + "/" + "FullImage.bmp")
  384.                 screen.blit(image, image.get_rect())
  385.                 current_point = win32api.GetCursorPos()
  386.                 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)
  387.  
  388.     pygame.display.update()
  389.  
  390. wb.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement