【发布时间】:2021-08-31 00:07:06
【问题描述】:
我有一个 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
【问题讨论】:
标签: python python-3.x list glob subdirectory