I'm trying to create a script that allows a user to cycle through a directory of images, and for each one mark key points (left click marks the green circle, and right click marks the red one).
FYI the script is to be used to create a labeled database for machine learning. At the end of each loop per image, I'd like to append filename and key point locations to a list that will be pickled and referenced later.
My problem is that I can create this (see below) for a single image, but can't seem to get it to work for a list of images.
Thanks in advance for your support!
### Import Modules
import pygame
import os
### Functions
# Load image
_image_library = {}
def get_image(path):
global _image_library
image = _image_library.get(path)
if image == None:
canonicalized_path = path.replace('/', os.sep).replace('\\', os.sep)
image = pygame.image.load(canonicalized_path)
_image_library[path] = image
return image
##############
### Script ###
pygame.init()
screen = pygame.display.set_mode((1200, 700))
done = False
clock = pygame.time.Clock()
## Initializations
# Colors
color_blue = (143,184,222)
color_teal = (68,161,160)
color_red = (200,0,0)
color_grey = (26,47,42)
# Standard initailizations
point_confirmed = True
active_color = color_teal
margin = 5
kpi_radius = 5
x, y = 30, -250 #starting location
fontsize = 36
font = 'helvetica' if 'helvetica' in pygame.font.get_fonts() else pygame.font.Font(None, fontsize) # Instantiate system's default font
# Image loop initialization
active_text = font.render("Image Loaded", True, color_grey)
image_flagged = False
kpi_primary = (0,0)
kpi_secondary = (0,0)
image = get_image('/Users/omeed/Dropbox/Spottr/Scripts/Data/4 AP JPG_1001_0.jpg')
while not done:
for event in pygame.event.get(): # Empties event queue (else OS will think program unresponsive)
# Non key/mouse inputs
if event.type == pygame.QUIT:
done = True
# Key inputs
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP]: y -= 100
if pressed[pygame.K_DOWN]: y += 100
if pressed[pygame.K_LEFT]: x -= 100
if pressed[pygame.K_RIGHT]: x += 100
if pressed[pygame.K_f]: image_flagged = not image_flagged
if pressed[pygame.K_ESCAPE]: done = True
if pressed[pygame.K_EQUALS]: kpi_radius = min(100, kpi_radius + 3)
if pressed[pygame.K_MINUS]: kpi_radius = max(1, kpi_radius - 3)
if pressed[pygame.K_SPACE]: point_confirmed = not point_confirmed
active_color = color_blue if point_confirmed else color_teal
# Mouse inputs
clicked = pygame.mouse.get_pressed()
if clicked[0]==1:
kpi_primary = pygame.mouse.get_pos() # Relative screen location
imgpixel_primary = ( kpi_primary[0]-x , kpi_primary[1]-y ) #Image pixel location (column, row)
if clicked[2]==1:
kpi_secondary = pygame.mouse.get_pos() # Relative screen location
imgpixel_secondary = ( kpi_secondary[0]-x , kpi_secondary[1]-y ) #Image pixel location (column, row)
# Blitting and Filling
screen.fill((0, 0, 0)) # Reset screen to black before updating with new keypress to move rectangle
screen.blit(image, (x, y))
pygame.draw.rect(screen, active_color, pygame.Rect(30, 30, 300, fontsize*2))
screen.blit(active_text, (35,35) )
if kpi_primary != (0,0):
pygame.draw.circle(screen, (0,255,0), (imgpixel_primary[0]+x,imgpixel_primary[1]+y), kpi_radius, 1)
if kpi_secondary != (0,0):
pygame.draw.circle(screen, (255,0,0), (imgpixel_secondary[0]+x,imgpixel_secondary[1]+y), kpi_radius, 1)
pygame.display.flip() # Required for updates to game screen to become visible
clock.tick(60) # Slows program to 60fps by blocking GUI e-xecution until 1/60 of sec passed