0

我正在尝试创建一个脚本,允许用户循环浏览图像目录,并为每一个标记关键点(左键单击标记绿色圆圈,右键单击标记红色圆圈)。

仅供参考,该脚本将用于创建用于机器学习的标记数据库。在每个图像的每个循环结束时,我想将文件名和关键点位置附加到稍后将被腌制和引用的列表中。

我的问题是我可以为单个图像创建这个(见下文),但似乎无法让它适用于图像列表。

提前感谢您的支持!

### 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
4

0 回答 0