自己从视频中弄出来的图片做voc数据集,有的比较模糊想删掉,写了个脚本
需要opencv python3
不要手动关闭图片!!!
图片在images文件夹中可以自己改路径
按w 删除该图片
按a 上一张图片
按d 下一张图片
按q 退出
import os
from PIL import Image
from pynput import mouse,keyboard
import cv2 as cv
import numpy as np
#指定路径
path = './images'
filelist = os.listdir(path)
fileloc = os.path.abspath(path)
num = len(filelist)
index = 0
ext = False
def on_press(key):
pass
def on_release(key):
global index,num,ext
ext=False
if key.char =='w':
item=filelist[index]
filelist.remove(item)
num = len(filelist)
if index>=num :
index=num-1
if num==0:
ext=True
try:
os.remove(src)
print(item+"被删除\n")
except:
print(item+"delete error!!!\n")
finally:
return False
if key.char == 'a':
if (index-1) >= 0:
index=index-1
else:
print("no left!!!")
return False
if key.char == 'd':
if (index+1)< num:
index=index+1
else:
print("no right!!!")
return False
if key.char == 'q':
ext=True
return False
cv.namedWindow("input_image",0)
cv.resizeWindow("input_image",500,500)
while(True):
try:
if num == 0:
print("图片为0!!!")
break
src = os.path.join(fileloc, filelist[index])
img = cv.imdecode(np.fromfile(src, dtype=np.uint8), 1)
cv.imshow('input_image', img)
cv.waitKey(0)
# # 监听键盘按键
with keyboard.Listener(on_press=on_press,on_release=on_release) as listener:
listener.join()
if ext:
break
else:
continue
except:
print("while error\n")
continue
cv.destroyAllWindows()