【Python脚本】手动过滤图片数据样本的自动化操作

这段代码实现了一个用于图像数据标注的交互式程序。用户可以通过键盘输入来标记图像,如'z'保存图片到缺失标签列表,'x'保存到缺少车辆列表,'a'忽略,'q'退出。程序读取指定目录下的图像文件,显示并等待用户输入,然后将图像路径写入相应的JSON文件。
摘要由CSDN通过智能技术生成

可以通过键盘快速操作

import cv2
import os
import argparse
import numpy as np 
from tqdm import tqdm
import shutil
import random
import glob
import json
import sys
import tty
import termios

def parse_arg():
    """
    Parse command line input parameters
    """
    parser = argparse.ArgumentParser(description="Calculate the accuracy of license plate recognition")
    parser.add_argument("--input_image_paths", type=str, default="/home/yangbinchao/桌面/雄研/draw_test_data_image/draw",help=" File path to be processed ")
    args = parser.parse_args()
    print()
    print(f"args = {args}")
    print()
    return args

def readchar():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(sys.stdin.fileno())
        ch = sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return ch

def readkey(getchar_fn=None):
    getchar = getchar_fn or readchar
    c1 = getchar()
    if ord(c1) != 0x1b:
        return c1
    c2 = getchar()
    if ord(c2) != 0x5b:
        return c1
    c3 = getchar()
    return chr(0x10 + ord(c3) - 65)

def wait_key(img_path):  # wait the key is put down
    while True:
        key=readkey()
        if key=='z':  # save the path 
            print('save the image path list',img_path)
            #miss_label_list.append(img_path)
            with open( './miss_label_list.json',  'a+',  encoding='utf-8') as f:  # 车牌对应的颜色标签
                f.write(json.dumps(img_path,ensure_ascii=False,indent=1)+',')
            break
        if key=='x':  
            print('save the image path list',img_path)
            #lack_vehicle_list.append(img_path)
            with open( './lack_vehicle_list.json',  'a+',  encoding='utf-8') as f:  # 车牌对应的颜色标签
                f.write(json.dumps(img_path,ensure_ascii=False,indent=1)+',')
            break
        if key=='a':
            print('ignore the image path list',img_path)
            break
        if key=='q':
            return key
            break

def read_img(img_path,image_dir):
    image_path = os.path.join( image_dir, img_path)
    img = cv2.imread(image_path) 
    img = cv2.resize(img,(1000,500))
    cv2.imshow(img_path, img) 
    while cv2.waitKey(2500) != 27:# loop time is 3000 if not get ESC
        if cv2.getWindowProperty('img_path' ,cv2.WND_PROP_VISIBLE) <= 0:
            break
    cv2.destroyAllWindows() 

def main(args):

    for img_path in tqdm(os.listdir(args.input_image_paths)):
        read_img(img_path, image_dir=args.input_image_paths)
        key_value = wait_key(img_path)
        if(key_value == 'q'):
            print('\nthe save image path list is {}'.format(miss_label_list))
            print('\nthe save image path list is {}'.format(lack_vehicle_list))
            exit()
    print('\nthe save image path list is {}'.format(miss_label_list))
    print('\nthe save image path list is {}'.format(lack_vehicle_list))

if __name__ == "__main__":
    args = parse_arg()
    miss_label_list = []
    lack_vehicle_list = []
    main(args)

        
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值