当前位置: 首页 > news >正文

筛选图片,写JSON文件和复制

筛选图片,写JSON文件和复制

  • 筛选图片,写JSON文件
  • 筛选图片复制

筛选图片,写JSON文件

# coding: utf-8
from PIL import Image, ImageDraw, ImageFont
import os
import shutil
import cv2 as cv
import numpy as np
import jsondef zh_ch(string):return string.encode("gbk").decode('UTF-8', errors='ignore')def create_json(path):path_dict = {}path = path.split('\\')file_name = path[-1]return path_dict, file_namedef get_root(scr):scr = scr.split('\\')root_path = os.path.join(*scr[0:-1])file_name_1 = scr[-2]file_name_0 = scr[-1]return root_path, file_name_1, file_name_0def save_json(filename, dicts):filename = filename.replace(':', ':\\')with open(filename, 'w') as f:json_str = json.dumps(dicts)f.write(json_str)def image_add_text(img1, text, left, top, text_color, text_size):if isinstance(img1, np.ndarray):# 判断图片是否为ndarray格式,转为成PIL的格式的RGB图片image = Image.fromarray(cv.cvtColor(img1, cv.COLOR_BGR2RGB))draw = ImageDraw.Draw(image)# 创建一个可以在给定图像上绘图的对象font_style = ImageFont.truetype("font/simsun.ttc", text_size, encoding='utf-8')# 参数依次为 字体、字体大小、编码draw.text((left, top), text, text_color, font=font_style)# 参数依次为位置、文本、颜色、字体return cv.cvtColor(np.asarray(image), cv.COLOR_RGB2BGR)def redu(image, ratio):width = int(image.shape[1] * ratio)height = int(image.shape[0] * ratio)image = cv.resize(image, (width, height), interpolation=cv.INTER_AREA)return image
def bor_image(image, path, file_name):path = path.split('\\')if file_name[path[-1]] == 0:sen = '无缺陷'elif file_name[path[-1]] == 1:sen = '有缺陷'else:sen = '不确定'text = path[-1] + '     ' + sen +'\n上一张:w,下一张:s,退出:q\n无缺陷:Backspace,有缺陷:Enter,不确定:{'border_img = cv.copyMakeBorder(image, 70, 2, 2, 2, cv.BORDER_CONSTANT, value=[255, 255, 255])border_img = image_add_text(border_img, text, 0, 0, (255, 0, 0), 20)cv.imshow('image', border_img)def read_json(file_name):with open(file_name, 'rb') as f:data = json.load(f)return data
def press_key(path_lsit):# image = cv.imread(path, cv.IMREAD_GRAYSCALE)i = 0root_paths = []file_dict = {}while(0 <= i and i <= len(path_lsit)):print(path_lsit[i])if 'bmp' not in path_lsit[i]:i = i + 1continueroot_path, file_name_1, file_name_0 = get_root(path_lsit[i])print(root_path)if os.path.exists(os.path.join(root_path, 'kuaisu.json')):file_dict = read_json(os.path.join(root_path, 'kuaisu.json'))else:file_dict = {}image = cv.imdecode(np.fromfile(path_lsit[i], dtype=np.uint8), cv.IMREAD_COLOR)  # 输入图片路径,当路径中有中文时需要采用该语句image = cv.transpose(image)print(file_dict)# 缩小图片image = redu(image, 0.55)if file_name_0 not in file_dict:file_dict[file_name_0] = 0  # 无缺陷print(file_dict)bor_image(image, path_lsit[i], file_dict)key = cv.waitKey(0)if key == 115:i = i + 1save_json(os.path.join(root_path, 'kuaisu.json'), file_dict)continueif key == 119:i = i - 1save_json(os.path.join(root_path, 'kuaisu.json'), file_dict)continueif key == 13 or key == 8 or key == 91:#enter/baskspace/{if key == 13:file_dict[file_name_0] = 1#有缺陷bor_image(image, path_lsit[i], file_dict)print(file_dict)elif key == 8:file_dict[file_name_0] = 0#无缺陷bor_image(image, path_lsit[i], file_dict)print(file_dict)else:file_dict[file_name_0] = 2#不确定bor_image(image, path_lsit[i], file_dict)print(file_dict)save_json(os.path.join(root_path, 'kuaisu.json'), file_dict)key = cv.waitKey(0)if key == 115:i = i + 1save_json(os.path.join(root_path, 'kuaisu.json'), file_dict)continueelif key == 119:i = i - 1save_json(os.path.join(root_path, 'kuaisu.json'), file_dict)continueif key == 113:save_json(os.path.join(root_path, 'kuaisu.json'), file_dict[file_name_1])cv.destroyAllWindows()breakif __name__ == '__main__':image_root_path = "E:\正在标注"path_list = []scr_path = []# save_path_name = 'H:ImageStore\\ti'for root, dirs, files in os.walk(image_root_path):for file in files:path = os.path.join(root, file)if 'bmp' in path:path_list.append(path)press_key(path_list)

筛选图片复制

