如何解决从子文件夹中读取所有图像,检测它们的文本并将它们以原始名称和文件夹保存在 Python 中?
我有一个 train 文件夹,它包含 20 个类,每个类都包含 .png 和 .jpg 格式的不同图像。我已经在一张图像上进行了测试,效果很好,现在我想通过维护文件夹和类结构来将此过程应用于所有图像。在检测到的文本之后,我们有一个带有“最终”名称的文件夹,它包含 20 个类,每个类中包含一个检测到的文本图像。检测到的文本代码已经可以正常工作
代码
my_path = "Path"
files = glob.glob(my_path + '/**/*.jpg',recursive=True)
images = []
each_image = ''
for root,dirs,files in os.walk(my_path,topdown=False):
for file in files:
each_image = file
print(root)
print(dirs)
for file in files:
image_path = file
with open(image_path,'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
response = client.text_detection(image=image)
texts = response.text_annotations
img_openCV = cv2.imread(image_path)
h,w = img_openCV.shape[:2]
print(h,w)
for text in texts:
box_w = abs(text.bounding_poly.vertices[2].x - text.bounding_poly.vertices[0].x)
box_h = abs(text.bounding_poly.vertices[2].y - text.bounding_poly.vertices[0].y)
if box_h * box_w > thres * h * w:
continue
if (text.bounding_poly.vertices[2].x - text.bounding_poly.vertices[0].x) > (w / 2):
print('ebug width ')
print(text.bounding_poly.vertices[2].x - text.bounding_poly.vertices[0].x)
else:
imagee = cv2.rectangle(img_openCV,(text.bounding_poly.vertices[0].x,text.bounding_poly.vertices[0].y),(text.bounding_poly.vertices[2].x,text.bounding_poly.vertices[2].y),(0,255,0),-1)
print('height,width,color:',text.bounding_poly)
cv2.imwrite(
os.path.join("Path",each_image + str(total_images) + '.png'),imagee)
total_images += 1
解决方法
import os,io
import random
from turtle import color
from google.cloud import vision
import numpy as np
from PIL import Image,ImageDraw,ImageFont
import cv2
from text import draw_borders
import glob
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "GoogleVisionAPI.json"
client = vision.ImageAnnotatorClient()
# file_name = '3017.jpg'
# image_folder = './img/'
total_images = 1
thres = 0.5
# Location with subdirectories
my_path = "/Path of whole folder which contains subclasses/"
files = glob.glob(my_path + '/**/*.jpg',recursive=True)
images = []
each_image = ''
# for root,dirs,files in os.walk(my_path,topdown=False):
# for file in files:
# each_image = file
# print(root)
# print(dirs)
for root,topdown=False):
for file in files:
image_path = file
with open(root + "/" + image_path,'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
response = client.text_detection(image=image)
texts = response.text_annotations
img_openCV = cv2.imread(root + "/" + image_path)
h,w = img_openCV.shape[:2]
print(h,w)
for text in texts:
box_w = abs(text.bounding_poly.vertices[2].x - text.bounding_poly.vertices[0].x)
box_h = abs(text.bounding_poly.vertices[2].y - text.bounding_poly.vertices[0].y)
if box_h * box_w > thres * h * w:
continue
if (text.bounding_poly.vertices[2].x - text.bounding_poly.vertices[0].x) > (w / 2):
print('ebug width ')
print(text.bounding_poly.vertices[2].x - text.bounding_poly.vertices[0].x)
else:
imagee = cv2.rectangle(img_openCV,(text.bounding_poly.vertices[0].x,text.bounding_poly.vertices[0].y),(text.bounding_poly.vertices[2].x,text.bounding_poly.vertices[2].y),(0,255,0),-1)
print('height,width,color:',text.bounding_poly)
if root.__contains__("BC"):
cv2.imwrite(os.path.join("/Specific_class",file),imagee)
elif root.__contains__("BK"):
cv2.imwrite(os.path.join("/Specific_class",imagee)
elif root.__contains__("CC"):
cv2.imwrite(os.path.join("/Specific_class",imagee)
total_images += 1
# cv2.imshow('window_name',imagee)
# cv2.waitKey(0)
# closing all open windows
# cv2.destroyAllWindows()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。