python - 循环遍历图像并获取用户输入的数据标签(值从 1 到 3)

标签 python

我正在尝试直接根据个人偏好(1-3 的整数)手动标记一些图像,以便我可以在带有 CNN 的学习个性化项目中使用它。

对于如何尝试在 Python 中执行此操作有什么建议吗?我尝试了下面的操作,但最终发生的情况是,一旦我放入标签,图像就不会自行关闭并循环到下一张。相反,我必须手动单击查看器窗口之外的内容。

import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from shutil import copyfile
import sys

#define folders 
data_src = "data/Photos/"
one_label = "data/one_label"
two_label = "data/two_label"
three_label = "data/three_label"

extensionsToCheck = ['jpg', 'png']

listing = os.listdir(data_src)
print("src file subdirectories: ", listing)

def userInput( file_name):
    user_input = sys.stdin.read(1)
    if(user_input=='1'):
        print("don't care")
        copyfile(data_src+file_name, one_label+'/'+file_name)
    elif(user_input=='2'):
        print("neutral")
        copyfile(data_src+file_name, two_label+'/'+file_name)
    elif(user_input=='3'):
        print("like")
        copyfile(data_src+file_name, three_label+'/'+file_name)
    elif(user_input=="e"):
        print("exit")
        os._exit(0)
    else:
        return    

def main():
    print("size of dir is: ", len(listing))
    for file in listing:
        print("file: ", file)
        if any(ext in file for ext in extensionsToCheck):
            plt.figure(file)
            img=mpimg.imread(data_src + '/' + file)
            imgplot = plt.imshow(img)
            plt.title(file)
            mng = plt.get_current_fig_manager()
            mng.full_screen_toggle()
            plt.show()

            user_input = userInput(file)
            plt.close()


if __name__ == '__main__':main()

最佳答案

显示图像时,Python 执行会暂停,这意味着它不会接受用户输入,直到您手动关闭图像。但是,您可以使用 tkinter 或 PyQt 构建 GUI 来显示图像以及 1、2 和 3 按钮。

更新

经过进一步研究,我发现 plt 有工具 plt.ion()plt.pause() 可以在绘图期间启用交互模式。但是,您必须手动单击终端来输入您的分类号(1,2,3)

import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from shutil import copyfile
import sys

#define folders 
data_src = "data/Photos/"
one_label = "data/one_label"
two_label = "data/two_label"
three_label = "data/three_label"

extensionsToCheck = ['jpg', 'png']

listing = os.listdir(data_src)
print("src file subdirectories: ", listing)

def userInput( file_name):
    user_input = input()
    if(user_input=='1'):
        print("don't care")
        copyfile(data_src+file_name, one_label+'/'+file_name)
        print("here")
    elif(user_input=='2'):
        print("neutral")
        copyfile(data_src+file_name, two_label+'/'+file_name)
    elif(user_input=='3'):
        print("like")
        copyfile(data_src+file_name, three_label+'/'+file_name)
    elif(user_input=="e"):
        print("exit")
        os._exit(0)
    else:
        return  0  

def main():
    print("size of dir is: ", len(listing))
    for file in listing:
        print("file: ", file)
        if any(ext in file for ext in extensionsToCheck):
            plt.figure(file)
            img=plt.imread(data_src + '/' + file)
            imgplot = plt.imshow(img)
            plt.title(file)
            #I disabled the 2 lines below because you viewing the image in full screen will prevent you from accessing the terminal to enter the classification number
            # mng = plt.get_current_fig_manager()
            # mng.full_screen_toggle()

            plt.ion()  #Turn the interactive mode on.
            plt.show()
            plt.pause(0.001) #Pause for interval seconds.
            user_input = userInput(file)
            plt.close()


if __name__ == '__main__':main()

祝你好运!

关于python - 循环遍历图像并获取用户输入的数据标签(值从 1 到 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59023450/

相关文章:

python - 如何使用 python 将混淆矩阵记录到 azureml 平台

python - 使用python在Lambda函数中运行命令wirth SSM

python - TypeError : 'torch.dtype' object is not callable. 如何调用这个函数?

python - 启用 selenium python 后找到并单击按钮

python - 将列表添加到新列表

python - 查找所有内置异常的简单函数在直接运行时有效,但在用作导入模块时失败,不提供回溯

python - Python 中的骰子统计

python - 使用 GroupBy 将新列添加到数据框

python - 如何在私信中发送图片?

python - Cython 中 C++ 函数的性能不佳