python3 快速删除数据集模糊图片(按键)

自己从视频中弄出来的图片做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()

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值