python - 遍历图像并获取用户输入的数据标签(值从 1-3)
问题描述
我正在尝试根据个人喜好(从 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()
祝你好运!
推荐阅读
- c++ - (b&(1<> i) & 1) C++
- c++ - 如何根据 cpp 模板中的实际类型调用整数类型?
- javascript - 为什么当我在 angularjs 中使用 ES6 语法时 gulp-uglify 不起作用
- javascript - javascript 不返回所需的值
- java - 如何在 Scala 中为 Kafka (ConsumerRebalanceListener) 实现 Java 接口?
- vba - 查找和替换表中的值
- html - 做一个
标签更易于访问
- c# - 使用多个文本框进行 SQL LIKE 搜索?
- git - 正确删除 git 历史记录,无需任何后续参考
- azure - 使用 Ajax 调用经过 Active Directory 身份验证的 Web api