# coding: utf-8
from PIL import Image, ImageDraw, ImageFont
import os
import shutil
import cv2 as cv
import numpy as np
import json# 保留无缺陷的图片def zh_ch(string):return string.encode("gbk").decode('UTF-8', errors='ignore')def create_json(path):path_dict = {}path = path.split('\\')file_name = path[-1]return path_dict, file_namedef get_root(scr):scr = scr.split('\\')root_path = os.path.join(*scr[0:-1])file_name_1 = scr[-2]file_name_0 = scr[-1]return root_path, file_name_1, file_name_0def save_json(filename, dicts):filename = filename.replace(':', ':\\')with open(filename, 'w') as f:json_str = json.dumps(dicts)f.write(json_str)def image_add_text(img1, text, left, top, text_color, text_size):if isinstance(img1, np.ndarray):# 判断图片是否为ndarray格式,转为成PIL的格式的RGB图片image = Image.fromarray(cv.cvtColor(img1, cv.COLOR_BGR2RGB))draw = ImageDraw.Draw(image)# 创建一个可以在给定图像上绘图的对象font_style = ImageFont.truetype("font/simsun.ttc", text_size, encoding='utf-8')# 参数依次为 字体、字体大小、编码draw.text((left, top), text, text_color, font=font_style)# 参数依次为位置、文本、颜色、字体return cv.cvtColor(np.asarray(image), cv.COLOR_RGB2BGR)def redu(image, ratio):width = int(image.shape[1] * ratio)height = int(image.shape[0] * ratio)image = cv.resize(image, (width, height), interpolation=cv.INTER_AREA)return image
def bor_image(image, path, file_name):path = path.split('\\')if file_name[path[-1]] == 0:sen = '无缺陷'elif file_name[path[-1]] == 1:sen = '有缺陷'else:sen = '不确定'text = path[-1] + '     ' + sen +'\n上一张:w,下一张:s,退出:q\n有缺陷:Enter'border_img = cv.copyMakeBorder(image, 70, 2, 2, 2, cv.BORDER_CONSTANT, value=[255, 255, 255])border_img = image_add_text(border_img, text, 0, 0, (255, 0, 0), 20)cv.imshow('image', border_img)def read_json(file_name):with open(file_name, 'rb') as f:data = json.load(f)return data
def press_key(path_lsit,save_path):# image = cv.imread(path, cv.IMREAD_GRAYSCALE)i = 0root_paths = []file_dict = {}while(0 <= i and i <= len(path_lsit)):print(path_lsit[i])if 'bmp' not in path_lsit[i]:i = i + 1continueroot_path, file_name_1, file_name_0 = get_root(path_lsit[i])print(root_path)if os.path.exists(os.path.join(root_path, 'kuaisu.json')):file_dict = read_json(os.path.join(root_path, 'kuaisu.json'))else:file_dict = {}image = cv.imdecode(np.fromfile(path_lsit[i], dtype=np.uint8), cv.IMREAD_COLOR)  # 输入图片路径,当路径中有中文时需要采用该语句image = cv.transpose(image)print(file_dict)# 缩小图片image = redu(image, 0.55)if file_name_0 not in file_dict:file_dict[file_name_0] = 0  # 无缺陷print(file_dict)bor_image(image, path_lsit[i], file_dict)key = cv.waitKey(0)if key == 115:i = i + 1continueif key == 119:i = i - 1continueif key == 13 or key == 8 or key == 91:#enter/baskspace/{if key == 13:file_dict[file_name_0] = 1#有缺陷bor_image(image, path_lsit[i], file_dict)shutil.copy(path_lsit[i], save_path)  # shutil.copy函数放入原文件的路径文件全名  然后放入目标文件夹key = cv.waitKey(0)if key == 32:  # 空格#os.remove(save_path)my_list = path_lsit[i].split('\\')print(my_list[-1])os.remove(save_path+"\\"+my_list[-1])if key == 115:i = i + 1continueelif key == 119:i = i - 1continueif key == 113:cv.destroyAllWindows()breakif __name__ == '__main__':image_root_path = "D:\\Datasets\\defect2\\final9.4\\yes"path_list = []scr_path = []# save_path_name = 'H:ImageStore\\ti'for root, dirs, files in os.walk(image_root_path):for file in files:path = os.path.join(root, file)if 'bmp' in path:path_list.append(path)press_key(path_list,"E:\\Datasets\\defect2\\final9.4\\yes")
http://www.lryc.cn/news/156013.html

相关文章:

  • C++并发编程:构建线程安全队列(第二部分:细粒度锁)
  • 浅述C++内存管理——new与malloc的不同
  • 语言基础篇11——函数、函数参数类型、装饰器、生成器
  • linux jar包class热部署 工具 arthas安装及使用
  • Android studio 调整jar包顺序
  • 用Qt写的机器视觉绘图工具
  • Spring Boot 打包,将依赖全部打进去
  • SpringCloud入门实战(十五)分布式事务框架Seata简介
  • MySQL数据库 主从复制与读写分离
  • Qt day2
  • JavaScript - 好玩的打字动画
  • rpm打包
  • 匠心新品:大彩科技超薄7寸WIFI线控器发布,热泵、温控器、智能家电首选!
  • 华为云云服务器评测|使用云耀云服务器L实例部署Portainer工具
  • C++并发编程:构建线程安全队列(第一部分:粗粒度锁)
  • C++设计模式-更新中
  • Hydra工具的使用
  • Pytorch学习:卷积神经网络—nn.Conv2d、nn.MaxPool2d、nn.ReLU、nn.Linear和nn.Dropout
  • 水果库存系统(SSM+Thymeleaf版)
  • 如何在VueJS应用程序中设置Toast通知
  • css让元素保持等比例宽高
  • 骨传导和入耳式哪个危害大一点?入耳式和骨传导哪种好?
  • 介绍OpenCV
  • Android中的view绘制流程,简单理解
  • 商城开发:店铺管理系统应具备哪些功能?
  • 小白学go基础04-命名惯例对标识符进行命名
  • 使用iCloud和Shortcuts实现跨设备同步与自动化数据采集
  • Spring框架-基于STOMP使用Websocket
  • kafka-- 安装kafka manager及简单使用
  • 深圳-海岸城购物中心数据分析